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

[python] Nullable keyword not taken into account when building a client #14181

Closed
4 of 6 tasks
volnt opened this issue Dec 5, 2022 · 3 comments
Closed
4 of 6 tasks

[python] Nullable keyword not taken into account when building a client #14181

volnt opened this issue Dec 5, 2022 · 3 comments

Comments

@volnt
Copy link

volnt commented Dec 5, 2022

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I'm generating a python-client based on the following OpenApi spec:

components:
  schemas:
    Pet:
      properties:
        name:
          type: string
          title: Name
        age:
          type: integer
          title: Age
      title: Pet
      type: object
    ListPetsResponse:
      properties:
        oldestPet:
          oneOf:
            - $ref: '#/components/schemas/Pet'
          title: Oldest Pet
          nullable: true
        pets:
          items:
            $ref: '#/components/schemas/Pet'
          nullable: true
          title: Pets
          type: array
      title: ListPetsResponse
      type: object

openapi: 3.0.2
info:
  title: Pet Store
  version: 0.0.1
paths:
  /api/v1/pets:
    get:
      operationId: list_pets
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ListPetsResponse'
          description: Successful Response
      summary: List Pets

This OpenAPI spec defines the response property oldestPet like that:

 oldestPet:
          oneOf:
            - $ref: '#/components/schemas/Pet'
          nullable: true

So oldestPet should be either a Pet object or null.

The issue is that the generated client defines the property like that:

            class oldestPet(schemas.ComposedSchema):           
                class MetaOapg:  
                    @classmethod
                    @functools.lru_cache()
                    def one_of(cls):
                        return [
                            Pet,
                        ]

Where I would expect this instead:

            class oldestPet(schemas.ComposedSchema):           
                class MetaOapg:  
                    @classmethod
                    @functools.lru_cache()
                    def one_of(cls):
                        return [
                            Pet, schemas.NoneBase,             # note the added `NoneBase`
                        ]
openapi-generator version

I'm using openapitools/openapi-generator-cli:v6.2.0.

I don't believe this is a regression but I haven't tested with previous versions.

Suggest a fix

I'd first like to confirm with others (especially @spacether) that this is indeed a bug, if that is confirmed I'll see how it can be fixed.

@spacether
Copy link
Contributor

spacether commented Dec 5, 2022

Hi there, this is a spec issue and the generator is behaving correctly.
nullable: true only adds type null when used adjacent to an explicit type definition.
Per the openapi spec, the value for nullable is described as:
A true value adds "null" to the allowed type specified by the type keyword, only if type is explicitly defined within the same Schema Object. Other Schema Object constraints retain their defined behavior, and therefore may disallow the use of null as a value. A false value leaves the specified or default type unmodified. The default value is false.
Your oldestPet schema has no type definition, so any type is allowed in. Adding nullable: True has no impact.

To get this to work as you want you can update your spec to one of these solutions and regenerate your client

  1. make the Pet schema nullable in the component and make oldestPet directly $ref Pet
  2. Update oldestPet to be:
anyOf:
- $ref: '#/components/schemas/Pet'
- type: object
  nullable: true

@spacether spacether changed the title [BUG][python] Nullable property not taken into account when building a client [python] Nullable property not taken into account when building a client Dec 5, 2022
@spacether spacether changed the title [python] Nullable property not taken into account when building a client [python] Nullable keyword not taken into account when building a client Dec 5, 2022
@volnt
Copy link
Author

volnt commented Dec 6, 2022

Oh! Thank you!

It's working now.

I'm amazed by the time it took for you to reply, kudos for that.

@volnt volnt closed this as completed Dec 6, 2022
@spacether
Copy link
Contributor

Welcome :) Glad to hear tat it is working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants