swagger-api / swagger-api/swagger-codegen

Is polymorphism supported in code generation?

Open
#3,904 7 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature: Composition / Inheritance General: Question
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

I've defined a POST method using polymorphism along the lines of the spec example at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#models-with-polymorphism-support . My real example is more complex but here's a simple alternative based on the spec example:

swagger: '2.0'
info:
  version: 1.0.0
  title: Pet Store Test
  description: Service for adding pets to a pet store.
basePath: /petstore
schemes:
  - http
paths:
  /pets:
    post:
      summary: Add a pet.
      description: Adds a new pet.
      consumes:
        - application/json
      parameters:
        - name: pet
          in: body
          description: The pet you want to add
          required: true
          schema:
            $ref: "#/definitions/Pet"
      responses:
        201:
          description: Pet added successfully
          headers:
            location:
              description: URL of the build request.
              type: string

definitions:
  Pet:
    type: object
    discriminator: petType
    properties:
      name:
        type: string
      petType:
        type: string
    required:
    - name
    - petType
  Cat:
    description: A representation of a cat
    allOf:
    - $ref: '#/definitions/Pet'
    - type: object
      properties:
        huntingSkill:
          type: string
          description: The measured skill for hunting
          default: lazy
          enum:
          - clueless
          - lazy
          - adventurous
          - aggressive
      required:
      - huntingSkill
  Dog:
    description: A representation of a dog
    allOf:
    - $ref: '#/definitions/Pet'
    - type: object
      properties:
        packSize:
          type: integer
          format: int32
          description: the size of the pack the dog is from
          default: 0
          minimum: 0
      required:
      - packSize

I can generate the Spring server-side stubs for this yaml using the Swagger Editor. But when I POST json requests of a Cat or a Dog only the base part is detected by the server-side code - i.e., name and petType in this case. All the derived data is lost. I was hoping to cast the retrieved Pet to a Cat or Dog based on the value of petType but that fails at runtime with a ClassCastException.

Actually I confess I haven't tried testing this with the above yaml but what I say is true for my yaml. I just wanted to use the above as a simple analogy to ask the question of whether polymorphism is currently supported by swagger-codegen? If not then I'm going to have to completely rewrite my real yaml...

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 POST request with the supplied OpenAPI definition using the generated Spring server stubs, then inspect how the Pet body is deserialized. Compare Cat and Dog requests with the base Pet result; done means derived fields are retained and the discriminator can select the appropriate subtype without a ClassCastException.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.