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:
Describe the bug
Using
constcombined withminimumandmaximuminsideanyOfin 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:
Expected behavior
The generated model shall limit the allowed values to a range form
0to65534and500000:Actual behavior
The generated model limits the allowed values to a range form
0to65534and any integer:Version:
python:3(https://hub.docker.com/_/python)