Skip to content

Commit

Permalink
Chore improve OpenApi derive macro
Browse files Browse the repository at this point in the history
* Improve OpenApi derive macro syntax. Now better (opinionated) syntax looks more clear, less equal (=) signs and is more consistent with other well known macros like serde.
* Fix tests and docs related to OpenApi macro
* Clean code and make it more consistent all around with less allocations.
  • Loading branch information
juhaku committed Mar 16, 2022
1 parent cc3e794 commit f2c1489
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 238 deletions.
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
//! # }
//! # use utoipa::OpenApi;
//! #[derive(OpenApi)]
//! #[openapi(handlers = [pet_api::get_pet_by_id], components = [Pet])]
//! #[openapi(handlers(pet_api::get_pet_by_id), components(Pet))]
//! struct ApiDoc;
//!
//! println!("{}", ApiDoc::openapi().to_pretty_json().unwrap());
Expand Down Expand Up @@ -175,7 +175,7 @@ pub use utoipa_gen::*;
/// ```rust
/// use utoipa::OpenApi;
/// #[derive(OpenApi)]
/// #[openapi(handlers = [])]
/// #[openapi(handlers())]
/// struct OpenApiDoc;
/// ```
///
Expand Down Expand Up @@ -373,7 +373,7 @@ pub trait Path {
/// # use utoipa::{OpenApi, Modify};
/// # use utoipa::openapi::security::{SecuritySchema, Http, HttpAuthenticationType};
/// #[derive(OpenApi)]
/// #[openapi(modifiers = [&SecurityAddon])]
/// #[openapi(modifiers(&SecurityAddon))]
/// struct ApiDoc;
///
/// struct SecurityAddon;
Expand Down
2 changes: 1 addition & 1 deletion tests/component_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ macro_rules! api_doc {

( @doc $name:ident $( $generic:tt )* ) => {{
#[derive(OpenApi)]
#[openapi(components = [$name$($generic)*])]
#[openapi(components($name$($generic)*))]
struct ApiDoc;

let json = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tests/modify_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod common;
#[test]
fn modify_openapi_add_security_schema() {
#[derive(Default, OpenApi)]
#[openapi(modifiers = [&SecurityAddon])]
#[openapi(modifiers(&SecurityAddon))]
struct ApiDoc;

struct SecurityAddon;
Expand Down
4 changes: 2 additions & 2 deletions tests/openapi_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ mod common;
#[test]
fn derive_openapi_with_security_requirement() {
#[derive(Default, OpenApi)]
#[openapi(security = [
#[openapi(security(
(),
("my_auth" = ["read:items", "edit:items"]),
("token_jwt" = [])
])]
))]
struct ApiDoc;

let doc_value = serde_json::to_value(&ApiDoc::openapi()).unwrap();
Expand Down
10 changes: 5 additions & 5 deletions tests/path_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ macro_rules! test_api_fn_doc {
( $handler:path, operation: $operation:expr, path: $path:literal ) => {{
use utoipa::OpenApi;
#[derive(OpenApi, Default)]
#[openapi(handlers = [$handler])]
#[openapi(handlers($handler))]
struct ApiDoc;

let doc = &serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -59,9 +59,9 @@ macro_rules! test_path_operation {
paste!{
use utoipa::OpenApi;
#[derive(OpenApi, Default)]
#[openapi(handlers = [
#[openapi(handlers(
[<mod_ $name>]::test_operation
])]
))]
struct ApiDoc;
}

Expand Down Expand Up @@ -207,11 +207,11 @@ fn derive_path_with_security_requirements() {
responses = [
(status = 200, description = "success response")
],
security = [
security(
(),
("api_oauth" = ["read:items", "edit:items"]),
("jwt_token" = [])
]
)
)]
#[allow(unused)]
fn get_items() -> String {
Expand Down
8 changes: 4 additions & 4 deletions tests/path_derive_actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod mod_derive_path_actix {
#[test]
fn derive_path_one_value_actix_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [mod_derive_path_actix::get_foo_by_id])]
#[openapi(handlers(mod_derive_path_actix::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -74,7 +74,7 @@ mod mod_derive_path_unnamed_regex_actix {
#[test]
fn derive_path_with_unnamed_regex_actix_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [mod_derive_path_unnamed_regex_actix::get_foo_by_id])]
#[openapi(handlers(mod_derive_path_unnamed_regex_actix::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -117,7 +117,7 @@ mod mod_derive_path_named_regex_actix {
#[test]
fn derive_path_with_named_regex_actix_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [mod_derive_path_named_regex_actix::get_foo_by_id])]
#[openapi(handlers(mod_derive_path_named_regex_actix::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -157,7 +157,7 @@ macro_rules! test_derive_path_operations {
#[test]
fn $name() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [$mod::test_operation])]
#[openapi(handlers($mod::test_operation))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions tests/path_parameter_derive_actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod derive_params_multiple_actix {
#[test]
fn derive_path_parameter_multiple_with_matching_names_and_types_actix_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_params_multiple_actix::get_foo_by_id])]
#[openapi(handlers(derive_params_multiple_actix::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -86,7 +86,7 @@ mod derive_parameters_multiple_no_matching_names_actix {
#[test]
fn derive_path_parameter_multiple_no_matching_names_acitx_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_parameters_multiple_no_matching_names_actix::get_foo_by_id])]
#[openapi(handlers(derive_parameters_multiple_no_matching_names_actix::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -132,7 +132,7 @@ mod derive_params_from_method_args_actix {
#[test]
fn derive_params_from_method_args_actix_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_params_from_method_args_actix::get_foo_by_id])]
#[openapi(handlers(derive_params_from_method_args_actix::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down
10 changes: 5 additions & 5 deletions tests/path_parameter_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod derive_params_all_options {
#[test]
fn derive_path_parameters_with_all_options_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_params_all_options::get_foo_by_id])]
#[openapi(handlers(derive_params_all_options::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -69,7 +69,7 @@ mod derive_params_minimal {
#[test]
fn derive_path_parameters_minimal_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_params_minimal::get_foo_by_id])]
#[openapi(handlers(derive_params_minimal::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -111,7 +111,7 @@ mod derive_params_multiple {
#[test]
fn derive_path_parameter_multiple_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_params_multiple::get_foo_by_id])]
#[openapi(handlers(derive_params_multiple::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -164,7 +164,7 @@ mod mod_derive_parameters_all_types {
#[test]
fn derive_parameters_with_all_types() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [mod_derive_parameters_all_types::get_foo_by_id])]
#[openapi(handlers(mod_derive_parameters_all_types::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -236,7 +236,7 @@ mod derive_params_without_args {
#[test]
fn derive_params_without_fn_args() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_params_without_args::get_foo_by_id])]
#[openapi(handlers(derive_params_without_args::get_foo_by_id))]
struct ApiDoc;

