[BUG][aspnetcore] Objects in request body deserialized as base class instead of child classes
Created by: Kallb123
Description
When posting data, such as a list of animals, the objects get deserialized as the base Animal class, rather than the Cat or Dog that they should be. These same classes are used as part of other paths, when sending data to the client and they get deserialized just fine by the c# client.
I would expect the SaveData.Animals list to contain Cat objects and Dog objects, but in fact they are all returned as Animal classes.
openapi-generator version
1.0.18-4.3.1
OpenAPI declaration file content or url
openapi: 3.0.0
paths:
/saveData:
post:
description: Send all the data required
parameters:
- name: id
in: query
required: true
description: The id of the animal
schema:
type: string
format: uuid
example: 3fa85f64-5717-4562-b3fc-2c963f66afa6
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/SaveData'
responses:
200:
description: The animal
content:
application/json:
schema:
$ref: '#/components/schemas/Animal'
401:
$ref: '#/components/responses/Unauthorised'
400:
$ref: '#/components/responses/BadRequest'
components:
schemas:
SaveData:
type: object
required:
- animals
properties:
animals:
type: array
description: A list of animals
items:
$ref: '#/components/schemas/Animal'
Animal:
type: object
discriminator:
propertyName: className
required:
- "id"
- "name"
- "className"
properties:
id:
type: string
description: The unique ID of this object
format: uuid
example: 3fa85f64-5717-4562-b3fc-2c963f66afa6
name:
type: string
description: Gives a name to the animal
example: Bob
className:
type: string
description: Determines which child class this object is
example: Cat
Cat:
allOf:
- $ref: '#/components/schemas/Animal'
Dog:
allOf:
- $ref: '#/components/schemas/Animal'
- type: object
required:
- "breed"
properties:
breed:
type: string
description: The breed of dog
example: Terrier
nullable: true
Generation Details
openapi-generator generate -g csharp --additional-properties=optionalPojectFile=false --additional-properties=targetFramework=v3.5 -i apiSpec.yaml -o csharp
Steps to reproduce
- Post the appropriate data, a list of Cats and Dogs.
- Read the list on the backend, see that it is only Animals, so child properties are lost.