When Pydantic V2 models are generated from an OpenAPI spec with discriminators, the generated Python code will have discriminators with original OpenAPI property names instead of the mapped (Python) property name (from the alias mapping file)
# generated by datamodel-codegen:
# filename: openapi.json
# timestamp: 2023-12-11T15:20:51+00:00
from __future__ import annotations
from typing import List, Union
from pydantic import BaseModel, Field, RootModel
from typing_extensions import Literal
class TypeA(BaseModel):
type: Literal['TypeA'] = Field('TypeA', alias='@type', title='@Type')
fu: int = Field(..., title='Fu')
class TypeB(BaseModel):
type: Literal['TypeB'] = Field('TypeB', alias='@type', title='@Type')
bar: str = Field(..., title='Bar')
class Data(RootModel[Union[TypeA, TypeB]]):
root: Union[TypeA, TypeB] = Field(..., discriminator='@type')
class Whatever(BaseModel):
type: Literal['SomeTypes'] = Field('Whatever', alias='@type', title='@Type')
data: List[Data] = Field(..., title='Data')
Expected behavior
The Field(..., discriminator='@type') in the generated Python code should have been Field(..., discriminator='type') eg without the @.
Version:
OS: MacOS 13.6.2 (arm64)
Python version: 3.11.7 (MacPorts version: port install python311)
Describe the bug
When Pydantic V2 models are generated from an OpenAPI spec with discriminators, the generated Python code will have discriminators with original OpenAPI property names instead of the mapped (Python) property name (from the alias mapping file)
To Reproduce
Example schema:
Alias mapping file
aliases.json
:Used commandline:
Result:
Expected behavior The
Field(..., discriminator='@type')
in the generated Python code should have beenField(..., discriminator='type')
eg without the@
.Version:
port install python311
)Additional context n/a