Skip to content

Commit

Permalink
Implemented nullable fields for openapi spec generation (#865)
Browse files Browse the repository at this point in the history
* Implemented nullable functionality for documentation generation

* Fixed case where the field is all_off and nullable is not at the top level, but one of the variants
  • Loading branch information
skytz authored Nov 25, 2024
1 parent 77bb93c commit b16f72e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 6 additions & 0 deletions poem-openapi-derive/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct ObjectField {
#[darling(default)]
write_only: bool,
#[darling(default)]
nullable: bool,
#[darling(default)]
read_only: bool,
#[darling(default)]
validator: Option<Validators>,
Expand Down Expand Up @@ -69,6 +71,8 @@ struct ObjectArgs {
#[darling(default)]
write_only_all: bool,
#[darling(default)]
nullable_all: bool,
#[darling(default)]
deny_unknown_fields: bool,
#[darling(default)]
example: bool,
Expand Down Expand Up @@ -115,6 +119,7 @@ pub(crate) fn generate(args: DeriveInput) -> GeneratorResult<TokenStream> {
let field_ty = &field.ty;
let read_only = args.read_only_all || field.read_only;
let write_only = args.write_only_all || field.write_only;
let nullable = args.nullable_all || field.nullable;
let skip_serializing_if_is_none =
field.skip_serializing_if_is_none || args.skip_serializing_if_is_none;
let skip_serializing_if_is_empty =
Expand Down Expand Up @@ -287,6 +292,7 @@ pub(crate) fn generate(args: DeriveInput) -> GeneratorResult<TokenStream> {
let patch_schema = {
let mut schema = #crate_name::registry::MetaSchema::ANY;
schema.default = #field_meta_default;
schema.nullable = #nullable;
schema.read_only = #read_only;
schema.write_only = #write_only;

Expand Down
9 changes: 7 additions & 2 deletions poem-openapi/src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pub struct MetaSchema {
pub enum_items: Vec<Value>,
#[serde(skip_serializing_if = "is_false")]
pub deprecated: bool,
#[serde(skip_serializing_if = "is_false")]
pub nullable: bool,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub any_of: Vec<MetaSchemaRef>,
#[serde(skip_serializing_if = "Vec::is_empty")]
Expand Down Expand Up @@ -150,6 +152,7 @@ impl MetaSchema {
discriminator: None,
read_only: false,
write_only: false,
nullable: false,
example: None,
multiple_of: None,
maximum: None,
Expand Down Expand Up @@ -189,6 +192,7 @@ impl MetaSchema {
default,
read_only,
write_only,
nullable,
title,
description,
external_docs,
Expand All @@ -213,6 +217,7 @@ impl MetaSchema {
) -> Self {
self.read_only |= read_only;
self.write_only |= write_only;
self.nullable |= nullable;

macro_rules! merge_optional {
($($name:ident),*) => {
Expand Down Expand Up @@ -333,9 +338,9 @@ impl MetaSchemaRef {
MetaSchemaRef::Inline(Box::new(MetaSchema {
all_of: vec![
MetaSchemaRef::Reference(name),
MetaSchemaRef::Inline(Box::new(other)),
MetaSchemaRef::Inline(Box::new(other.clone())),
],
..MetaSchema::ANY
..other
}))
}
}
Expand Down

0 comments on commit b16f72e

Please sign in to comment.