datamodel-code-generator
datamodel-code-generator copied to clipboard
Using `const` inside `anyOf` combined with `minimum` and `maximum` not working
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:
- OS: Docker container
python:3(https://hub.docker.com/_/python) - Python version: 3.11.2
- datamodel-code-generator version: 0.26.0