From 6c89f81e9def4f3ae9717db87d0aa05b81fb7d8a Mon Sep 17 00:00:00 2001 From: John Vandenberg Date: Mon, 17 Apr 2023 14:39:45 +0800 Subject: [PATCH] Add docs and tests for aliases (#587) Add docs and tests for aliases with `value_type`. --- utoipa-gen/src/lib.rs | 6 +++--- .../tests/path_derive_auto_types_actix.rs | 2 +- utoipa-gen/tests/schema_derive_test.rs | 19 +++++++++++++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/utoipa-gen/src/lib.rs b/utoipa-gen/src/lib.rs index 56160da1..7ed7a7ab 100644 --- a/utoipa-gen/src/lib.rs +++ b/utoipa-gen/src/lib.rs @@ -125,7 +125,7 @@ use self::{ /// This is useful in cases where the default type does not correspond to the actual type e.g. when /// any third-party types are used which are not [`ToSchema`][to_schema]s nor [`primitive` types][primitive]. /// The value can be any Rust type what normally could be used to serialize to JSON or either virtual type _`Object`_ -/// or _`Value`. +/// or _`Value`_, or an alias defined using `#[aliases(..)]`. /// _`Object`_ will be rendered as generic OpenAPI object _(`type: object`)_. /// _`Value`_ will be rendered as any OpenAPI value (i.e. no `type` restriction). /// * `title = ...` Literal string value. Can be used to define title for struct in OpenAPI @@ -149,7 +149,7 @@ use self::{ /// This is useful in cases where the default type does not correspond to the actual type e.g. when /// any third-party types are used which are not [`ToSchema`][to_schema]s nor [`primitive` types][primitive]. /// The value can be any Rust type what normally could be used to serialize to JSON, or either virtual type _`Object`_ -/// or _`Value`. +/// or _`Value`_, or an alias defined using `#[aliases(..)]`. /// _`Object`_ will be rendered as generic OpenAPI object _(`type: object`)_. /// _`Value`_ will be rendered as any OpenAPI value (i.e. no `type` restriction). /// * `inline` If the type of this field implements [`ToSchema`][to_schema], then the schema definition @@ -1650,7 +1650,7 @@ pub fn openapi(input: TokenStream) -> TokenStream { /// This is useful in cases where the default type does not correspond to the actual type e.g. when /// any third-party types are used which are not [`ToSchema`][to_schema]s nor [`primitive` types][primitive]. /// The value can be any Rust type what normally could be used to serialize to JSON, or either virtual type _`Object`_ -/// or _`Value`. +/// or _`Value`_, or an alias defined using `#[aliases(..)]`. /// _`Object`_ will be rendered as generic OpenAPI object _(`type: object`)_. /// _`Value`_ will be rendered as any OpenAPI value (i.e. no `type` restriction). /// diff --git a/utoipa-gen/tests/path_derive_auto_types_actix.rs b/utoipa-gen/tests/path_derive_auto_types_actix.rs index dcf0f63b..368f65df 100644 --- a/utoipa-gen/tests/path_derive_auto_types_actix.rs +++ b/utoipa-gen/tests/path_derive_auto_types_actix.rs @@ -9,7 +9,7 @@ use actix_web::{get, HttpResponse, Responder, ResponseError}; use assert_json_diff::assert_json_eq; #[test] -fn path_operation_auto_types_ressponses() { +fn path_operation_auto_types_responses() { /// Test item to to return #[derive(serde::Serialize, serde::Deserialize, utoipa::ToSchema)] struct Item<'s> { diff --git a/utoipa-gen/tests/schema_derive_test.rs b/utoipa-gen/tests/schema_derive_test.rs index 3b4b6b00..31d26fac 100644 --- a/utoipa-gen/tests/schema_derive_test.rs +++ b/utoipa-gen/tests/schema_derive_test.rs @@ -3178,7 +3178,7 @@ fn derive_complex_enum_as() { #[test] fn derive_component_with_primitive_aliases() { #[derive(Debug, OpenApi)] - #[openapi(components(schemas(BarString, BarInt)))] + #[openapi(components(schemas(BarString, BarInt, Foo)))] struct ApiDoc; #[derive(ToSchema)] @@ -3187,6 +3187,12 @@ fn derive_component_with_primitive_aliases() { #[allow(dead_code)] bar: R, } + #[derive(ToSchema)] + struct Foo { + #[allow(dead_code)] + #[schema(value_type=BarString)] + baz: Bar, + } let doc = ApiDoc::openapi(); let doc_value = &serde_json::to_value(doc).unwrap(); @@ -3214,9 +3220,18 @@ fn derive_component_with_primitive_aliases() { }, "type": "object", "required": ["bar"] + }, + "Foo": { + "properties": { + "baz": { + "$ref": "#/components/schemas/BarString", + } + }, + "type": "object", + "required": ["baz"] } }) - ) + ); } #[test]