Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #7377
Closed
Open
Issue created Sep 09, 2020 by Administrator@rootContributor

[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
  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.
Assignee
Assign to
Time tracking