Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
andzno1 opened this issue Sep 5, 2024 · 0 comments
Open

Comments

@andzno1
Copy link

andzno1 commented Sep 5, 2024

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant