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.75k stars 301 forks source link

JSON Schema: oneOf with const #1925

Open roquie opened 6 months ago

roquie commented 6 months ago

Describe the bug Does not generate an enumeration for oneOf + cost. For example, autocomplete in Jetbrains IDE works fine for this case.

To Reproduce

  1. create a schema
  2. generate
  3. get an incorrect result generation

Example schema:

# definition

  nodeJsModeEnum:
    title: NodeJS mode
    type: string
    description: |
       A long description here.
    default: npm
    oneOf:
      - title: npm
        const: npm
      - title: yarn
        const: yarn
      - title: npm ci
        const: npm_ci

# usage
        properties:
          mode:
            $ref: "#/definitions/nodejsModeEnum"

Used commandline:

pdm run datamodel-codegen --input config/schema/cd.schema.yaml --input-file-type jsonschema --output src/config/cd_model.py --output-model-type pydantic_v2.BaseModel

# pyproject.toml options
#[tool.datamodel-codegen]
#field-constraints = true
#snake-case-field = true
#strip-default-none = false
#target-python-version = "3.11"

Expected behavior

# expects
class NodeJsModeEnum(Enum):
    npm = 'npm'
    yarn = 'yarn'
    npm_ci = 'npm_ci'

# actual
class NodeJsModeEnum(RootModel[str]):
    root: str = Field(
        ..., description='...'
    )

Version:

REZUCE commented 1 month ago

How to fix with problem ? @roquie