let doc = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tests/path_response_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ macro_rules! api_doc {
( module: $module:expr ) => {{
use utoipa::OpenApi;
#[derive(OpenApi, Default)]
#[openapi(handlers = [$module::get_foo])]
#[openapi(handlers($module::get_foo))]
struct ApiDoc;

let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tests/path_response_derive_test_no_serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn derive_response_with_string_example_compiles_success() {
use utoipa::OpenApi;

#[derive(OpenApi, Default)]
#[openapi(handlers = [response_with_string_example::get_foo])]
#[openapi(handlers(response_with_string_example::get_foo))]
struct ApiDoc;
ApiDoc::openapi();
}
16 changes: 8 additions & 8 deletions tests/request_body_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test_fn! {
#[test]
fn derive_path_request_body_simple_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_request_body_simple::post_foo])]
#[openapi(handlers(derive_request_body_simple::post_foo))]
struct ApiDoc;

let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap();
Expand All @@ -53,7 +53,7 @@ test_fn! {
#[test]
fn derive_path_request_body_simple_array_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_request_body_simple_array::post_foo])]
#[openapi(handlers(derive_request_body_simple_array::post_foo))]
struct ApiDoc;

let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap();
Expand All @@ -76,7 +76,7 @@ test_fn! {
#[test]
fn derive_request_body_option_array_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_request_body_option_array::post_foo])]
#[openapi(handlers(derive_request_body_option_array::post_foo))]
struct ApiDoc;

let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap();
Expand All @@ -98,7 +98,7 @@ test_fn! {
#[test]
fn derive_request_body_primitive_simple_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_request_body_primitive_simple::post_foo])]
#[openapi(handlers(derive_request_body_primitive_simple::post_foo))]
struct ApiDoc;

let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap();
Expand All @@ -121,7 +121,7 @@ test_fn! {
#[test]
fn derive_request_body_primitive_array_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_request_body_primitive_simple_array::post_foo])]
#[openapi(handlers(derive_request_body_primitive_simple_array::post_foo))]
struct ApiDoc;

let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap();
Expand All @@ -144,7 +144,7 @@ test_fn! {
#[test]
fn derive_request_body_complex_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_request_body_complex::post_foo])]
#[openapi(handlers(derive_request_body_complex::post_foo))]
struct ApiDoc;

let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap();
Expand All @@ -167,7 +167,7 @@ test_fn! {
#[test]
fn derive_request_body_complex_required_explisit_false_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_request_body_complex_required_explisit::post_foo])]
#[openapi(handlers(derive_request_body_complex_required_explisit::post_foo))]
struct ApiDoc;

let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap();
Expand All @@ -190,7 +190,7 @@ test_fn! {
#[test]
fn derive_request_body_complex_primitive_array_success() {
#[derive(OpenApi, Default)]
#[openapi(handlers = [derive_request_body_complex_primitive_array::post_foo])]
#[openapi(handlers(derive_request_body_complex_primitive_array::post_foo))]
struct ApiDoc;

let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap();
Expand Down
19 changes: 12 additions & 7 deletions tests/utoipa_gen_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ mod pet_api {
params = [
("id" = u64, path, description = "Pet database id to get Pet for"),
]
security = [
security(
(),
("my_auth" = ["read:items", "edit:items"]),
("token_jwt" = [])
]
)
)]
#[allow(unused)]
async fn get_pet_by_id(pet_id: u64) -> Pet {
Expand All @@ -76,11 +76,16 @@ mod pet_api {
}

#[derive(Default, OpenApi)]
#[openapi(handlers = [pet_api::get_pet_by_id], components = [Pet], modifiers = [&Foo], security = [
(),
("my_auth" = ["read:items", "edit:items"]),
("token_jwt" = [])
])]
#[openapi(
handlers(pet_api::get_pet_by_id),
components(Pet),
modifiers(&Foo),
security(
(),
("my_auth" = ["read:items", "edit:items"]),
("token_jwt" = [])
)
)]
struct ApiDoc;

#[test]
Expand Down
Loading

0 comments on commit f2c1489

Please sign in to comment.