In Rest response, the field which is marked as a "discriminator" in my swagger file is duplicated. I have a parent object which has a field called "subjectType" which I've marked as the discriminator. And in my rest call, I just return the resource object(either SubjectA object or SubjectB object based on subjectType in request) which has all the parameters mentioned in the below swagger file:
Subject:
type: object
discriminator: subjectType
properties:
id:
type: string
minLength: 32
maxLength: 32
description:
type: string
maxLength: 350
subjectType:
type: string
enum:
- SubjectA
- SubjectB
required:
- subjectType
- description
SubjectA:
allOf:
- $ref: "#/definitions/Subject"
- type: object
properties:
name:
type: string
maxLength: 100
complexity:
type: string
maxLength: 256
required:
- name
- complexity
SubjectB:
allOf:
- $ref: "#/definitions/Subject"
- type: object
properties:
prof:
type: string
maxLength: 100
ref:
type: string
maxLength: 256
required:
- prof
- ref
So, when I return the response object of type either SubjectA or SubjectB, the response object which I send back has only one "subjectType" field but the actual json response returned to the client(s) has two "subjectType" fields which I think swagger is doing it. Swagger Version: 2.4.1
Here's the response:
{ "subjectType" : "SubjectA", "id" : "123", "subjectType" : "SubjectA", "name" : "abc", "complexity" : "L1" }
@Path("/subjects")
@POST
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@ResponseStatus(status = Response.Status.CREATED)
public Response createSubject(Subject subject) {
//Removed my DAO calls and other logic.
final Subject subject =
createSubject(profile); //Modifying the subject object in this method as per my needs.
return Response.status(Response.Status.CREATED).entity(subject).build();
}
Here are the objects which got generated by swagger codegen:
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-04-01T17:05:27.110-07:00")@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "subjectType", visible = true )
@JsonSubTypes({
@JsonSubTypes.Type(value = SubjectA.class, name = "SubjectA"),
@JsonSubTypes.Type(value = SubjectB.class, name = "SubjectB"),
})
public class Subject {
@JsonProperty("id")
private String id;
@JsonProperty("description")
private String description;
@JsonProperty("subjectType")
private String subjectType;
//Getters and Setters
}
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2019-04-01T17:05:27.110-07:00")
public class SubjectA extends Subject {
@JsonProperty("name")
private String name;
@JsonProperty("complexity")
private String complexity;
//Getter and Setters
}
How I stop the subjectType field from populating twice in the Json response which is sent back to client(s)?