Skip to content

Commit

Permalink
Add more axum path parameter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juhaku committed Aug 3, 2023
1 parent 1dccaf4 commit 7cc90b1
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions utoipa-gen/tests/path_derive_axum_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,119 @@ fn test_into_params_for_option_query_type() {
])
)
}

#[test]
fn path_param_single_arg_primitive_type() {
#[utoipa::path(
get,
path = "/items/{id}",
params(("id" = u32, Path, description = "")),
responses(
(status = 200, description = "success response")
)
)]
#[allow(unused)]
async fn get_item(id: Path<u32>) {}

#[derive(OpenApi)]
#[openapi(paths(get_item))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
let operation = doc.pointer("/paths/~1items~1{id}/get").unwrap();

assert_json_eq!(
operation.pointer("/parameters"),
json!([
{
"description": "",
"in": "path",
"name": "id",
"required": true,
"schema": {
"format": "int32",
"type": "integer",
"minimum": 0
}
}
])
)
}

#[test]
fn path_param_single_arg_non_primitive_type() {
#[derive(utoipa::ToSchema)]
struct Id(String);

#[utoipa::path(
get,
path = "/items/{id}",
params(("id" = inline(Id), Path, description = "")),
responses(
(status = 200, description = "success response")
)
)]
#[allow(unused)]
async fn get_item(id: Path<Id>) {}

#[derive(OpenApi)]
#[openapi(paths(get_item))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
let operation = doc.pointer("/paths/~1items~1{id}/get").unwrap();

assert_json_eq!(
operation.pointer("/parameters"),
json!([
{
"description": "",
"in": "path",
"name": "id",
"required": true,
"schema": {
"type": "string",
}
}
])
)
}

#[test]
fn path_param_single_arg_non_primitive_type_into_params() {
#[derive(utoipa::ToSchema, utoipa::IntoParams)]
#[into_params(names("id"))]
struct Id(String);

#[utoipa::path(
get,
path = "/items/{id}",
params(Id),
responses(
(status = 200, description = "success response")
)
)]
#[allow(unused)]
async fn get_item(id: Path<Id>) {}

#[derive(OpenApi)]
#[openapi(paths(get_item))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
let operation = doc.pointer("/paths/~1items~1{id}/get").unwrap();

assert_json_eq!(
operation.pointer("/parameters"),
json!([
{
"in": "path",
"name": "id",
"required": true,
"schema": {
"type": "string",
}
}
])
)
}

0 comments on commit 7cc90b1

Please sign in to comment.