Skip to content

polymorphism: swagger-codegen generates doubled classtype-attribute in json #9

Open
@volkaoth

Description

@volkaoth

TL;DR:
Using our polymorphic class-definitions, used e.g. in Tasks by Ports, Interfaces and Requirements in combination with the actual swagger-codegen master (version 2.3.0) results in a doubled listed classtype-attribute in the json output.
Solution: Redefining JsonTypeInfo in generated superclass models

Problem:
polymorphistic Classdefinitions in swagger:
e.g. Port-Definition:
superclass:

Port:
type: object
discriminator: type
description: |
Represents a communication port of a task
required:
- name
properties:
type:
description: |
Discriminator for polymorphism.
type: string
name:
type: string
description: |
Uniquely identifies a port. Defines the name of the environment variables holding IP addresses of remote tasks.

and the subclasses:

PortProvided:
type: object
description: Represents a communication port that the tasks provides for other tasks or the end user.
allOf:
- $ref: '#/definitions/Port'
- properties:
port:
type: integer
format: int32

PortRequired:
type: object
description: Represents a communication port that the task requires from other (downstream) tasks.
allOf:
- $ref: '#/definitions/Port'
- properties:
updateAction:
type: string
description: A script that is executed if a new instance of a downstream task is available.
isMandatory:
type: boolean
description: States if an instance of a downstream tasks needs to be already started (true), or if the task can start without a downstream task (false).

The generated Model of the superclass Port.java looks like:
(important therefor are the first lines)

@apimodel(description = "Represents a communication port of a task ")
@validated
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY, property = "type", visible = true )
@JsonSubTypes({
@JsonSubTypes.Type(value = PortRequired.class, name = "PortRequired"),
@JsonSubTypes.Type(value = PortProvided.class, name = "PortProvided"),
})
...

Resulting json output of a task using the polymophistic class-definition:

{"name":"TaskTest","ports":[{"type":"PortProvided","type":"PortProvided","name":"PortProvidedTest","port":12345},{"type":"PortRequired","type":"PortRequired","name":"PortRequiredTest","updateAction":"UpdateActionTest","isMandatory":true}],"interfaces":[{"type":"DockerInterface","type":"DockerInterface","dockerImage":"DockerImage"},{"type":"LanceInterface","type":"LanceInterface","init":"init","preInstall":"preInstall","install":"install","postInstall":"postInstall","preStart":"preStart","start":"start","startDetection":"startDetection","stopDetection":"stopDetection","postStart":"postStart","preStop":"preStop","stop":"stop","postStop":"postStop","shutdown":"shutdown"}],"executionEnvironment":"LANCE","requirements":[{"type":"OclRequirement","type":"OclRequirement","constraint":"oclRequirement"},{"type":"IdentifierRequirement","type":"IdentifierRequirement","hardwareId":"hardwareId","locationId":"locationId","imageId":"imageId"}],"taskType":"BATCH"}

Solution to avoid the highlighted doubled typedefintion:
changing the JsonTypeInfo in the superclass.
from "[...]include = AS.PROPERTY, property = "type"[...]"
to "[...]include = AS.EXISTING_PROPERTY, property = "type"[...]"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions