Skip to content

Is polymorphism supported in code generation? #3904

Open
@cacti77

Description

@cacti77

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...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions