Skip to content

Commit

Permalink
Merge pull request #2835 from swagger-api/responses-extensions-OAS2
Browse files Browse the repository at this point in the history
Adding Extensions in  Responses (OAS2)
  • Loading branch information
gracekarina authored Jul 2, 2021
2 parents 68d8974 + 206a9e0 commit b4b9282
Show file tree
Hide file tree
Showing 20 changed files with 251 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ private Swagger removeBrokenReferenceDefinitions (Swagger swagger) {
}
if (path.getOperations() != null) {
for (Operation op: path.getOperations()) {
if (op.getResponses() != null) {
for (Response response: op.getResponses().values()) {
if (op.getResponsesObject() != null) {
for (Response response: op.getResponsesObject().values()) {
Set<String> modelRef = getModelRef(response.getResponseSchema());
if (modelRef != null) {
referencedDefinitions.addAll(modelRef);
Expand Down Expand Up @@ -280,7 +280,7 @@ public Operation filterOperation(SwaggerSpecFilter filter, Operation op, ApiDesc
}
clonedOperation.setParameters(clonedParams);
clonedOperation.setSecurity(op.getSecurity());
clonedOperation.setResponses(op.getResponses());
clonedOperation.setResponsesObject(op.getResponsesObject());

return clonedOperation;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.swagger.jackson.mixin;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSetter;
import io.swagger.models.Response;
import io.swagger.models.Responses;


import java.util.Map;

public abstract class OperationResponseMixin {

@JsonIgnore
public abstract Map<String,Response> getResponses();

@JsonIgnore
public abstract void setResponses(Map<String,Response> responses);

@JsonGetter("responses")
public abstract Responses getResponsesObject();

@JsonSetter("responses")
public abstract void getResponsesObject(Responses responsesObject);



}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.swagger.jackson.mixin.OperationResponseMixin;
import io.swagger.jackson.mixin.ResponseSchemaMixin;
import io.swagger.models.Operation;
import io.swagger.models.Response;

public class ObjectMapperFactory {
Expand Down Expand Up @@ -39,6 +41,7 @@ private static ObjectMapper create(JsonFactory jsonFactory, boolean includePathD
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

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

ReferenceSerializationConfigurer.serializeAsComputedRef(mapper);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ArrayPropertyDeserializerTest {
public void testArrayDeserialization () throws Exception {

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
assertNotNull(response);

Model responseSchema = response.getResponseSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class MapPropertyDeserializerTest {
public void testMapDeserialization () throws Exception {

Operation operation = Json.mapper().readValue(json, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
assertNotNull(response);

Model responseSchema = response.getResponseSchema();
Expand All @@ -62,7 +62,7 @@ public void testMapDeserialization () throws Exception {
@Test(description = "vendor extensions should be included with object type")
public void testMapDeserializationVendorExtensions () throws Exception {
Operation operation = Json.mapper().readValue(json, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
assertNotNull(response);

Model responseSchema = response.getResponseSchema();
Expand Down Expand Up @@ -97,7 +97,7 @@ public void testIssue1261InlineSchemaExample() throws Exception {
" name: Arthur Dent\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
assertNotNull(response);
Model schema = response.getResponseSchema();
Object example = schema.getExample();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void testDeserializeAPathRef() throws Exception {
public void testDeserializeAResponseRef() throws Exception {
final Swagger swagger = TestUtils.deserializeJsonFileFromClasspath("specFiles/responseRef.json", Swagger.class);

final Map<String, Response> responseMap = swagger.getPath("/pet").getPut().getResponses();
final Map<String, Response> responseMap = swagger.getPath("/pet").getPut().getResponsesObject();

assertIsRefResponse(responseMap.get("405"), "http://my.company.com/responses/errors.json#/method-not-allowed");
assertIsRefResponse(responseMap.get("404"), "http://my.company.com/responses/errors.json#/not-found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testSerializeASpecWithResponseReferences() throws Exception {
String swaggerJson = Json.mapper().writeValueAsString(swagger);
Swagger rebuilt = Json.mapper().readValue(swaggerJson, Swagger.class);

assertEquals(rebuilt.getPath("/health").getGet().getResponses().get("200"), expectedResponse);
assertEquals(rebuilt.getPath("/health").getGet().getResponsesObject().get("200"), expectedResponse);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void convertToUUIDProperty()throws Exception{
" format: uuid\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -63,7 +63,7 @@ public void convertToEmailProperty()throws Exception{
" format: email\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -85,7 +85,7 @@ public void convertToBooleanProperty()throws Exception{
" type: boolean\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -107,7 +107,7 @@ public void convertToDateProperty()throws Exception{
" format: date\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -130,7 +130,7 @@ public void convertToDateTimeProperty()throws Exception{
" format: date-time\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -153,7 +153,7 @@ public void convertToStringProperty()throws Exception{
" format: password\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -178,7 +178,7 @@ public void convertToStringWithEnumProperty()throws Exception{
" - b\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -202,7 +202,7 @@ public void convertToStringNewProperty()throws Exception{
" pattern: Pattern\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -226,7 +226,7 @@ public void convertToBinaryProperty()throws Exception{
" format: binary\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -249,7 +249,7 @@ public void convertToDoubleProperty()throws Exception{
" format: double\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -274,7 +274,7 @@ public void convertToNumericNewProperties()throws Exception{
" format: double\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -299,7 +299,7 @@ public void convertToByteArrayProperty()throws Exception{
" format: byte\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -322,7 +322,7 @@ public void convertToLongProperty()throws Exception{
" format: int64\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -345,7 +345,7 @@ public void convertToIntegerProperty()throws Exception{
" format: int32\n";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand All @@ -371,7 +371,7 @@ public void convertToArrayProperty()throws Exception{
" example: 1985-04-12T23:20:50.52Z";

Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Assert.assertNotNull(response);
Property property = response.getSchema();

Expand Down Expand Up @@ -596,7 +596,7 @@ public void convertStringProperty()throws Exception{

Path string = swagger.getPaths().get("/string");
Operation operation = string.getOperations().get(0);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Property property = response.getSchema();

PropertyModelConverter converter = new PropertyModelConverter();
Expand All @@ -619,7 +619,7 @@ public void convertStringPropertyWithEnum()throws Exception{

Path string = swagger.getPaths().get("/stringenum");
Operation operation = string.getOperations().get(0);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Property property = response.getSchema();

Assert.assertEquals(((StringProperty)property).getEnum().size(),2);
Expand All @@ -645,7 +645,7 @@ public void convertStringRefProperty()throws Exception{

Path string = swagger.getPaths().get("/stringRef");
Operation operation = string.getOperations().get(0);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Property property = response.getSchema();

PropertyModelConverter converter = new PropertyModelConverter();
Expand All @@ -667,7 +667,7 @@ public void convertBooleanProperty()throws Exception{

Path string = swagger.getPaths().get("/boolean");
Operation operation = string.getOperations().get(0);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Property property = response.getSchema();

PropertyModelConverter converter = new PropertyModelConverter();
Expand All @@ -690,7 +690,7 @@ public void convertNumberProperty()throws Exception{

Path string = swagger.getPaths().get("/number");
Operation operation = string.getOperations().get(0);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Property property = response.getSchema();

PropertyModelConverter converter = new PropertyModelConverter();
Expand All @@ -713,7 +713,7 @@ public void convertArrayProperty()throws Exception{

Path string = swagger.getPaths().get("/arrayOfInt");
Operation operation = string.getOperations().get(0);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Property property = response.getSchema();

PropertyModelConverter converter = new PropertyModelConverter();
Expand All @@ -736,7 +736,7 @@ public void convertArrayOfRefProperty()throws Exception{

Path string = swagger.getPaths().get("/arrayOfRef");
Operation operation = string.getOperations().get(0);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Property property = response.getSchema();

PropertyModelConverter converter = new PropertyModelConverter();
Expand All @@ -761,7 +761,7 @@ public void convertArrayRefProperty()throws Exception{

Path string = swagger.getPaths().get("/arrayRef");
Operation operation = string.getOperations().get(0);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Property property = response.getSchema();

PropertyModelConverter converter = new PropertyModelConverter();
Expand All @@ -783,7 +783,7 @@ public void convertObjectProperty()throws Exception{

Path string = swagger.getPaths().get("/object");
Operation operation = string.getOperations().get(0);
Response response = operation.getResponses().get("200");
Response response = operation.getResponsesObject().get("200");
Property property = response.getSchema();

PropertyModelConverter converter = new PropertyModelConverter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ private Operation parseMethod(Class<?> cls, Method method, AnnotatedMethod annot
// merge class level @ApiResponse
for (ApiResponse apiResponse : classApiResponses) {
String key = (apiResponse.code() == 0) ? "default" : String.valueOf(apiResponse.code());
if (operation.getResponses() != null && operation.getResponses().containsKey(key)) {
if (operation.getResponsesObject() != null && operation.getResponsesObject().containsKey(key)) {
continue;
}
addResponse(operation, apiResponse, jsonViewAnnotation);
Expand Down Expand Up @@ -949,7 +949,7 @@ private Operation parseMethod(Class<?> cls, Method method, AnnotatedMethod annot
}
}

if (operation.getResponses() == null) {
if (operation.getResponsesObject() == null) {
Response response = new Response().description(SUCCESSFUL_OPERATION);
operation.defaultResponse(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ private BodyParameter getBodyParameter(Operation op, int index) {
@Test(description = "Tests child type response schema ref is correctly set up")
public void testChildTypeResponse() {
Operation op = swagger.getPath("/childType/testChildTypeResponse").getGet();
Model schema = op.getResponses().get("200").getResponseSchema();
Model schema = op.getResponsesObject().get("200").getResponseSchema();
assertEquals(schema.getClass().getName(), RefModel.class.getName());
assertEquals(((RefModel) schema).getSimpleRef(), "Sub1Bean");
}

@Test(description = "Tests child type response schema ref is correctly set up when specified on the operation")
public void testChildTypeResponseOnOperation() {
Operation op = swagger.getPath("/childType/testChildTypeResponseOnOperation").getGet();
Model schema = op.getResponses().get("200").getResponseSchema();
Model schema = op.getResponsesObject().get("200").getResponseSchema();
assertEquals(schema.getClass().getName(), RefModel.class.getName());
assertEquals(((RefModel) schema).getSimpleRef(), "Sub1Bean");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void checkParametersOfGenericTypes() {
@Test(description = "check generic result")
public void checkGenericResult() {
Operation op = swagger.getPath("/generics/testGenericResult").getGet();
Model schema = op.getResponses().get("200").getResponseSchema();
Model schema = op.getResponsesObject().get("200").getResponseSchema();
assertEquals(schema.getClass().getName(), RefModel.class.getName());
assertEquals(((RefModel) schema).getSimpleRef(), "GenericListWrapperTag");

Expand Down
Loading

0 comments on commit b4b9282

Please sign in to comment.