diff --git a/utoipa-gen/tests/schema_derive_test.rs b/utoipa-gen/tests/schema_derive_test.rs index 102c8d3e..d72eaece 100644 --- a/utoipa-gen/tests/schema_derive_test.rs +++ b/utoipa-gen/tests/schema_derive_test.rs @@ -3226,6 +3226,24 @@ fn derive_map_with_property_names() { assert_json_snapshot!(value); } +#[test] +fn derive_map_with_optional_ref_value() { + #![allow(unused)] + + #[derive(ToSchema)] + struct NewString(String); + + let value = api_doc! { + struct Map { + map: std::collections::BTreeMap>, + #[schema(inline)] + inline_map: std::collections::BTreeMap>, + } + }; + + assert_json_snapshot!(value); +} + #[test] fn derive_schema_with_ignored_field() { #![allow(unused)] diff --git a/utoipa-gen/tests/snapshots/schema_derive_test__derive_map_with_optional_ref_value.snap b/utoipa-gen/tests/snapshots/schema_derive_test__derive_map_with_optional_ref_value.snap new file mode 100644 index 00000000..92ff37a6 --- /dev/null +++ b/utoipa-gen/tests/snapshots/schema_derive_test__derive_map_with_optional_ref_value.snap @@ -0,0 +1,45 @@ +--- +source: utoipa-gen/tests/schema_derive_test.rs +expression: value +--- +{ + "properties": { + "inline_map": { + "additionalProperties": { + "oneOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + }, + "propertyNames": { + "type": "string" + }, + "type": "object" + }, + "map": { + "additionalProperties": { + "oneOf": [ + { + "$ref": "#/components/schemas/NewString" + }, + { + "type": "null" + } + ] + }, + "propertyNames": { + "type": "string" + }, + "type": "object" + } + }, + "required": [ + "map", + "inline_map" + ], + "type": "object" +} diff --git a/utoipa/src/openapi/schema.rs b/utoipa/src/openapi/schema.rs index 7c491b3f..769071f5 100644 --- a/utoipa/src/openapi/schema.rs +++ b/utoipa/src/openapi/schema.rs @@ -1669,6 +1669,18 @@ impl From for AdditionalProperties { } } +impl From for AdditionalProperties { + fn from(value: OneOfBuilder) -> Self { + Self::RefOr(RefOr::T(Schema::OneOf(value.build()))) + } +} + +impl From for AdditionalProperties { + fn from(value: AnyOfBuilder) -> Self { + Self::RefOr(RefOr::T(Schema::AnyOf(value.build()))) + } +} + builder! { RefBuilder;