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 @@ -27,6 +27,7 @@
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchema;

import io.swagger.models.refs.RefFormat;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -173,7 +174,11 @@ public Property resolveProperty(JavaType propType,
}
if (innerModel instanceof ModelImpl) {
ModelImpl mi = (ModelImpl) innerModel;
property = new RefProperty(StringUtils.isNotEmpty(mi.getReference()) ? mi.getReference() : mi.getName());
if (StringUtils.isNotEmpty(mi.getReference())) {
property = new RefProperty(mi.getReference());
} else {
property = new RefProperty(mi.getName(), RefFormat.INTERNAL);
}
}
}
}
Expand Down Expand Up @@ -970,7 +975,7 @@ private boolean resolveSubtypes(ModelImpl model, BeanDescription bean, ModelConv
}

impl.setDiscriminator(null);
ComposedModel child = new ComposedModel().parent(new RefModel(model.getName())).child(impl);
ComposedModel child = new ComposedModel().parent(new RefModel(model.getName(), RefFormat.INTERNAL)).child(impl);
context.defineModel(impl.getName(), child, subtypeType, null);
++count;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.swagger.jackson.mixin;

import com.fasterxml.jackson.annotation.JsonIgnore;

public abstract class IgnoreOriginalRefMixin {

@JsonIgnore
public abstract String getOriginalRef();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.swagger.jackson.mixin;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;

public abstract class OriginalRefMixin {

@JsonIgnore
public abstract String get$ref();

@JsonGetter("$ref")
public abstract String getOriginalRef();

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
import io.swagger.jackson.mixin.ResponseSchemaMixin;
import io.swagger.models.Response;




public class ObjectMapperFactory {

protected static ObjectMapper createJson() {
Expand Down Expand Up @@ -43,6 +40,8 @@ private static ObjectMapper create(JsonFactory jsonFactory, boolean includePathD

mapper.addMixIn(Response.class, ResponseSchemaMixin.class);

ReferenceSerializationConfigurer.serializeAsComputedRef(mapper);

return mapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.swagger.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.jackson.mixin.IgnoreOriginalRefMixin;
import io.swagger.jackson.mixin.OriginalRefMixin;
import io.swagger.models.RefModel;
import io.swagger.models.RefPath;
import io.swagger.models.RefResponse;
import io.swagger.models.parameters.RefParameter;
import io.swagger.models.properties.RefProperty;

/**
* @since 1.5.21
*/
public abstract class ReferenceSerializationConfigurer {

private static void serializeAs(Class<?> cls, ObjectMapper mapper) {
mapper.addMixIn(RefModel.class, cls);
mapper.addMixIn(RefProperty.class, cls);
mapper.addMixIn(RefPath.class, cls);
mapper.addMixIn(RefParameter.class, cls);
mapper.addMixIn(RefResponse.class, cls);
}

public static void serializeAsOriginalRef() {
serializeAs(OriginalRefMixin.class, Json.mapper());
serializeAs(OriginalRefMixin.class, Yaml.mapper());
}

public static void serializeAsComputedRef() {
serializeAs(IgnoreOriginalRefMixin.class, Json.mapper());
serializeAs(IgnoreOriginalRefMixin.class, Yaml.mapper());
}

public static void serializeAsOriginalRef(ObjectMapper mapper) {
serializeAs(OriginalRefMixin.class, mapper);
}

public static void serializeAsComputedRef(ObjectMapper mapper) {
serializeAs(IgnoreOriginalRefMixin.class, mapper);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public static void assertEqualsToJson(Object objectToSerialize, String jsonStr)
apply(objectToSerialize, jsonStr, Json.mapper());
}

public static void assertEqualsToString(Object objectToSerialize, String jsonStr, ObjectMapper mapper) {
apply(objectToSerialize, jsonStr, mapper);
}

private static void apply(Object objectToSerialize, String str, ObjectMapper mapper) {
final ObjectNode lhs = mapper.convertValue(objectToSerialize, ObjectNode.class);
ObjectNode rhs = null;
Expand Down
51 changes: 51 additions & 0 deletions modules/swagger-jaxrs/src/test/java/io/swagger/ReferenceTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package io.swagger;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.converter.ModelConverters;
import io.swagger.jaxrs.Reader;
import io.swagger.matchers.SerializationMatchers;
import io.swagger.models.Pet;
import io.swagger.models.Swagger;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.refs.GenericRef;
import io.swagger.resources.ResourceWithMoreReferences;
import io.swagger.resources.ResourceWithReferences;
import io.swagger.util.Json;
import io.swagger.util.ReferenceSerializationConfigurer;
import io.swagger.util.ResourceUtils;
import org.testng.annotations.Test;

Expand All @@ -32,8 +37,54 @@ public void scanModel() {

@Test(description = "Scan API with operation and response references")
public void scanAPI() throws IOException {

final Swagger swagger = new Reader(new Swagger()).read(ResourceWithReferences.class);
final String json = ResourceUtils.loadClassResource(getClass(), "ResourceWithReferences.json");
SerializationMatchers.assertEqualsToJson(swagger, json);
}

@Test(description = "Scan API with references")
public void scanRef() throws IOException {

final Swagger swagger = new Reader(new Swagger()).read(ResourceWithMoreReferences.class);
final String json = ResourceUtils.loadClassResource(getClass(), "ResourceWithMoreReferences.json");
SerializationMatchers.assertEqualsToJson(swagger, json);
}

@Test(description = "Serialize API with references and OriginalRefMixin activated")
public void serializeRefWithOriginalRef() throws Exception {

// workaround for https://github.com/FasterXML/jackson-databind/issues/1998
ObjectMapper mapper = Json.mapper().copy();
ReferenceSerializationConfigurer.serializeAsOriginalRef(mapper);

final String json = ResourceUtils.loadClassResource(getClass(), "ResourceWithMoreReferencesAsOriginalRef.json");
Swagger swagger = Json.mapper().readValue(json, Swagger.class);
SerializationMatchers.assertEqualsToString(swagger, json, mapper);
}

@Test(description = "Serialize API with references and internal ref also with dots activated")
public void serializeRefWithInternalRef() throws Exception {
try {
GenericRef.internalRefWithAnyDot();
final String json = ResourceUtils.loadClassResource(getClass(), "ResourceWithMoreReferencesWithInternalRef.json");
Swagger swagger = Json.mapper().readValue(json, Swagger.class);
SerializationMatchers.assertEqualsToJson(swagger, json);
} finally {
GenericRef.relativeRefWithAnyDot();
}
}

@Test(description = "Scan API with references and OriginalRefMixin activated")
public void scanRefWithOriginalRef() throws IOException {

// workaround for https://github.com/FasterXML/jackson-databind/issues/1998
ObjectMapper mapper = Json.mapper().copy();
ReferenceSerializationConfigurer.serializeAsOriginalRef(mapper);

final Swagger swagger = new Reader(new Swagger()).read(ResourceWithMoreReferences.class);
final String json = ResourceUtils.loadClassResource(getClass(), "ResourceWithMoreReferencesAsOriginalRef.json");
SerializationMatchers.assertEqualsToString(swagger, json, mapper);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.swagger.models;

import io.swagger.annotations.ApiModelProperty;

public class ModelWithReferences {
public String getOne() {
return null;
}

@ApiModelProperty(reference = "http://swagger.io/schemas.json#/Models/AnotherModel")
public String getAnother() {
return null;
}

@ApiModelProperty(reference = "Three")
public String getThree() {
return null;
}

@ApiModelProperty(reference = "Four.json")
public String getFour() {
return null;
}

@ApiModelProperty(reference = "Five.json.MyClass")
public String getFive() {
return null;
}

@ApiModelProperty(reference = "./Six")
public String getSix() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.swagger.resources;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.models.ModelContainingModelWithReference;
import io.swagger.models.ModelWithReference;
import io.swagger.models.ModelWithReferences;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

@Api(value = "/basic")
@Path("/")
public class ResourceWithMoreReferences {

@GET
@Path("/test")
@ApiResponses({
@ApiResponse(code = 500, message = "Error", reference = "http://swagger.io/schemas.json#/Models/ErrorResponse")
})
public Response getTest() throws WebApplicationException {
return Response.ok().build();
}

@GET
@Path("/some")
@ApiOperation(value = "Get Some", responseReference = "http://swagger.io/schemas.json#/Models/SomeResponse")
public Response getSome() throws WebApplicationException {
return Response.ok().build();
}

@GET
@Path("/testSome")
@ApiOperation(value = "Get Some", responseReference = "http://swagger.io/schemas.json#/Models/SomeResponse")
@ApiResponses({
@ApiResponse(code = 500, message = "Error", reference = "http://swagger.io/schemas.json#/Models/ErrorResponse")
})
public Response getTestSome() throws WebApplicationException {
return Response.ok().build();
}

@GET
@Path("/testSomeOther")
@ApiOperation(value = "Get Some Other", responseReference = "foo")
@ApiResponses({
@ApiResponse(code = 500, message = "Error", reference = "foo.json"),
@ApiResponse(code = 502, message = "Error", reference = "foo.json.MyClass")
})
public Response getTestSomeOther() throws WebApplicationException {
return Response.ok().build();
}

@GET
@Path("/model")
@ApiOperation(value = "Get Model", response = ModelContainingModelWithReference.class)
public Response getModel() throws WebApplicationException {
return Response.ok().build();
}

@GET
@Path("/anotherModel")
@ApiOperation(value = "Get Another Model", response = ModelWithReference.class)
public Response getAnotherModel() throws WebApplicationException {
return Response.ok().build();
}

@GET
@Path("/aThirdModel")
@ApiOperation(value = "Get A Third Model", response = ModelWithReferences.class)
public Response getAThirdModel() throws WebApplicationException {
return Response.ok().build();
}
}
Loading