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.79k stars 306 forks source link

[BUG]: All of the fields have a None default #2040

Open Paillat-dev opened 4 months ago

Paillat-dev commented 4 months ago

Describe the bug For some reason, all of my fields are set as nullable, using strict-nullable dosen't change anything. This migh well be my error, but after spending much time trying endless combinations of settings, I resorted to opening a bug report. To Reproduce

Example schema:

openapi: 3.0.0
components:
  schemas:
    ParentCompany:
      description: A parent company record
      type: object
      properties:
        id:
          type: integer
          nullable: true
        name:
          type: string

Used commandline:

$ datamodel-codegen --input ./src/tvdb/swagger.yml --input-file-type openapi --output ./src/tvdb/generated_models.py --output-model-type pydantic_v2.BaseModel

pyprohect.toml:

[tool.datamodel-codegen]
field-constraints = true
snake-case-field = true
target-python-version = "3.12"
use-default-kwarg = true
use-exact-imports = true
use-field-description = true
use-union-operator = true
reuse-model = true
output-model-type = "pydantic_v2.BaseModel"
custom-file-header = "# ruff: noqa: D101 # Allow missing docstrings"
field-include-all-keys = true
strict-nullable = false # or true, dosen't change anything

Expected output

# ruff: noqa: D101 # Allow missing docstrings

from __future__ import annotations

from pydantic import BaseModel

class ParentCompany(BaseModel):
    id: int | None = None
    name: str

Actual output

# ruff: noqa: D101 # Allow missing docstrings

from __future__ import annotations

from pydantic import BaseModel

class ParentCompany(BaseModel):
    id: int | None = None
    name: str | None = None

Version:

whisper-bye commented 4 weeks ago

+1