Skip to content

Commit

Permalink
Refactor and organize code2 (#11)
Browse files Browse the repository at this point in the history
* Refactor and organized code structure
  • Loading branch information
juhaku committed Jan 17, 2022
1 parent b7b37b4 commit a13f66a
Show file tree
Hide file tree
Showing 22 changed files with 892 additions and 727 deletions.
19 changes: 18 additions & 1 deletion tests/component_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ macro_rules! api_doc {

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

let json = serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -304,3 +304,20 @@ fn derive_struct_unnamed_field_vec_type_success() {
"items.format" = r#""int32""#, "Wrapper items format"
}
}

#[test]
#[ignore = "not supported yet!!!"]
fn derive_struct_nested_vec_success() {
let vecs = api_doc! {
struct VecTest {
vecs: Vec<Vec<String>>
}
};

println!("{:#?}", vecs);
// assert_value! {point=>
// "type" = r#""array""#, "Wrapper type"
// "items.type" = r#""integer""#, "Wrapper items type"
// "items.format" = r#""int32""#, "Wrapper items format"
// }
}
4 changes: 2 additions & 2 deletions tests/path_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro_rules! test_api_fn_doc {
( $handler:path, operation: $operation:expr, path: $path:literal ) => {{
use utoipa::OpenApi;
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], handlers = [$handler])]
#[openapi(handlers = [$handler])]
struct ApiDoc;

let doc = &serde_json::to_value(ApiDoc::openapi()).unwrap();
Expand Down Expand Up @@ -58,7 +58,7 @@ macro_rules! test_path_operation {
paste!{
use utoipa::OpenApi;
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], handlers = [
#[openapi(handlers = [
[<mod_ $name>]::test_operation
])]
struct ApiDoc;
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 @@ -30,7 +30,7 @@ mod mod_derive_path_actix {
#[test]
fn derive_path_one_value_actix_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -73,7 +73,7 @@ mod mod_derive_path_unnamed_regex_actix {
#[test]
fn derive_path_with_unnamed_regex_actix_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -116,7 +116,7 @@ mod mod_derive_path_named_regex_actix {
#[test]
fn derive_path_with_named_regex_actix_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -156,7 +156,7 @@ macro_rules! test_derive_path_operations {
#[test]
fn $name() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -31,7 +31,7 @@ mod derive_params_multiple_actix {
#[test]
fn derive_path_parameter_multiple_with_matching_names_and_types_actix_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -85,7 +85,7 @@ mod derive_parameters_multiple_no_matching_names_actix {
#[test]
fn derive_path_parameter_multiple_no_matching_names_acitx_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -131,7 +131,7 @@ mod derive_params_from_method_args_actix {
#[test]
fn derive_params_from_method_args_actix_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -29,7 +29,7 @@ mod derive_params_all_options {
#[test]
fn derive_path_parameters_with_all_options_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -70,7 +70,7 @@ mod derive_params_minimal {
#[test]
fn derive_path_parameters_minimal_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -112,7 +112,7 @@ mod derive_params_multiple {
#[test]
fn derive_path_parameter_multiple_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -165,7 +165,7 @@ mod mod_derive_parameters_all_types {
#[test]
fn derive_parameters_with_all_types() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -237,7 +237,7 @@ mod derive_params_without_args {
#[test]
fn derive_params_without_fn_args() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -14,7 +14,7 @@ macro_rules! api_doc {
( module: $module:expr ) => {{
use utoipa::OpenApi;
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], handlers = [$module::get_foo])]
#[openapi(handlers = [$module::get_foo])]
struct ApiDoc;

let doc = serde_json::to_value(&ApiDoc::openapi()).unwrap();
Expand Down
14 changes: 7 additions & 7 deletions tests/request_body_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test_fn! {
#[test]
fn derive_path_request_body_simple_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -52,7 +52,7 @@ test_fn! {
#[test]
fn derive_path_request_body_simple_array_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -75,7 +75,7 @@ test_fn! {
#[test]
fn derive_request_body_primitive_simple_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -98,7 +98,7 @@ test_fn! {
#[test]
fn derive_request_body_primitive_array_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -121,7 +121,7 @@ test_fn! {
#[test]
fn derive_request_body_complex_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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 @@ -144,7 +144,7 @@ test_fn! {
#[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])]
#[openapi(handlers = [derive_request_body_complex_required_explisit::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_primitive_array_success() {
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], 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
8 changes: 4 additions & 4 deletions tests/utoipa_gen_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![cfg(feature = "actix_extras")]
use actix_web::{get, web, HttpResponse, Responder};
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_json::json;
// use utoipa::openapi_spec;
use utoipa::OpenApi;
use utoipa::{Component, OpenApi};

#[derive(Deserialize)]
#[derive(Deserialize, Serialize, Component)]
struct Foo {
ids: Vec<i32>,
}
Expand Down Expand Up @@ -42,7 +42,7 @@ async fn foo_delete(web::Query(foo): web::Query<Foo>) -> impl Responder {
fn derive_openapi() {
// use crate::api::__path_foo_delete;
#[derive(OpenApi, Default)]
#[openapi(handler_files = [], handlers = [foo_delete])]
#[openapi(handlers = [foo_delete], components = [Foo])]
struct ApiDoc;

println!("{}", ApiDoc::openapi().to_pretty_json().unwrap());
Expand Down
Loading

0 comments on commit a13f66a

Please sign in to comment.