Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,9 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
}
}
}

resolveDiscriminatorProperty(type, context, model);

return model;
}

Expand Down Expand Up @@ -1624,6 +1627,27 @@ protected Map<String, Object> resolveExtensions(Annotated a, Annotation[] annota
return null;
}

protected void resolveDiscriminatorProperty(JavaType type, ModelConverterContext context, Schema model) {
// add JsonTypeInfo.property if not member of bean
JsonTypeInfo typeInfo = type.getRawClass().getDeclaredAnnotation(JsonTypeInfo.class);
if (typeInfo != null) {
String typeInfoProp = typeInfo.property();
if (StringUtils.isNotBlank(typeInfoProp)) {
Schema modelToUpdate = model;
if (StringUtils.isNotBlank(model.get$ref())) {
modelToUpdate = context.getDefinedModels().get(model.get$ref().substring(21));
}
if (modelToUpdate.getProperties() == null || !modelToUpdate.getProperties().keySet().contains(typeInfoProp)) {
Schema discriminatorSchema = new StringSchema().name(typeInfoProp);
modelToUpdate.addProperties(typeInfoProp, discriminatorSchema);
if (modelToUpdate.getRequired() == null || !model.getRequired().contains(typeInfoProp)) {
modelToUpdate.addRequiredItem(typeInfoProp);
}
}
}
}
}

protected Discriminator resolveDiscriminator(JavaType type, ModelConverterContext context) {

io.swagger.v3.oas.annotations.media.Schema declaredSchemaAnnotation = AnnotationsUtils.getSchemaDeclaredAnnotation(type.getRawClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import io.swagger.v3.oas.annotations.media.DiscriminatorMapping;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Schema;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Map;
Expand All @@ -23,12 +24,18 @@ public class InheritedBeanTest extends SwaggerTestBase {
private ModelResolver modelResolver;
private ModelConverterContextImpl context;

@BeforeTest
@BeforeMethod
public void setup() {
ModelResolver.composedModelPropertiesAsSibling = false;
modelResolver = new ModelResolver(new ObjectMapper());
context = new ModelConverterContextImpl(modelResolver);
}

@AfterTest
public void afterTest() {
ModelResolver.composedModelPropertiesAsSibling = false;
}

@Test
public void testInheritedBean() throws Exception {
final Schema baseModel = context.resolve(new AnnotatedType(BaseBean.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ public void testTicket3197() throws Exception {
final Schema model = context.resolve(new AnnotatedType(Car.class));
assertNotNull(model);
String yaml = "Car:\n" +
" required:\n" +
" - type\n" +
" type: object\n" +
" properties:\n" +
" carMetaData:\n" +
" type: string\n" +
" type:\n" +
" type: string\n" +
" discriminator:\n" +
" propertyName: type\n" +
" mapping:\n" +
Expand Down Expand Up @@ -77,7 +81,7 @@ public void testTicket3197() throws Exception {
" type: integer\n" +
" format: int64\n" +
" model:\n" +
" type: string";
" type: string\n";

SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), yaml);
}
Expand All @@ -87,15 +91,20 @@ public void testTicket3197AsSibling() throws Exception {

ModelResolver.composedModelPropertiesAsSibling = true;
ModelResolver myModelResolver = new ModelResolver(new ObjectMapper());
ModelResolver.composedModelPropertiesAsSibling = true;
ModelConverterContextImpl myContext = new ModelConverterContextImpl(myModelResolver);

final Schema model = myContext.resolve(new AnnotatedType(Car.class));
assertNotNull(model);
String yaml = "Car:\n" +
" required:\n" +
" - type\n" +
" type: object\n" +
" properties:\n" +
" carMetaData:\n" +
" type: string\n" +
" type:\n" +
" type: string\n" +
" discriminator:\n" +
" propertyName: type\n" +
" mapping:\n" +
Expand Down Expand Up @@ -128,7 +137,7 @@ public void testTicket3197AsSibling() throws Exception {
" model:\n" +
" type: string\n" +
" allOf:\n" +
" - $ref: '#/components/schemas/Car'";
" - $ref: '#/components/schemas/Car'\n";

SerializationMatchers.assertEqualsToYaml(myContext.getDefinedModels(), yaml);
ModelResolver.composedModelPropertiesAsSibling = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import io.swagger.v3.jaxrs2.resources.TagsResource;
import io.swagger.v3.jaxrs2.resources.Test2607;
import io.swagger.v3.jaxrs2.resources.TestResource;
import io.swagger.v3.jaxrs2.resources.Ticket2340Resource;
import io.swagger.v3.jaxrs2.resources.Ticket2644ConcreteImplementation;
import io.swagger.v3.jaxrs2.resources.Ticket2763Resource;
import io.swagger.v3.jaxrs2.resources.Ticket2793Resource;
Expand Down Expand Up @@ -993,6 +994,55 @@ public void testTicket2763() {
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}

@Test(description = "Responses with array schema")
public void testTicket2340() {
Reader reader = new Reader(new OpenAPI());

OpenAPI openAPI = reader.read(Ticket2340Resource.class);
String yaml = "openapi: 3.0.1\n" +
"paths:\n" +
" /test/test:\n" +
" post:\n" +
" operationId: getBook\n" +
" responses:\n" +
" default:\n" +
" description: default response\n" +
" content:\n" +
" application/json:\n" +
" schema:\n" +
" type: string\n" +
"components:\n" +
" schemas:\n" +
" Animal:\n" +
" required:\n" +
" - type\n" +
" type: object\n" +
" properties:\n" +
" type:\n" +
" type: string\n" +
" discriminator:\n" +
" propertyName: type\n" +
" Cat:\n" +
" type: object\n" +
" allOf:\n" +
" - $ref: '#/components/schemas/Animal'\n" +
" - type: object\n" +
" properties:\n" +
" lives:\n" +
" type: integer\n" +
" format: int32\n" +
" Dog:\n" +
" type: object\n" +
" allOf:\n" +
" - $ref: '#/components/schemas/Animal'\n" +
" - type: object\n" +
" properties:\n" +
" barkVolume:\n" +
" type: number\n" +
" format: double\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}

@Test(description = "array schema example")
public void testTicket2806() {
Reader reader = new Reader(new OpenAPI());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.swagger.v3.jaxrs2.resources;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import io.swagger.v3.oas.annotations.parameters.RequestBody;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/test")
public class Ticket2340Resource {

@Produces({ MediaType.APPLICATION_JSON })
@Path("/test")
@POST
public String getBook(@RequestBody Animal animal) {
return "ok";
}


@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")//, visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = Dog.class, name = "dog"),
@JsonSubTypes.Type(value = Cat.class, name = "cat")
})
public static class Animal {
//public String type;
}

@JsonTypeName("dog")
public static class Dog extends Animal {
public double barkVolume;
}

@JsonTypeName("cat")
public static class Cat extends Animal {
boolean likesCream;
public int lives;
}

}