-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add axum integration for `IntoParams` of `Path` and `Query` parameters. Refactor extension argument resolving logic and refactor actix-web and rocket extension logic. Also fix extension related tests and docs. Allow comparing types by their `path` for `IntoParams` types. Previously comparison was done based on `Ident` causing name clashes in case there would be `IntoParams` type with same name but different module. Remove some unnecessary clones and address clippy lint rules. Fix axum and actix-web extensions.
- Loading branch information
Showing
18 changed files
with
700 additions
and
329 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
#![cfg(feature = "axum_extras")] | ||
#![cfg(feature = "serde_json")] | ||
|
||
use assert_json_diff::assert_json_eq; | ||
use axum::extract::{Path, Query}; | ||
use serde::Deserialize; | ||
use serde_json::json; | ||
use utoipa::{IntoParams, OpenApi}; | ||
|
||
#[test] | ||
fn derive_path_params_into_params_axum() { | ||
#[derive(Deserialize, IntoParams)] | ||
#[allow(unused)] | ||
struct Person { | ||
/// Id of person | ||
id: i64, | ||
/// Name of person | ||
name: String, | ||
} | ||
|
||
pub mod custom { | ||
use serde::Deserialize; | ||
use utoipa::IntoParams; | ||
#[derive(Deserialize, IntoParams)] | ||
#[allow(unused)] | ||
pub(super) struct Filter { | ||
/// Age filter for user | ||
#[deprecated] | ||
age: Option<Vec<String>>, | ||
} | ||
} | ||
|
||
#[utoipa::path( | ||
get, | ||
path = "/person/{id}/{name}", | ||
params(Person, custom::Filter), | ||
responses( | ||
(status = 200, description = "success response") | ||
) | ||
)] | ||
#[allow(unused)] | ||
async fn get_person(person: Path<Person>, query: Query<custom::Filter>) {} | ||
|
||
#[derive(OpenApi)] | ||
#[openapi(handlers(get_person))] | ||
struct ApiDoc; | ||
|
||
let doc = serde_json::to_value(ApiDoc::openapi()).unwrap(); | ||
let parameters = doc | ||
.pointer("/paths/~1person~1{id}~1{name}/get/parameters") | ||
.unwrap(); | ||
|
||
assert_json_eq!( | ||
parameters, | ||
&json!([ | ||
{ | ||
"description": "Id of person", | ||
"in": "path", | ||
"name": "id", | ||
"required": true, | ||
"schema": { | ||
"format": "int64", | ||
"type": "integer", | ||
}, | ||
}, | ||
{ | ||
"description": "Name of person", | ||
"in": "path", | ||
"name": "name", | ||
"required": true, | ||
"schema": { | ||
"type": "string", | ||
}, | ||
}, | ||
{ | ||
"deprecated": true, | ||
"description": "Age filter for user", | ||
"in": "query", | ||
"name": "age", | ||
"required": false, | ||
"schema": { | ||
"items": { | ||
"type": "string", | ||
}, | ||
"type": "array", | ||
} | ||
}, | ||
]) | ||
) | ||
} | ||
|
||
#[test] | ||
fn derive_path_params_into_params_unnamed() { | ||
#[derive(Deserialize, IntoParams)] | ||
#[into_params(names("id", "name"))] | ||
struct IdAndName(u64, String); | ||
|
||
#[utoipa::path( | ||
get, | ||
path = "/person/{id}/{name}", | ||
params(IdAndName), | ||
responses( | ||
(status = 200, description = "success response") | ||
) | ||
)] | ||
#[allow(unused)] | ||
async fn get_person(person: Path<IdAndName>) {} | ||
|
||
#[derive(OpenApi)] | ||
#[openapi(handlers(get_person))] | ||
struct ApiDoc; | ||
|
||
let doc = serde_json::to_value(ApiDoc::openapi()).unwrap(); | ||
let parameters = doc | ||
.pointer("/paths/~1person~1{id}~1{name}/get/parameters") | ||
.unwrap(); | ||
|
||
assert_json_eq!( | ||
parameters, | ||
&json!([ | ||
{ | ||
"in": "path", | ||
"name": "id", | ||
"required": true, | ||
"schema": { | ||
"format": "int64", | ||
"type": "integer", | ||
}, | ||
}, | ||
{ | ||
"in": "path", | ||
"name": "name", | ||
"required": true, | ||
"schema": { | ||
"type": "string", | ||
}, | ||
}, | ||
]) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.