OpenAPITools / OpenAPITools/openapi-generator

[openapi spec 3.1.0][python-flask generator] Nullable properties not propagated to child models

Open
#19,619 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

Currently, we use openapi spec 3.0.3 with openapi-generator version 5.4.0 and we are planning to migrate to openapi spec 3.1.0 with the latest generator version.

While migrating to 3.1.0 I see that nullable is not supported anymore and we use the isNullable value to set our generated model instance variable to Optional as follows:

{{#isNullable}}Optional[{{/isNullable}}{{#isDateTime}}Union[datetime, DateTimeProxy]{{/isDateTime}}{{^isDateTime}}{{dataType}}{{/isDateTime}}{{#isNullable}}]{{/isNullable}}

I noticed that with the new version of the generator and openapi spec 3.1.0 when we set a property as "null" this constraint is not inherited by the child schema in the model generated. Here is a simple example:

Openapi spec 3.1.0:

openapi: 3.1.0
paths:
  /users/{userId}:
    get:
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UserResponse'

components:
  schemas:
    BaseUser:
      type: object
      properties:
        id:
          type:
            - string
            - 'null'
        username:
          type: string

    UserResponse:
      type: object
      allOf:
        - $ref: '#/components/schemas/BaseUser'
        - type: object
          properties:
            email:
              type: string
              description: The email of the user

Model generated:
base_user.py

class BaseUser(TypedDict):
    id: Optional[str]
    username: str

user_response.py

class UserResponse(TypedDict):
    id: str
    username: str
    email: str

Notice how the id : Optional[str] is not the same as parent in the child schema.
This worked as expected with 3.0.3 and the new version of the generator as well. So makes me wonder if the 3.1.0 spec isNullable property is propagated to the child schema.

Please let me know if there is a way I can resolve this as we want to utilize the great features in 3.1.0 without breaking our client code.

openapi-generator version

7.8.0

OpenAPI declaration file content or url
Command line used for generation

openapi-generator-cli generate --input-spec spec/bundle.yaml --config openapi-generator.yaml --output ../..

Note: the openapi-generator.yaml config file is straightforward and no options are enabled in it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied OpenAPI 3.1.0 schema and the openapi-generator-cli command using the python-flask generator. Compare the generated base_user.py and user_response.py models, focusing on the nullable id property across allOf inheritance. Done means the child model preserves the parent property's Optional typing without breaking the existing 3.0.3 behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.