Skip to content

Commit

Permalink
Add support for nullable schema map items (juhaku#1032)
Browse files Browse the repository at this point in the history
Add support for nullable schemas in maps like `HashMap` and `BTreeMap`.

Resolves juhaku#897
  • Loading branch information
juhaku authored Sep 7, 2024
1 parent ed255a1 commit 5341dd0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
26 changes: 26 additions & 0 deletions utoipa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,18 @@ impl<'__s, K: PartialSchema, V: ToSchema<'__s>> PartialSchema for Option<BTreeMa
}
}

#[cfg(feature = "macros")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "macros")))]
impl<'__s, K: PartialSchema, V: ToSchema<'__s>> PartialSchema for BTreeMap<K, Option<V>> {
fn schema() -> openapi::RefOr<openapi::schema::Schema> {
schema!(
#[inline]
BTreeMap<K, Option<V>>
)
.into()
}
}

#[cfg(feature = "macros")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "macros")))]
impl<'__s, K: PartialSchema, V: ToSchema<'__s>> PartialSchema for std::collections::HashMap<K, V> {
Expand All @@ -680,6 +692,20 @@ impl<'__s, K: PartialSchema, V: ToSchema<'__s>> PartialSchema
}
}

#[cfg(feature = "macros")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "macros")))]
impl<'__s, K: PartialSchema, V: ToSchema<'__s>> PartialSchema
for std::collections::HashMap<K, Option<V>>
{
fn schema() -> openapi::RefOr<openapi::schema::Schema> {
schema!(
#[inline]
std::collections::HashMap<K, Option<V>>
)
.into()
}
}

/// Trait for implementing OpenAPI PathItem object with path.
///
/// This trait is implemented via [`#[utoipa::path(...)]`][derive] attribute macro and there
Expand Down
6 changes: 6 additions & 0 deletions utoipa/src/openapi/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,12 @@ impl From<Schema> for AdditionalProperties<Schema> {
}
}

impl From<AllOfBuilder> for AdditionalProperties<Schema> {
fn from(value: AllOfBuilder) -> Self {
Self::RefOr(RefOr::T(Schema::AllOf(value.build())))
}
}

builder! {
RefBuilder;

Expand Down

0 comments on commit 5341dd0

Please sign in to comment.