Skip to content

Using const inside anyOf combined with minimum and maximum not working #2085

@andzno1

Description

@andzno1

Describe the bug
Using const combined with minimum and maximum inside anyOf in a JSON schema does not yield the desired result when generating a model.

To Reproduce

Example schema:

{
  "type": "object",
  "properties": {
    "SomeValue": {
      "type": "integer",
      "anyOf": [
        {
          "const": 500000
        },
        {
          "minimum": 0,
          "maximum": 65534
        }
      ]
    }
  }
}

Used commandline:

datamodel-codegen --input schema.json --input-file-type jsonschema --output model.py --output-model-type pydantic_v2.BaseModel --use-annotated

Expected behavior
The generated model shall limit the allowed values to a range form 0 to 65534 and 500000:

from __future__ import annotations

from typing import Any, Literal, Optional, Union

from pydantic import BaseModel, Field, RootModel
from typing_extensions import Annotated


class SomeValue(RootModel[Any]):
    root: Annotated[int, Field(ge=0, le=65534)]


class Model(BaseModel):
    SomeValue: Optional[Union[Literal[500000], SomeValue]] = None

Actual behavior
The generated model limits the allowed values to a range form 0 to 65534 and any integer:

from __future__ import annotations

from typing import Optional, Union

from pydantic import BaseModel, Field, RootModel
from typing_extensions import Annotated


class SomeValue(RootModel[int]):
    root: Annotated[int, Field(ge=0, le=65534)]


class Model(BaseModel):
    SomeValue: Optional[Union[int, SomeValue]] = None

Version:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions