OpenAPITools / OpenAPITools/openapi-generator

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

Open
#22,348 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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): https://github.com/pydantic/pydantic/issues/10220#issuecomment-2305909989

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Regenerate the Python client with the command and OpenAPI declaration linked in the issue, then inspect the generated openapi_client/models/multi_value_value.py validator. Compare handling of the integer and float schemas, and verify that constructing MultiValueValue(42) succeeds without the multiple-match validation error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.