OpenAPITools / OpenAPITools/openapi-generator

[BUG][aspnetcore] Objects in request body deserialized as base class instead of child classes

Open
#7,377 1 comment 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

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
  1. Post the appropriate data, a list of Cats and Dogs.
  2. Read the list on the backend, see that it is only Animals, so child properties are lost.

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

Reproduce the issue with the supplied OpenAPI declaration and the csharp generator command, then inspect the generated models and request-body deserialization path. Confirm how the Animal discriminator is handled for SaveData.Animals; done means posted Cat and Dog objects are materialized as their child classes without losing child properties.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.