Skip to content

[BUG][Python] oneOf-Model does not accept int if float is also allowed #22348

@zeratul2099

Description

@zeratul2099

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The Python-Codegenerator produces oneOf-models with number- and integer-types that fail validation of integers

openapi-generator version
  • 7.16.0
  • 7.17.0
  • 7.18.0-20251113.055138-36
OpenAPI declaration file content or url

https://gist.github.com/zeratul2099/aef06f45e5a7f47216b6e23d59d04da5

Generation Details
java -jar openapi-generator-cli-7.18.0-20251113.055138-36.jar generate -g python -i multi_value_spec.yaml -o test
Steps to reproduce
from openapi_client.models.multi_value_value import MultiValueValue

v = MultiValueValue(42)

Actual output:

ValidationError: 1 validation error for MultiValueValue
actual_instance
  Value error, Multiple matches found when setting `actual_instance` in MultiValueValue with oneOf schemas: bool, float, int, str. Details: 1 validation error for MultiValueValue
oneof_schema_3_validator
  Input should be a valid boolean [type=bool_type, input_value=42, input_type=int]
    For further information visit https://errors.pydantic.dev/2.11/v/bool_type, 1 validation error for MultiValueValue
oneof_schema_4_validator
  Input should be a valid string [type=string_type, input_value=42, input_type=int]
    For further information visit https://errors.pydantic.dev/2.11/v/string_type [type=value_error, input_value=42, input_type=int]
    For further information visit https://errors.pydantic.dev/2.11/v/value_error

Expected output:

v is instantiated

Related issues/PRs
Suggest a fix

The generated model has this validation method:

class MultiValueValue(BaseModel):
    # data type: float
    oneof_schema_1_validator: Optional[StrictFloat] = None
    # data type: int
    oneof_schema_2_validator: Optional[StrictInt] = None
    # data type: bool
    oneof_schema_3_validator: Optional[StrictBool] = None
    # data type: str
    oneof_schema_4_validator: Optional[StrictStr] = None
    actual_instance: Optional[Union[bool, float, int, str]] = None
    one_of_schemas: Set[str] = { "bool", "float", "int", "str" }

    ...

    @field_validator('actual_instance')
    def actual_instance_must_validate_oneof(cls, v):
        instance = MultiValueValue.model_construct()
        error_messages = []
        match = 0
        # validate data type: float
        try:
            instance.oneof_schema_1_validator = v
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # validate data type: int
        try:
            instance.oneof_schema_2_validator = v
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # validate data type: bool
        try:
            instance.oneof_schema_3_validator = v
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # validate data type: str
        try:
            instance.oneof_schema_4_validator = v
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        if match > 1:
            # more than 1 match
            raise ValueError("Multiple matches found when setting `actual_instance` in MultiValueValue with oneOf schemas: bool, float, int, str. Details: " + ", ".join(error_messages))

This leads to above error due 42 validates successfully with StrictFloat as well as StrictInt (more than 1 match). The matching of int to StrictFloat seems to be intended by pydantic (and python itself): pydantic/pydantic#10220 (comment)

This problem occurs with mapNumberTo=StrictFloat, mapNumberTo=float and the default Union[StrictFloat, StrictInt]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions