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

Model property not optional when referencing a nullable subschema #2244

Open
grant-zietsman opened this issue Dec 29, 2024 · 0 comments
Open

Comments

@grant-zietsman
Copy link

Describe the bug

Given an OpenAPI component schema configured with nullable: true, properties referencing this schema are not generated as optional.

To Reproduce

Example schema (OpenAPI):

openapi: 3.0.3
components:
  schemas:
    Parent:
      type: object
      required:
        - child
      properties:
        child:
          $ref: "#/components/schemas/Child"
    Child:
      nullable: true
      type: object
      required:
        - value
      properties:
        value:
          type: string

Used commandline:

datamodel-code-generator datamodel-codegen \
    --input ... \
    --input-file-type openapi \
    --output-model-type typing.TypedDict \
    --target-python-version 3.12 \
    --strict-nullable \
    --use-annotated \
    --use-field-description \
    --use-schema-description \
    --use-standard-collections \
    --use-union-operator \
    --disable-timestamp

Model output

# generated by datamodel-codegen:
#   filename:  openapi.yaml

from __future__ import annotations

from typing import TypedDict


class Child(TypedDict):
    value: str


class Parent(TypedDict):
    child: Child

Expected behavior
It is expected that the child property of the Parent model be optional.

Model output

# generated by datamodel-codegen:
#   filename:  openapi.yaml

from __future__ import annotations

from typing import TypedDict


class Child(TypedDict):
    value: str


class Parent(TypedDict):
    child: Child | None

Version:

  • OS: iOS 15.1.1
  • Python version: 3.12.4
  • datamodel-code-generator version: 0.26.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant