OpenAPITools / OpenAPITools/openapi-generator
[BUG][Python] Wrapper models for oneOf values include metadata fields in serialized responses
Nobody has claimed this yet.
- 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
When using the python-pydantic-v2 generator, defining a schema with additionalProperties whose values are oneOf produces an intermediate wrapper model for each dictionary value.
For the API definition below, the generator produces for followingApiResponse class:
class ApiResponse(BaseModel):
"""
ApiResponse
""" # noqa: E501
message: StrictStr
extra: Optional[Dict[str, ApiResponseExtraValue]] = Field(default=None, description="Map of extras")
__properties: ClassVar[List[str]] = ["message", "extra"]
where the ApiResponseExtraValue class is generated as:
APIRESPONSEEXTRAVALUE_ONE_OF_SCHEMAS = ["ExtraAlpha", "ExtraBeta"]
class ApiResponseExtraValue(BaseModel):
"""
ApiResponseExtraValue
"""
# data type: ExtraAlpha
oneof_schema_1_validator: Optional[ExtraAlpha] = None
# data type: ExtraBeta
oneof_schema_2_validator: Optional[ExtraBeta] = None
actual_instance: Optional[Union[ExtraAlpha, ExtraBeta]] = None
one_of_schemas: List[str] = Literal["ExtraAlpha", "ExtraBeta"]
model_config = {
"validate_assignment": True,
"protected_namespaces": (),
}
Returning a ApiResponse currently serializes all fields of the wrapper model, resulting in responses like:
{
"oneof_schema_1_validator": null,
"oneof_schema_2_validator": null,
"actual_instance": {
"type": "beta",
"variant": "excel",
"label": "Descargar",
"downloadId": "68b84d830ed1dd568216e27a"
},
"one_of_schemas": ["ExtraButton", "ExtraTable"]
}
The desired output is only the actual payload:
{
"type": "beta",
"variant": "excel",
"label": "Descargar",
"downloadId": "68b84d830ed1dd568216e27a"
}
The generator creates a wrapper model for the extra property which was defined with oneOf value.
Wrapper fields like oneof_schema_1_validator, oneof_schema_2_validator, and one_of_schemas are metadata for the generator, not real API data. The generated code includes these fields in instance serialization, so FastAPI returns them in responses.
openapi-generator version
openapi-generator v7.15.0 (latest at the moment)
generator: python-fastapi (pydantic v2)
pydantic version: 2.11.7 (latest at the moment)
fastapi version: 0.116.1 (latest at the moment)
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: Literal Bug Example
version: 1.0.0
paths: {}
components:
schemas:
ApiResponse:
type: object
additionalProperties: false
properties:
message:
type: string
example: "Hello world"
extra:
description: "Map of extras"
type: object
additionalProperties:
oneOf:
- $ref: '#/components/schemas/ExtraAlpha'
- $ref: '#/components/schemas/ExtraBeta'
required:
- message
ExtraAlpha:
type: object
required: [type, foo]
properties:
type:
type: string
enum: ["alpha"]
foo:
type: string
ExtraBeta:
type: object
required: [type, variant, label, url]
properties:
type:
type: string
enum: ["beta"]
variant:
type: string
enum: ["excel"]
label:
type: string
maxLength: 40
downloadId:
type: string
maxLength: 50
Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix
Manually converting metadata fields to ClassVar can remove them from the serialized response:
class ApiResponseExtraValue(BaseModel):
"""
ApiResponseExtraValue
"""
# data type: ExtraAlpha
oneof_schema_1_validator: ClassVar[Optional[ExtraAlpha]] = None
# data type: ExtraBeta
oneof_schema_2_validator: ClassVar[Optional[ExtraBeta]] = None
actual_instance: Optional[Union[ExtraAlpha, ExtraBeta]] = None
one_of_schemas: ClassVar[List[str]] = Literal["ExtraAlpha", "ExtraBeta"]
model_config = {
"validate_assignment": True,
"protected_namespaces": (),
}
However, the actual_instance wrapper is still serialized:
{
"actual_instance": {
"type": "beta",
"variant": "excel",
"label": "Descargar",
"downloadId": "68b943b1bb9793a88d289545"
}
}
when the response should be
{
"type": "download",
"variant": "excel",
"label": "Descargar",
"downloadId": "68b84d830ed1dd568216e27a"
}
The suggested fix is:
- Only serialize the actual payload (
actual_instance) when returning the model. - Treat the wrapper fields (
oneof_schema_1_validator,oneof_schema_2_validator, andone_of_schemas) as class-only metadata or remove them entirely.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the supplied OpenAPI 3.0.3 declaration and generate the python-fastapi output with Pydantic v2. Inspect the generated ApiResponseExtraValue wrapper and reproduce serialization of the additionalProperties oneOf value. Done means the response contains only the actual payload, without wrapper metadata fields.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100