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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe improve yaml formatting of nullable fields #1186

Open
LHolten opened this issue Nov 5, 2024 · 1 comment
Open

Maybe improve yaml formatting of nullable fields #1186

LHolten opened this issue Nov 5, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@LHolten
Copy link

LHolten commented Nov 5, 2024

This looks like a cool project! I have one question about the generated yaml with nullable fields:

This struct has three nullable fields:

#[derive(ToSchema)]
struct Dummy {
    #[schema(inline)]
    attention_line: Option<AttentionLine>,
    external_reference: Option<ExternalReference>,
    external_updated_at: Option<DateTime<Utc>>,
}

It generates openapi yaml that looks like this:

attention_line:
  oneOf:
  - type: 'null'
  - type: string
    title: Attention line
    description: The person or department for who the letter is intended.
    examples:
    - Procurement department
    maxLength: 300
    minLength: 1
external_reference:
  oneOf:
  - type: 'null'
  - $ref: '#/components/schemas/external_reference'
external_updated_at:
  type:
  - string
  - 'null'
  format: date-time

As you can see, for the DateTime known format it does not need to use oneOf.
This is my prefered format for nullable fields, e.g. it generates type: [ string, 'null' ] and then the format is outside that list.

AttentionLine and ExternalReference are both just some string newtype with extra requirements on the string length.
Independent of whether these get inlined or not, they generate use of oneOf instead of the nice type: [ string, 'null' ].

Would it be possible to always generate type: [ string, 'null' ] instead of oneOf, or at least when the type is inlined?

@juhaku juhaku added the enhancement New feature or request label Nov 7, 2024
@juhaku
Copy link
Owner

juhaku commented Nov 7, 2024

Would it be possible to always generate type: [ string, 'null' ] instead of oneOf, or at least when the type is inlined?

If the type is known to be a primitive type, it gets the type as type: [ type, 'null' ] but as of now when ToSchema macro executes for Dummy it does not know what the actual type is when it sees field attention_line: Option<AttentionLine> or external_reference: Option<ExternalReference>. This is also why it treats it as a generic type and creates a oneOf composition type to describe a nullable type.

Perhaps there are ways to make it smarter but that requires more effort from the schema generation and it is not super simple. This would need some experimenting around possibilities and mean time let the thought age well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants