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:
python:3
(https://hub.docker.com/_/python)Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too