Skip to content
New issue

Have a question about this project?Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of serviceand privacy statement.We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented nullable fields for openapi spec generation #865

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implemented nullable functionality for documentation generation
  • Loading branch information
skytz committed Aug 14, 2024
commit5e893ef7d0118772117b115553cfbd774365d1cf
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
5 changes: 5 additions & 0 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