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

Support for nullable data types (Option<>) in poem-openapi #701

Open
ettom opened this issue Nov 30, 2023 · 2 comments
Open

Support for nullable data types (Option<>) in poem-openapi #701

ettom opened this issue Nov 30, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@ettom
Copy link

ettom commented Nov 30, 2023

Description of the feature

It would be nice if a type being wrapped in Option<> would be shown as nullable in the OpenAPI document. See https://stackoverflow.com/questions/48111459/how-to-define-a-property-that-can-be-string-or-null-in-openapi-swagger

Code example

#[oai(path = "/hello", method = "get")]
async fn hello(&self) -> Json<Option<String>> {
    Json(None)
}

would generate

paths:
  /hello:
    get:
      responses:
        '200':
          description: ''
          content:
            application/json; charset=utf-8:
              schema:
                type: string
                nullable: true

Additionally,

#[derive(Object)]
struct Foo {
    x: Option<i32>,
}

#[OpenApi]
impl Api {
    #[oai(path = "/hello", method = "post")]
    async fn hello(&self, _p: Json<Foo>) {}
}

would generate

paths:
  /hello:
    post:
      requestBody:
        content:
          application/json; charset=utf-8:
            schema:
              $ref: '#/components/schemas/Foo'
        required: true
      responses:
        '200':
          description: ''
components:
  schemas:
    Foo:
      type: object
      properties:
        x:
          type: integer
          format: int32
          nullable: true

Thanks for the amazing work!

@ettom ettom added the enhancement New feature or request label Nov 30, 2023
@moritzruth
Copy link

By default, serde serializes Option::None as null. It does not skip the field altogether.

This means that objects serialized by serde actually do not adhere to the schema generated by poem-openapi in case there are Option::None values.

@kidqueb
Copy link

kidqueb commented Oct 9, 2024

By default, serde serializes Option::None as null. It does not skip the field altogether.

Isn't this what is happening in the example and PR? A nullable field, albeit manually specified in the PR, would make None become null

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

3 participants