Due to changes in how serialization of subclasses is handled in pydantic v2, generated models do not produce correct output when using model_dump or model_dump_json.
paths:
/pets:
patch:
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
discriminator:
propertyName: pet_type
responses:
'200':
description: Updated
components:
schemas:
Animals:
type: object
properties:
pet:
$ref: '#/components/schemas/Pet'
Pet:
type: object
required:
- pet_type
properties:
pet_type:
type: string
discriminator:
propertyName: pet_type
Dog: # "Dog" is a value for the pet_type property (the discriminator value)
allOf: # Combines the main `Pet` schema with `Dog`-specific properties
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Dog`
properties:
bark:
type: boolean
breed:
type: string
enum: [Dingo, Husky, Retriever, Shepherd]
Cat: # "Cat" is a value for the pet_type property (the discriminator value)
allOf: # Combines the main `Pet` schema with `Cat`-specific properties
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Cat`
properties:
hunts:
type: boolean
age:
type: integer
Expected behavior
To be able to serialize with all data from subclasses. One way to achieve this is to specify type of pet field in Animals with SerializeAsAny (docs). Another way is to specify all possible subclasses that pet field can take with union.
Describe the bug
Due to changes in how serialization of subclasses is handled in pydantic v2, generated models do not produce correct output when using model_dump or model_dump_json.
See issue https://github.com/pydantic/pydantic/issues/7326
To Reproduce
Example schema:
Used commandline:
Generated models:
and their usage:
Expected behavior To be able to serialize with all data from subclasses. One way to achieve this is to specify type of pet field in Animals with SerializeAsAny (docs). Another way is to specify all possible subclasses that pet field can take with union.
Version: