-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
491 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
use utoipa::OpenApi; | ||
|
||
mod common; | ||
|
||
macro_rules! test_fn { | ||
( module: $name:ident, body: $body:expr ) => { | ||
#[allow(unused)] | ||
mod $name { | ||
|
||
struct Foo { | ||
name: String, | ||
} | ||
#[utoipa::path( | ||
post, | ||
path = "/foo", | ||
request_body = $body, | ||
responses = [ | ||
(200, "success", String), | ||
] | ||
)] | ||
fn post_foo() {} | ||
} | ||
}; | ||
} | ||
|
||
test_fn! { | ||
module: derive_request_body_simple, | ||
body: Foo | ||
} | ||
|
||
#[test] | ||
fn derive_path_request_body_simple_success() { | ||
#[derive(OpenApi, Default)] | ||
#[openapi(handler_files = [], handlers = [derive_request_body_simple::post_foo])] | ||
struct ApiDoc; | ||
|
||
let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap(); | ||
|
||
assert_value! {doc=> | ||
"paths./foo.post.requestBody.content.application/json.schema.$ref" = r###""#/components/schemas/Foo""###, "Request body content object type" | ||
"paths./foo.post.requestBody.content.text/plain" = r###"null"###, "Request body content object type not text/plain" | ||
"paths./foo.post.requestBody.required" = r###"null"###, "Request body required" | ||
"paths./foo.post.requestBody.description" = r###"null"###, "Request body description" | ||
} | ||
} | ||
|
||
test_fn! { | ||
module: derive_request_body_simple_array, | ||
body: [Foo] | ||
} | ||
|
||
#[test] | ||
fn derive_path_request_body_simple_array_success() { | ||
#[derive(OpenApi, Default)] | ||
#[openapi(handler_files = [], handlers = [derive_request_body_simple_array::post_foo])] | ||
struct ApiDoc; | ||
|
||
let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap(); | ||
|
||
assert_value! {doc=> | ||
"paths./foo.post.requestBody.content.application/json.schema.$ref" = r###"null"###, "Request body content object type" | ||
"paths./foo.post.requestBody.content.application/json.schema.items.$ref" = r###""#/components/schemas/Foo""###, "Request body content items object type" | ||
"paths./foo.post.requestBody.content.application/json.schema.type" = r###""array""###, "Request body content items type" | ||
"paths./foo.post.requestBody.content.text/plain" = r###"null"###, "Request body content object type not text/plain" | ||
"paths./foo.post.requestBody.required" = r###"null"###, "Request body required" | ||
"paths./foo.post.requestBody.description" = r###"null"###, "Request body description" | ||
} | ||
} | ||
|
||
test_fn! { | ||
module: derive_request_body_primitive_simple, | ||
body: String | ||
} | ||
|
||
#[test] | ||
fn derive_request_body_primitive_simple_success() { | ||
#[derive(OpenApi, Default)] | ||
#[openapi(handler_files = [], handlers = [derive_request_body_primitive_simple::post_foo])] | ||
struct ApiDoc; | ||
|
||
let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap(); | ||
|
||
assert_value! {doc=> | ||
"paths./foo.post.requestBody.content.application/json.schema.$ref" = r###"null"###, "Request body content object type not application/json" | ||
"paths./foo.post.requestBody.content.application/json.schema.items.$ref" = r###"null"###, "Request body content items object type" | ||
"paths./foo.post.requestBody.content.application/json.schema.type" = r###"null"###, "Request body content items type" | ||
"paths./foo.post.requestBody.content.text/plain.schema.type" = r###""string""###, "Request body content object type" | ||
"paths./foo.post.requestBody.required" = r###"null"###, "Request body required" | ||
"paths./foo.post.requestBody.description" = r###"null"###, "Request body description" | ||
} | ||
} | ||
|
||
test_fn! { | ||
module: derive_request_body_primitive_simple_array, | ||
body: [u64] | ||
} | ||
|
||
#[test] | ||
fn derive_request_body_primitive_array_success() { | ||
#[derive(OpenApi, Default)] | ||
#[openapi(handler_files = [], handlers = [derive_request_body_primitive_simple_array::post_foo])] | ||
struct ApiDoc; | ||
|
||
let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap(); | ||
|
||
assert_value! {doc=> | ||
"paths./foo.post.requestBody.content.application/json" = r###"null"###, "Request body content object type not application/json" | ||
"paths./foo.post.requestBody.content.text/plain.schema.type" = r###""array""###, "Request body content object item type" | ||
"paths./foo.post.requestBody.content.text/plain.schema.items.type" = r###""integer""###, "Request body content items object type" | ||
"paths./foo.post.requestBody.content.text/plain.schema.items.format" = r###""int64""###, "Request body content items object format" | ||
"paths./foo.post.requestBody.required" = r###"null"###, "Request body required" | ||
"paths./foo.post.requestBody.description" = r###"null"###, "Request body description" | ||
} | ||
} | ||
|
||
test_fn! { | ||
module: derive_request_body_complex, | ||
body: (content = Foo, required, description = "Create new Foo", content_type = "application/xml") | ||
} | ||
|
||
#[test] | ||
fn derive_request_body_complex_success() { | ||
#[derive(OpenApi, Default)] | ||
#[openapi(handler_files = [], handlers = [derive_request_body_complex::post_foo])] | ||
struct ApiDoc; | ||
|
||
let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap(); | ||
|
||
assert_value! {doc=> | ||
"paths./foo.post.requestBody.content.application/json" = r###"null"###, "Request body content object type not application/json" | ||
"paths./foo.post.requestBody.content.application/xml.schema.$ref" = r###""#/components/schemas/Foo""###, "Request body content object type" | ||
"paths./foo.post.requestBody.content.text/plain.schema.type" = r###"null"###, "Request body content object item type" | ||
"paths./foo.post.requestBody.content.text/plain.schema.items.type" = r###"null"###, "Request body content items object type" | ||
"paths./foo.post.requestBody.required" = r###"true"###, "Request body required" | ||
"paths./foo.post.requestBody.description" = r###""Create new Foo""###, "Request body description" | ||
} | ||
} | ||
|
||
test_fn! { | ||
module: derive_request_body_complex_required_explisit, | ||
body: (content = Foo, required = false, description = "Create new Foo", content_type = "application/xml") | ||
} | ||
|
||
#[test] | ||
fn derive_request_body_complex_required_explisit_false_success() { | ||
#[derive(OpenApi, Default)] | ||
#[openapi(handler_files = [], handlers = [derive_request_body_complex_required_explisit::post_foo])] | ||
struct ApiDoc; | ||
|
||
let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap(); | ||
|
||
assert_value! {doc=> | ||
"paths./foo.post.requestBody.content.application/json" = r###"null"###, "Request body content object type not application/json" | ||
"paths./foo.post.requestBody.content.application/xml.schema.$ref" = r###""#/components/schemas/Foo""###, "Request body content object type" | ||
"paths./foo.post.requestBody.content.text/plain.schema.type" = r###"null"###, "Request body content object item type" | ||
"paths./foo.post.requestBody.content.text/plain.schema.items.type" = r###"null"###, "Request body content items object type" | ||
"paths./foo.post.requestBody.required" = r###"false"###, "Request body required" | ||
"paths./foo.post.requestBody.description" = r###""Create new Foo""###, "Request body description" | ||
} | ||
} | ||
|
||
test_fn! { | ||
module: derive_request_body_complex_primitive_array, | ||
body: (content = [u32], description = "Create new foo references") | ||
} | ||
|
||
#[test] | ||
fn derive_request_body_complex_primitive_array_success() { | ||
#[derive(OpenApi, Default)] | ||
#[openapi(handler_files = [], handlers = [derive_request_body_complex_primitive_array::post_foo])] | ||
struct ApiDoc; | ||
|
||
let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap(); | ||
|
||
assert_value! {doc=> | ||
"paths./foo.post.requestBody.content.application/json" = r###"null"###, "Request body content object type not application/json" | ||
"paths./foo.post.requestBody.content.text/plain.schema.type" = r###""array""###, "Request body content object item type" | ||
"paths./foo.post.requestBody.content.text/plain.schema.items.type" = r###""integer""###, "Request body content items object type" | ||
"paths./foo.post.requestBody.content.text/plain.schema.items.format" = r###""int32""###, "Request body content items object format" | ||
"paths./foo.post.requestBody.required" = r###"null"###, "Request body required" | ||
"paths./foo.post.requestBody.description" = r###""Create new foo references""###, "Request body description" | ||
} | ||
} |
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
Oops, something went wrong.