Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
volkaoth opened this issue Dec 4, 2017 · 0 comments

Comments

@volkaoth
Copy link
Contributor

volkaoth commented Dec 4, 2017

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"[...]"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant