mirror of https://github.com/fafhrd91/actix-web
Compare commits
9 Commits
83ce545a59
...
5e99f427b7
Author | SHA1 | Date |
---|---|---|
|
5e99f427b7 | |
|
fcd10fbb5e | |
|
95b6a81f43 | |
|
ab18efe0ac | |
|
b47f65063b | |
|
ea19d26724 | |
|
7038244a7d | |
|
04e8a417e1 | |
|
570d0fc156 |
|
@ -0,0 +1,3 @@
|
|||
version: "0.2"
|
||||
words:
|
||||
- actix
|
|
@ -49,7 +49,7 @@ jobs:
|
|||
toolchain: ${{ matrix.version.version }}
|
||||
|
||||
- name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean
|
||||
uses: taiki-e/install-action@v2.49.17
|
||||
uses: taiki-e/install-action@v2.49.32
|
||||
with:
|
||||
tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean
|
||||
|
||||
|
@ -83,7 +83,7 @@ jobs:
|
|||
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||
|
||||
- name: Install just, cargo-hack
|
||||
uses: taiki-e/install-action@v2.49.17
|
||||
uses: taiki-e/install-action@v2.49.32
|
||||
with:
|
||||
tool: just,cargo-hack
|
||||
|
||||
|
|
|
@ -64,7 +64,7 @@ jobs:
|
|||
toolchain: ${{ matrix.version.version }}
|
||||
|
||||
- name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean
|
||||
uses: taiki-e/install-action@v2.49.17
|
||||
uses: taiki-e/install-action@v2.49.32
|
||||
with:
|
||||
tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean
|
||||
|
||||
|
@ -113,7 +113,7 @@ jobs:
|
|||
toolchain: nightly
|
||||
|
||||
- name: Install just
|
||||
uses: taiki-e/install-action@v2.49.17
|
||||
uses: taiki-e/install-action@v2.49.32
|
||||
with:
|
||||
tool: just
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ jobs:
|
|||
components: llvm-tools
|
||||
|
||||
- name: Install just, cargo-llvm-cov, cargo-nextest
|
||||
uses: taiki-e/install-action@v2.49.17
|
||||
uses: taiki-e/install-action@v2.49.32
|
||||
with:
|
||||
tool: just,cargo-llvm-cov,cargo-nextest
|
||||
|
||||
|
|
|
@ -77,7 +77,7 @@ jobs:
|
|||
toolchain: ${{ vars.RUST_VERSION_EXTERNAL_TYPES }}
|
||||
|
||||
- name: Install just
|
||||
uses: taiki-e/install-action@v2.49.17
|
||||
uses: taiki-e/install-action@v2.49.32
|
||||
with:
|
||||
tool: just
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
Cargo.lock
|
||||
target/
|
||||
guide/build/
|
||||
/gh-pages
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -18,8 +18,8 @@ all-features = true
|
|||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
bytesize = "2"
|
||||
darling = "0.20"
|
||||
parse-size = "1"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = "2"
|
||||
|
@ -27,7 +27,7 @@ syn = "2"
|
|||
[dev-dependencies]
|
||||
actix-multipart = "0.7"
|
||||
actix-web = "4"
|
||||
rustversion = "1"
|
||||
rustversion-msrv = "0.100"
|
||||
trybuild = "1"
|
||||
|
||||
[lints]
|
||||
|
|
|
@ -9,8 +9,8 @@
|
|||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use bytesize::ByteSize;
|
||||
use darling::{FromDeriveInput, FromField, FromMeta};
|
||||
use parse_size::parse_size;
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::Ident;
|
||||
use quote::quote;
|
||||
|
@ -103,7 +103,7 @@ struct ParsedField<'t> {
|
|||
/// # Field Limits
|
||||
///
|
||||
/// You can use the `#[multipart(limit = "<size>")]` attribute to set field level limits. The limit
|
||||
/// string is parsed using [parse_size].
|
||||
/// string is parsed using [`bytesize`].
|
||||
///
|
||||
/// Note: the form is also subject to the global limits configured using `MultipartFormConfig`.
|
||||
///
|
||||
|
@ -150,7 +150,7 @@ struct ParsedField<'t> {
|
|||
/// struct Form { }
|
||||
/// ```
|
||||
///
|
||||
/// [parse_size]: https://docs.rs/parse-size/1/parse_size
|
||||
/// [`bytesize`]: https://docs.rs/bytesize/2
|
||||
#[proc_macro_derive(MultipartForm, attributes(multipart))]
|
||||
pub fn impl_multipart_form(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input: syn::DeriveInput = parse_macro_input!(input);
|
||||
|
@ -191,8 +191,8 @@ pub fn impl_multipart_form(input: proc_macro::TokenStream) -> proc_macro::TokenS
|
|||
let attrs = FieldAttrs::from_field(field).map_err(|err| err.write_errors())?;
|
||||
let serialization_name = attrs.rename.unwrap_or_else(|| rust_name.to_string());
|
||||
|
||||
let limit = match attrs.limit.map(|limit| match parse_size(&limit) {
|
||||
Ok(size) => Ok(usize::try_from(size).unwrap()),
|
||||
let limit = match attrs.limit.map(|limit| match limit.parse::<ByteSize>() {
|
||||
Ok(ByteSize(size)) => Ok(usize::try_from(size).unwrap()),
|
||||
Err(err) => Err(syn::Error::new(
|
||||
field.ident.as_ref().unwrap().span(),
|
||||
format!("Could not parse size limit `{}`: {}", limit, err),
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#[rustversion::stable(1.72)] // MSRV
|
||||
#[rustversion_msrv::msrv]
|
||||
#[test]
|
||||
fn compile_macros() {
|
||||
let t = trybuild::TestCases::new();
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
error: Could not parse size limit `2 bytes`: invalid digit found in string
|
||||
error: Could not parse size limit `2 bytes`: couldn't parse "bytes" into a known SI unit, couldn't parse unit of "bytes"
|
||||
--> tests/trybuild/size-limit-parse-fail.rs:6:5
|
||||
|
|
||||
6 | description: Text<String>,
|
||||
| ^^^^^^^^^^^
|
||||
|
||||
error: Could not parse size limit `2 megabytes`: invalid digit found in string
|
||||
error: Could not parse size limit `2 megabytes`: couldn't parse "megabytes" into a known SI unit, couldn't parse unit of "megabytes"
|
||||
--> tests/trybuild/size-limit-parse-fail.rs:12:5
|
||||
|
|
||||
12 | description: Text<String>,
|
||||
| ^^^^^^^^^^^
|
||||
|
||||
error: Could not parse size limit `four meters`: invalid digit found in string
|
||||
error: Could not parse size limit `four meters`: couldn't parse "four meters" into a ByteSize, cannot parse float from empty string
|
||||
--> tests/trybuild/size-limit-parse-fail.rs:18:5
|
||||
|
|
||||
18 | description: Text<String>,
|
||||
|
|
|
@ -34,7 +34,7 @@ actix-web = "4"
|
|||
|
||||
futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
||||
trybuild = "1"
|
||||
rustversion = "1"
|
||||
rustversion-msrv = "0.100"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#[rustversion::stable(1.72)] // MSRV
|
||||
#[rustversion_msrv::msrv]
|
||||
#[test]
|
||||
fn compile_macros() {
|
||||
let t = trybuild::TestCases::new();
|
||||
|
|
|
@ -33,6 +33,7 @@ features = [
|
|||
"compress-zstd",
|
||||
"cookies",
|
||||
"secure-cookies",
|
||||
"beautify-errors"
|
||||
]
|
||||
|
||||
[package.metadata.cargo_check_external_types]
|
||||
|
@ -89,6 +90,9 @@ cookies = ["dep:cookie"]
|
|||
# Secure & signed cookies
|
||||
secure-cookies = ["cookies", "cookie/secure"]
|
||||
|
||||
# Field names in deserialization errors
|
||||
beautify-errors = ["dep:serde_path_to_error"]
|
||||
|
||||
# HTTP/2 support (including h2c).
|
||||
http2 = ["actix-http/http2"]
|
||||
|
||||
|
@ -161,6 +165,7 @@ regex = { version = "1.5.5", optional = true }
|
|||
regex-lite = "0.1"
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
serde_path_to_error = { version = "0.1", optional = true }
|
||||
serde_urlencoded = "0.7"
|
||||
smallvec = "1.6.1"
|
||||
tracing = "0.1.30"
|
||||
|
@ -213,6 +218,10 @@ required-features = ["compress-gzip"]
|
|||
name = "on-connect"
|
||||
required-features = []
|
||||
|
||||
[[example]]
|
||||
name = "error"
|
||||
required-features = ["beautify-errors"]
|
||||
|
||||
[[bench]]
|
||||
name = "server"
|
||||
harness = false
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
use actix_web::{
|
||||
get, middleware, post,
|
||||
web::{Json, Query},
|
||||
App, HttpServer,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[get("/optional")]
|
||||
async fn optional_query_params(maybe_qs: Option<Query<OptionalFilters>>) -> String {
|
||||
format!("you asked for the optional query params: {:#?}", maybe_qs)
|
||||
}
|
||||
|
||||
#[get("/mandatory")]
|
||||
async fn mandatory_query_params(qs: Query<MandatoryFilters>) -> String {
|
||||
format!("you asked for the mandatory query params: {:#?}", qs)
|
||||
}
|
||||
|
||||
#[post("/optional")]
|
||||
async fn optional_payload(
|
||||
maybe_qs: Option<Query<OptionalFilters>>,
|
||||
maybe_payload: Option<Json<OptionalPayload>>,
|
||||
) -> String {
|
||||
format!(
|
||||
"you asked for the optional query params: {:#?} and optional body: {:#?}",
|
||||
maybe_qs, maybe_payload
|
||||
)
|
||||
}
|
||||
|
||||
#[post("/mandatory")]
|
||||
async fn mandatory_payload(qs: Query<MandatoryFilters>, payload: Json<MandatoryPayload>) -> String {
|
||||
format!(
|
||||
"you asked for the mandatory query params: {:#?} and mandatory body: {:#?}",
|
||||
qs, payload
|
||||
)
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(optional_query_params)
|
||||
.service(mandatory_query_params)
|
||||
.service(optional_payload)
|
||||
.service(mandatory_payload)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.workers(1)
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct OptionalFilters {
|
||||
pub limit: Option<i32>,
|
||||
pub active: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct MandatoryFilters {
|
||||
pub limit: i32,
|
||||
pub active: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct OptionalPayload {
|
||||
pub name: Option<String>,
|
||||
pub age: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct MandatoryPayload {
|
||||
pub name: String,
|
||||
pub age: i32,
|
||||
}
|
|
@ -0,0 +1,848 @@
|
|||
{
|
||||
"info": {
|
||||
"_postman_id": "1147f102-8d16-40e4-8642-5f0679879e59",
|
||||
"name": "actix-web",
|
||||
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
|
||||
},
|
||||
"item": [
|
||||
{
|
||||
"name": "field in deserialize errors",
|
||||
"item": [
|
||||
{
|
||||
"name": "optional filters",
|
||||
"item": [
|
||||
{
|
||||
"name": "without filters",
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/optional",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"optional"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with a single filter",
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/optional?limit=1",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"optional"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "1"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with both filters",
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/optional?limit=1&active=true",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"optional"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "1"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "true"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with a single invalid filter",
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/optional?limit=wrong&active=true",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"optional"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "wrong"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "true"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with both invalid filters",
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/optional?limit=wrong&active=wrong",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"optional"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "wrong"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "wrong"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "mandatory filters",
|
||||
"item": [
|
||||
{
|
||||
"name": "without filters",
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/mandatory",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"mandatory"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with a single filter",
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/mandatory?limit=1",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"mandatory"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "1"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with both filters",
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/mandatory?limit=1&active=true",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"mandatory"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "1"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "true"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with a single invalid filter",
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/mandatory?limit=wrong&active=true",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"mandatory"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "wrong"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "true"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with both invalid filters",
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/mandatory?limit=wrong&active=wrong",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"mandatory"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "wrong"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "wrong"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "optional filters and payload",
|
||||
"item": [
|
||||
{
|
||||
"name": "without filters",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": 13\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/optional",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"optional"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with a single filter",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": 13\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/optional?limit=1",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"optional"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "1"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with both filters",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": 13\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/optional?limit=1&active=true",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"optional"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "1"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "true"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with a single invalid filter",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": 13\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/optional?limit=wrong&active=true",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"optional"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "wrong"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "true"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with both invalid filters",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": 13\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/optional?limit=wrong&active=wrong",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"optional"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "wrong"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "wrong"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with both filters and invalid payload",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": \"wrong\"\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/optional?limit=1&active=true",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"optional"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "1"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "true"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with both invalid filters and invalid payload",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": \"wrong\"\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/optional?limit=wrong&active=wrong",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"optional"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "wrong"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "wrong"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "mandatory filters and payload",
|
||||
"item": [
|
||||
{
|
||||
"name": "without filters",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": 13\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/mandatory",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"mandatory"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with a single filter",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": 13\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/mandatory?limit=1",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"mandatory"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "1"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with both filters",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": 13\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/mandatory?limit=1&active=true",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"mandatory"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "1"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "true"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with a single invalid filter",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": 13\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/mandatory?limit=wrong&active=true",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"mandatory"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "wrong"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "true"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with both invalid filters",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": 13\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/mandatory?limit=wrong&active=wrong",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"mandatory"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "wrong"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "wrong"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with both filters and invalid payload",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": \"wrong\"\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/mandatory?limit=1&active=true",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"mandatory"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "1"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "true"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "with both invalid filters and invalid payload",
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"header": [],
|
||||
"body": {
|
||||
"mode": "raw",
|
||||
"raw": "{\n \"name\": \"John\",\n \"age\": \"wrong\"\n}",
|
||||
"options": {
|
||||
"raw": {
|
||||
"language": "json"
|
||||
}
|
||||
}
|
||||
},
|
||||
"url": {
|
||||
"raw": "127.0.0.1:8080/mandatory?limit=wrong&active=wrong",
|
||||
"host": [
|
||||
"127",
|
||||
"0",
|
||||
"0",
|
||||
"1"
|
||||
],
|
||||
"port": "8080",
|
||||
"path": [
|
||||
"mandatory"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "limit",
|
||||
"value": "wrong"
|
||||
},
|
||||
{
|
||||
"key": "active",
|
||||
"value": "wrong"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
|
@ -17,6 +17,8 @@ use serde::{de::DeserializeOwned, Serialize};
|
|||
|
||||
#[cfg(feature = "__compress")]
|
||||
use crate::dev::Decompress;
|
||||
#[cfg(feature = "beautify-errors")]
|
||||
use crate::web::map_deserialize_error;
|
||||
use crate::{
|
||||
body::EitherBody,
|
||||
error::{Error, JsonPayloadError},
|
||||
|
@ -427,11 +429,28 @@ impl<T: DeserializeOwned> Future for JsonBody<T> {
|
|||
buf.extend_from_slice(&chunk);
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "beautify-errors"))]
|
||||
None => {
|
||||
let json = serde_json::from_slice::<T>(buf)
|
||||
.map_err(JsonPayloadError::Deserialize)?;
|
||||
return Poll::Ready(Ok(json));
|
||||
}
|
||||
#[cfg(feature = "beautify-errors")]
|
||||
None => {
|
||||
let mut deserializer = serde_json::Deserializer::from_slice(buf);
|
||||
let json =
|
||||
serde_path_to_error::deserialize(&mut deserializer).map_err(|e| {
|
||||
JsonPayloadError::Deserialize(
|
||||
<serde_json::error::Error as serde::de::Error>::custom(
|
||||
map_deserialize_error(
|
||||
&e.path().to_string(),
|
||||
&e.inner().to_string(),
|
||||
),
|
||||
),
|
||||
)
|
||||
})?;
|
||||
return Poll::Ready(Ok(json));
|
||||
}
|
||||
}
|
||||
},
|
||||
JsonBody::Error(err) => Poll::Ready(Err(err.take().unwrap())),
|
||||
|
|
|
@ -21,3 +21,11 @@ pub use self::{
|
|||
query::{Query, QueryConfig},
|
||||
readlines::Readlines,
|
||||
};
|
||||
|
||||
#[cfg(feature = "beautify-errors")]
|
||||
pub fn map_deserialize_error(field: &str, original: &str) -> String {
|
||||
if field == "." {
|
||||
return original.to_string();
|
||||
}
|
||||
format!("'{}': {}", field, original)
|
||||
}
|
||||
|
|
|
@ -4,7 +4,11 @@ use std::{fmt, ops, sync::Arc};
|
|||
|
||||
use actix_utils::future::{ok, ready, Ready};
|
||||
use serde::de::DeserializeOwned;
|
||||
#[cfg(feature = "beautify-errors")]
|
||||
use url::form_urlencoded::parse;
|
||||
|
||||
#[cfg(feature = "beautify-errors")]
|
||||
use crate::web::map_deserialize_error;
|
||||
use crate::{dev::Payload, error::QueryPayloadError, Error, FromRequest, HttpRequest};
|
||||
|
||||
/// Extract typed information from the request's query.
|
||||
|
@ -61,7 +65,6 @@ use crate::{dev::Payload, error::QueryPayloadError, Error, FromRequest, HttpRequ
|
|||
pub struct Query<T>(pub T);
|
||||
|
||||
impl<T> Query<T> {
|
||||
/// Unwrap into inner `T` value.
|
||||
pub fn into_inner(self) -> T {
|
||||
self.0
|
||||
}
|
||||
|
@ -78,11 +81,27 @@ impl<T: DeserializeOwned> Query<T> {
|
|||
/// assert_eq!(numbers.get("two"), Some(&2));
|
||||
/// assert!(numbers.get("three").is_none());
|
||||
/// ```
|
||||
#[cfg(not(feature = "beautify-errors"))]
|
||||
pub fn from_query(query_str: &str) -> Result<Self, QueryPayloadError> {
|
||||
serde_urlencoded::from_str::<T>(query_str)
|
||||
.map(Self)
|
||||
.map_err(QueryPayloadError::Deserialize)
|
||||
}
|
||||
#[cfg(feature = "beautify-errors")]
|
||||
pub fn from_query(query_str: &str) -> Result<Self, QueryPayloadError> {
|
||||
let deserializer = serde_urlencoded::Deserializer::new(parse(query_str.as_bytes()));
|
||||
let qs = serde_path_to_error::deserialize(deserializer)
|
||||
.map(Self)
|
||||
.map_err(|e| {
|
||||
QueryPayloadError::Deserialize(
|
||||
<serde::de::value::Error as serde::de::Error>::custom(map_deserialize_error(
|
||||
&e.path().to_string(),
|
||||
&e.inner().to_string(),
|
||||
)),
|
||||
)
|
||||
})?;
|
||||
Ok(qs)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ops::Deref for Query<T> {
|
||||
|
@ -110,6 +129,7 @@ impl<T: DeserializeOwned> FromRequest for Query<T> {
|
|||
type Error = Error;
|
||||
type Future = Ready<Result<Self, Error>>;
|
||||
|
||||
#[cfg(not(feature = "beautify-errors"))]
|
||||
#[inline]
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
let error_handler = req
|
||||
|
@ -136,6 +156,41 @@ impl<T: DeserializeOwned> FromRequest for Query<T> {
|
|||
ready(Err(err))
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "beautify-errors")]
|
||||
#[inline]
|
||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||
let error_handler = req
|
||||
.app_data::<QueryConfig>()
|
||||
.and_then(|c| c.err_handler.clone());
|
||||
|
||||
let deserializer =
|
||||
serde_urlencoded::Deserializer::new(parse(req.query_string().as_bytes()));
|
||||
return serde_path_to_error::deserialize(deserializer)
|
||||
.map(|val| ok(Query(val)))
|
||||
.unwrap_or_else(move |e| {
|
||||
let e = QueryPayloadError::Deserialize(
|
||||
<serde::de::value::Error as serde::de::Error>::custom(map_deserialize_error(
|
||||
&e.path().to_string(),
|
||||
&e.inner().to_string(),
|
||||
)),
|
||||
);
|
||||
|
||||
log::debug!(
|
||||
"Failed during Query extractor deserialization. \
|
||||
Request path: {:?}",
|
||||
req.path()
|
||||
);
|
||||
|
||||
let e = if let Some(error_handler) = error_handler {
|
||||
(error_handler)(e, req)
|
||||
} else {
|
||||
e.into()
|
||||
};
|
||||
|
||||
err(e)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Query extractor configuration.
|
||||
|
|
12
justfile
12
justfile
|
@ -7,14 +7,14 @@ fmt:
|
|||
cargo +nightly fmt
|
||||
fd --hidden --type=file --extension=md --extension=yml --exec-batch npx -y prettier --write
|
||||
|
||||
# Downgrade dev-dependencies necessary to run MSRV checks/tests.
|
||||
# Downgrade dependencies necessary to run MSRV checks/tests.
|
||||
[private]
|
||||
downgrade-for-msrv:
|
||||
cargo update -p=parse-size --precise=1.0.0
|
||||
cargo update -p=clap --precise=4.4.18
|
||||
cargo update -p=divan --precise=0.1.15
|
||||
cargo update -p=litemap --precise=0.7.4
|
||||
cargo update -p=zerofrom --precise=0.1.5
|
||||
cargo update -p=clap --precise=4.4.18 # next ver: 1.74.0
|
||||
cargo update -p=divan --precise=0.1.15 # next ver: 1.80.0
|
||||
cargo update -p=litemap --precise=0.7.4 # next ver: 1.81.0
|
||||
cargo update -p=zerofrom --precise=0.1.5 # next ver: 1.81.0
|
||||
cargo update -p=half --precise=2.4.1 # next ver: 1.81.0
|
||||
|
||||
msrv := ```
|
||||
cargo metadata --format-version=1 \
|
||||
|
|
Loading…
Reference in New Issue