koxudaxi / datamodel-code-generator

Pydantic model and dataclasses.dataclass generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.
https://koxudaxi.github.io/datamodel-code-generator/
MIT License
2.73k stars 298 forks source link

AttributeError: 'NoneType' object has no attribute 'is_list' #2069

Open comic31 opened 2 months ago

comic31 commented 2 months ago

Describe the bug

Fail to generate python code with an array of $ref from an allof, an AttributeError has been raised by the lib.

If this is not a bug, please let me know if I'm doing something wrong.

To Reproduce

Example schema:

    PetOpts:
      type: string
    ProjectedPet:
      type: object
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
        opts:
          type: array
          items:
            $ref: "#/components/schemas/PetOpts"
    Pet:
      allOf:
        - $ref: "#/components/schemas/ProjectedPet"
      required:
          - id
          - name
          - opts

Used commandline:

datamodel-codegen --input api.yaml --output model.py --input-file-type openapi --use-annotated --collapse-root-models  --use-standard-collections --capitalize-enum-members  --target-python-version 3.10 --output-model-type pydantic_v2.BaseModel

Actual behavior

AttributeError: 'NoneType' object has no attribute 'is_list'

Expected behavior

class ProjectedPet(BaseModel):
    id: Optional[int] = None
    name: Optional[str] = None
    tag: Optional[str] = None
    opts: Optional[list[str]] = None

class Pet(ProjectedPet):
    id: int
    name: str
    opts: list[str]

Version:

Additional context

The traceback

Traceback (most recent call last):
  File "/Users/user/work/issue-datamodel-code-generator/.venv/lib/python3.11/site-packages/datamodel_code_generator/__main__.py", line 445, in main
    generate(
  File "/Users/user/work/issue-datamodel-code-generator/.venv/lib/python3.11/site-packages/datamodel_code_generator/__init__.py", line 473, in generate
    results = parser.parse()
              ^^^^^^^^^^^^^^
  File "/Users/user/work/issue-datamodel-code-generator/.venv/lib/python3.11/site-packages/datamodel_code_generator/parser/base.py", line 1366, in parse
    self.__collapse_root_models(
  File "/Users/user/work/issue-datamodel-code-generator/.venv/lib/python3.11/site-packages/datamodel_code_generator/parser/base.py", line 1015, in __collapse_root_models
    elif data_type.parent.is_list:
         ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'is_list'
zaphod72 commented 1 week ago
    Pet:
      allOf:
        - $ref: "#/components/schemas/ProjectedPet"
        - type: object
          required:
            - id
            - name
            - opts