From 8e205032ef3caf2aee2d749c6d507d27192d3960 Mon Sep 17 00:00:00 2001 From: russellb337 Date: Thu, 23 Apr 2015 08:44:50 -0700 Subject: [PATCH 1/4] Operations should inherit consumes and produces from ApiDeclaration object during Swagger 1.0 -> 2.0 conversion --- .../parser/SwaggerCompatConverter.java | 18 +++++++++--- .../converter/OperationConverterTest.java | 29 +++++++++++++++++-- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/modules/swagger-compat-spec-parser/src/main/java/io/swagger/parser/SwaggerCompatConverter.java b/modules/swagger-compat-spec-parser/src/main/java/io/swagger/parser/SwaggerCompatConverter.java index b40a157e43..fb7af0f937 100644 --- a/modules/swagger-compat-spec-parser/src/main/java/io/swagger/parser/SwaggerCompatConverter.java +++ b/modules/swagger-compat-spec-parser/src/main/java/io/swagger/parser/SwaggerCompatConverter.java @@ -327,7 +327,8 @@ else if(type != null && !type.equals("void")) return output; } - public Operation convertOperation(String tag, io.swagger.models.apideclaration.Operation operation) { + public Operation convertOperation(String tag, io.swagger.models.apideclaration.Operation operation, + ApiDeclaration apiDeclaration) { Operation output = new Operation() .summary(operation.getSummary()) .description(operation.getNotes()) @@ -339,15 +340,23 @@ public Operation convertOperation(String tag, io.swagger.models.apideclaration.O for(io.swagger.models.apideclaration.Parameter parameter : operation.getParameters()) output.parameter(convertParameter(parameter)); - if(operation.getConsumes() != null) { + if(operation.getConsumes() != null && !operation.getConsumes().isEmpty()) { for(String consumes: operation.getConsumes()) { output.consumes(consumes); } + } else if (apiDeclaration.getConsumes() != null) { + for(String consumes: apiDeclaration.getConsumes()) { + output.consumes(consumes); + } } - if(operation.getProduces() != null) { + if(operation.getProduces() != null && !operation.getProduces().isEmpty()) { for(String produces: operation.getProduces()) { output.produces(produces); } + } else if (apiDeclaration.getProduces() != null) { + for(String produces: apiDeclaration.getProduces()) { + output.produces(produces); + } } for(ResponseMessage message: operation.getResponseMessages()) { Response response = new Response().description(message.getMessage()); @@ -477,7 +486,8 @@ else if(resourceListing.getApiVersion() != null) { paths.put(apiPath, path); } for(io.swagger.models.apideclaration.Operation op : ops) { - Operation operation = convertOperation(tag, op); + Operation operation = convertOperation(tag, op, apiDeclaration); + if(op.getMethod() != null) { path.set(op.getMethod().toString().toLowerCase(), operation); } diff --git a/modules/swagger-compat-spec-parser/src/test/java/com/wordnik/swagger/converter/OperationConverterTest.java b/modules/swagger-compat-spec-parser/src/test/java/com/wordnik/swagger/converter/OperationConverterTest.java index e4f213d054..9f96050bbf 100644 --- a/modules/swagger-compat-spec-parser/src/test/java/com/wordnik/swagger/converter/OperationConverterTest.java +++ b/modules/swagger-compat-spec-parser/src/test/java/com/wordnik/swagger/converter/OperationConverterTest.java @@ -1,5 +1,6 @@ package com.wordnik.swagger.converter; +import io.swagger.models.apideclaration.ApiDeclaration; import io.swagger.parser.SwaggerCompatConverter; import io.swagger.models.ParamType; import io.swagger.models.Method; @@ -7,9 +8,7 @@ import com.wordnik.swagger.models.Operation; import com.wordnik.swagger.models.Response; -import com.wordnik.swagger.models.parameters.*; import com.wordnik.swagger.models.properties.*; -import com.wordnik.swagger.util.Json; import java.util.*; @@ -56,7 +55,7 @@ public void convertOperation1() throws Exception { parameters.add(param); operation.setParameters(parameters); - Operation converted = converter.convertOperation("tag", operation); + Operation converted = converter.convertOperation("tag", operation, new ApiDeclaration()); assertTrue(converted.getTags().size() == 1); assertEquals(converted.getTags().get(0), "tag"); @@ -78,4 +77,28 @@ public void convertOperation1() throws Exception { RefProperty ref = (RefProperty) property; assertEquals(ref.getSimpleRef(), "Cat"); } + + @Test + public void testConvertOperation_ConsumesAndProducesInheritedFromApiDeclaration() throws Exception { + Set expectedConsumes = new HashSet<>(Arrays.asList("application/json", "application/xml")); + Set expectedProduces = new HashSet<>(Arrays.asList("text/plain")); + + final ApiDeclaration apiDeclaration = new ApiDeclaration(); + apiDeclaration.setConsumes(new ArrayList<>(expectedConsumes)); + apiDeclaration.setProduces(new ArrayList<>(expectedProduces)); + + io.swagger.models.apideclaration.Operation operation = new io.swagger.models.apideclaration.Operation(); + + Operation converted = converter.convertOperation("tag", operation, apiDeclaration); + + assertSetsAreEqual(expectedConsumes, converted.getConsumes()); + assertSetsAreEqual(expectedProduces, converted.getProduces()); + } + + private void assertSetsAreEqual(Set expectedConsumes, List actualConsumes) { + Set actualConsumesSet = new HashSet<>(); + actualConsumesSet.addAll(actualConsumes); + assertEquals(expectedConsumes.size(), actualConsumes.size()); + assertTrue(actualConsumesSet.containsAll(expectedConsumes)); + } } \ No newline at end of file From 4a68e703c19b791733f30d84bb9f30266663414a Mon Sep 17 00:00:00 2001 From: russellb337 Date: Fri, 26 Jun 2015 10:04:14 -0700 Subject: [PATCH 2/4] fixing merge --- .../converter/OperationConverterTest.java | 104 ------------------ .../converter/OperationConverterTest.java | 27 ++++- 2 files changed, 25 insertions(+), 106 deletions(-) delete mode 100644 modules/swagger-compat-spec-parser/src/test/java/com/wordnik/swagger/converter/OperationConverterTest.java diff --git a/modules/swagger-compat-spec-parser/src/test/java/com/wordnik/swagger/converter/OperationConverterTest.java b/modules/swagger-compat-spec-parser/src/test/java/com/wordnik/swagger/converter/OperationConverterTest.java deleted file mode 100644 index e6fd8a3e99..0000000000 --- a/modules/swagger-compat-spec-parser/src/test/java/com/wordnik/swagger/converter/OperationConverterTest.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.wordnik.swagger.converter; - -import io.swagger.models.Operation; -import io.swagger.models.Response; -import io.swagger.models.apideclaration.ApiDeclaration; -import io.swagger.models.properties.Property; -import io.swagger.models.properties.RefProperty; -import io.swagger.parser.SwaggerCompatConverter; -import io.swagger.models.ParamType; -import io.swagger.models.Method; -import io.swagger.models.apideclaration.ResponseMessage; - -import java.util.*; - -import org.testng.annotations.Test; - -import static org.testng.Assert.*; - -public class OperationConverterTest { - SwaggerCompatConverter converter = new SwaggerCompatConverter(); - - @Test - public void convertOperation1() throws Exception { - io.swagger.models.apideclaration.Operation operation = new io.swagger.models.apideclaration.Operation(); - - operation.setMethod(Method.GET); - operation.setSummary("the summary"); - operation.setNotes("the notes"); - operation.setNickname("getFun"); - List produces = new ArrayList(); - produces.add("application/json"); - operation.setProduces(produces); - - // response type - operation.setRef("Cat"); - - // response messages - List responses = new ArrayList(); - ResponseMessage message400 = new ResponseMessage(); - message400.setCode(400); - message400.setMessage("got a 400"); - responses.add(message400); - - operation.setResponseMessages(responses); - - // parameters - io.swagger.models.apideclaration.Parameter param = new io.swagger.models.apideclaration.Parameter(); - param.setParamType(ParamType.QUERY); - param.setDescription("a string query param"); - param.setRequired(false); - param.setAllowMultiple(false); - param.setType("string"); - - List parameters = new ArrayList(); - parameters.add(param); - operation.setParameters(parameters); - - Operation converted = converter.convertOperation("tag", operation, new ApiDeclaration()); - - assertTrue(converted.getTags().size() == 1); - assertEquals(converted.getTags().get(0), "tag"); - assertEquals(operation.getSummary(), converted.getSummary()); - assertEquals(operation.getNotes(), converted.getDescription()); - assertEquals(operation.getNickname(), converted.getOperationId()); - assertTrue(converted.getProduces().size() == 1); - assertEquals(converted.getProduces().get(0), "application/json"); - assertTrue(converted.getParameters().size() == 1); - assertTrue(converted.getResponses().size() == 2); - - Response response = converted.getResponses().get("200"); - assertNotNull(response); - assertEquals(response.getDescription(), "success"); - - Property property = response.getSchema(); - assertNotNull(property); - assertTrue(property.getClass().equals(RefProperty.class)); - RefProperty ref = (RefProperty) property; - assertEquals(ref.getSimpleRef(), "Cat"); - } - - @Test - public void testConvertOperation_ConsumesAndProducesInheritedFromApiDeclaration() throws Exception { - Set expectedConsumes = new HashSet<>(Arrays.asList("application/json", "application/xml")); - Set expectedProduces = new HashSet<>(Arrays.asList("text/plain")); - - final ApiDeclaration apiDeclaration = new ApiDeclaration(); - apiDeclaration.setConsumes(new ArrayList<>(expectedConsumes)); - apiDeclaration.setProduces(new ArrayList<>(expectedProduces)); - - io.swagger.models.apideclaration.Operation operation = new io.swagger.models.apideclaration.Operation(); - - Operation converted = converter.convertOperation("tag", operation, apiDeclaration); - - assertSetsAreEqual(expectedConsumes, converted.getConsumes()); - assertSetsAreEqual(expectedProduces, converted.getProduces()); - } - - private void assertSetsAreEqual(Set expectedConsumes, List actualConsumes) { - Set actualConsumesSet = new HashSet<>(); - actualConsumesSet.addAll(actualConsumes); - assertEquals(expectedConsumes.size(), actualConsumes.size()); - assertTrue(actualConsumesSet.containsAll(expectedConsumes)); - } -} \ No newline at end of file diff --git a/modules/swagger-compat-spec-parser/src/test/java/io/swagger/converter/OperationConverterTest.java b/modules/swagger-compat-spec-parser/src/test/java/io/swagger/converter/OperationConverterTest.java index d8895d65ce..a59bdece16 100644 --- a/modules/swagger-compat-spec-parser/src/test/java/io/swagger/converter/OperationConverterTest.java +++ b/modules/swagger-compat-spec-parser/src/test/java/io/swagger/converter/OperationConverterTest.java @@ -11,8 +11,7 @@ import io.swagger.parser.SwaggerCompatConverter; import org.testng.annotations.Test; -import java.util.ArrayList; -import java.util.List; +import java.util.*; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; @@ -79,4 +78,28 @@ public void convertOperation1() throws Exception { RefProperty ref = (RefProperty) property; assertEquals(ref.getSimpleRef(), "Cat"); } + + @Test + public void testConvertOperation_ConsumesAndProducesInheritedFromApiDeclaration() throws Exception { + Set expectedConsumes = new HashSet<>(Arrays.asList("application/json", "application/xml")); + Set expectedProduces = new HashSet<>(Arrays.asList("text/plain")); + + final ApiDeclaration apiDeclaration = new ApiDeclaration(); + apiDeclaration.setConsumes(new ArrayList<>(expectedConsumes)); + apiDeclaration.setProduces(new ArrayList<>(expectedProduces)); + + io.swagger.models.apideclaration.Operation operation = new io.swagger.models.apideclaration.Operation(); + + Operation converted = converter.convertOperation("tag", operation, apiDeclaration); + + assertSetsAreEqual(expectedConsumes, converted.getConsumes()); + assertSetsAreEqual(expectedProduces, converted.getProduces()); + } + + private void assertSetsAreEqual(Set expectedConsumes, List actualConsumes) { + Set actualConsumesSet = new HashSet<>(); + actualConsumesSet.addAll(actualConsumes); + assertEquals(expectedConsumes.size(), actualConsumes.size()); + assertTrue(actualConsumesSet.containsAll(expectedConsumes)); + } } \ No newline at end of file From f1a5bf41f5e6744e0b6a6c08ee3940c4d14f4960 Mon Sep 17 00:00:00 2001 From: russellb337 Date: Fri, 26 Jun 2015 10:05:17 -0700 Subject: [PATCH 3/4] fixing formatting --- .../parser/SwaggerCompatConverter.java | 182 ++++++++---------- 1 file changed, 76 insertions(+), 106 deletions(-) diff --git a/modules/swagger-compat-spec-parser/src/main/java/io/swagger/parser/SwaggerCompatConverter.java b/modules/swagger-compat-spec-parser/src/main/java/io/swagger/parser/SwaggerCompatConverter.java index 24e37092dd..6978ca31ad 100644 --- a/modules/swagger-compat-spec-parser/src/main/java/io/swagger/parser/SwaggerCompatConverter.java +++ b/modules/swagger-compat-spec-parser/src/main/java/io/swagger/parser/SwaggerCompatConverter.java @@ -2,51 +2,21 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; -import io.swagger.models.ArrayModel; -import io.swagger.models.AuthorizationScope; -import io.swagger.models.Contact; -import io.swagger.models.Info; -import io.swagger.models.License; -import io.swagger.models.Method; +import io.swagger.models.*; import io.swagger.models.Model; -import io.swagger.models.ModelImpl; import io.swagger.models.Operation; -import io.swagger.models.ParamType; -import io.swagger.models.PassAs; -import io.swagger.models.Path; -import io.swagger.models.RefModel; -import io.swagger.models.Response; -import io.swagger.models.Scheme; -import io.swagger.models.Swagger; -import io.swagger.models.apideclaration.Api; -import io.swagger.models.apideclaration.ApiDeclaration; -import io.swagger.models.apideclaration.ExtendedTypedObject; -import io.swagger.models.apideclaration.Items; -import io.swagger.models.apideclaration.ModelProperty; -import io.swagger.models.apideclaration.ResponseMessage; +import io.swagger.models.apideclaration.*; import io.swagger.models.auth.ApiKeyAuthDefinition; import io.swagger.models.auth.AuthorizationValue; import io.swagger.models.auth.In; import io.swagger.models.auth.OAuth2Definition; -import io.swagger.models.parameters.BodyParameter; -import io.swagger.models.parameters.FormParameter; -import io.swagger.models.parameters.HeaderParameter; +import io.swagger.models.parameters.*; import io.swagger.models.parameters.Parameter; -import io.swagger.models.parameters.PathParameter; -import io.swagger.models.parameters.QueryParameter; -import io.swagger.models.parameters.SerializableParameter; import io.swagger.models.properties.ArrayProperty; import io.swagger.models.properties.Property; import io.swagger.models.properties.PropertyBuilder; import io.swagger.models.properties.RefProperty; -import io.swagger.models.resourcelisting.ApiInfo; -import io.swagger.models.resourcelisting.ApiKeyAuthorization; -import io.swagger.models.resourcelisting.ApiListingReference; -import io.swagger.models.resourcelisting.Authorization; -import io.swagger.models.resourcelisting.AuthorizationCodeGrant; -import io.swagger.models.resourcelisting.ImplicitGrant; -import io.swagger.models.resourcelisting.OAuth2Authorization; -import io.swagger.models.resourcelisting.ResourceListing; +import io.swagger.models.resourcelisting.*; import io.swagger.parser.util.RemoteUrl; import io.swagger.report.MessageBuilder; import io.swagger.transform.migrate.ApiDeclarationMigrator; @@ -344,83 +314,83 @@ public Property propertyFromTypedObject(ExtendedTypedObject obj) { return output; } - public Operation convertOperation(String tag, io.swagger.models.apideclaration.Operation operation, - ApiDeclaration apiDeclaration) { - Method method; - - if (operation.getMethod() == null) { - JsonNode node = (JsonNode) operation.getExtraFields().get("httpMethod"); - method = Method.forValue(node.asText()); - operation.setMethod(method); - } - - Operation output = new Operation() - .summary(operation.getSummary()) - .description(operation.getNotes()) - .operationId(operation.getNickname()); - - if (tag != null) { - output.tag(tag); - } - - for (io.swagger.models.apideclaration.Parameter parameter : operation.getParameters()) { - output.parameter(convertParameter(parameter)); - } - - if(operation.getConsumes() != null && !operation.getConsumes().isEmpty()) { - for(String consumes: operation.getConsumes()) { - output.consumes(consumes); + public Operation convertOperation(String tag, io.swagger.models.apideclaration.Operation operation, + ApiDeclaration apiDeclaration) { + Method method; + + if (operation.getMethod() == null) { + JsonNode node = (JsonNode) operation.getExtraFields().get("httpMethod"); + method = Method.forValue(node.asText()); + operation.setMethod(method); } - } else if (apiDeclaration.getConsumes() != null) { - for(String consumes: apiDeclaration.getConsumes()) { - output.consumes(consumes); + + Operation output = new Operation() + .summary(operation.getSummary()) + .description(operation.getNotes()) + .operationId(operation.getNickname()); + + if (tag != null) { + output.tag(tag); } - } - if(operation.getProduces() != null && !operation.getProduces().isEmpty()) { - for(String produces: operation.getProduces()) { - output.produces(produces); + + for (io.swagger.models.apideclaration.Parameter parameter : operation.getParameters()) { + output.parameter(convertParameter(parameter)); } - } else if (apiDeclaration.getProduces() != null) { - for(String produces: apiDeclaration.getProduces()) { - output.produces(produces); + + if (operation.getConsumes() != null && !operation.getConsumes().isEmpty()) { + for (String consumes : operation.getConsumes()) { + output.consumes(consumes); + } + } else if (apiDeclaration.getConsumes() != null) { + for (String consumes : apiDeclaration.getConsumes()) { + output.consumes(consumes); + } } - } - - for(ResponseMessage message: operation.getResponseMessages()) { - Response response = new Response().description(message.getMessage()); - - Model responseModel = null; - if (message.getResponseModel() != null) { - response.schema(new RefProperty(message.getResponseModel())); + if (operation.getProduces() != null && !operation.getProduces().isEmpty()) { + for (String produces : operation.getProduces()) { + output.produces(produces); + } + } else if (apiDeclaration.getProduces() != null) { + for (String produces : apiDeclaration.getProduces()) { + output.produces(produces); + } } - output.response(message.getCode(), response); - } - - // default response type - Property responseProperty = propertyFromTypedObject(operation); - if (responseProperty != null) { - Response response = new Response() - .description("success") - .schema(responseProperty); - if (output.getResponses() == null) { - output.defaultResponse(response); - } else { - output.response(200, response); + + for (ResponseMessage message : operation.getResponseMessages()) { + Response response = new Response().description(message.getMessage()); + + Model responseModel = null; + if (message.getResponseModel() != null) { + response.schema(new RefProperty(message.getResponseModel())); + } + output.response(message.getCode(), response); + } + + // default response type + Property responseProperty = propertyFromTypedObject(operation); + if (responseProperty != null) { + Response response = new Response() + .description("success") + .schema(responseProperty); + if (output.getResponses() == null) { + output.defaultResponse(response); + } else { + output.response(200, response); + } + } + + Map> auths = operation.getAuthorizations(); + + for (String securityName : auths.keySet()) { + List scopes = auths.get(securityName); + List updatedScopes = new ArrayList(); + for (AuthorizationScope s : scopes) { + updatedScopes.add(s.getScope()); + } + output.addSecurity(securityName, updatedScopes); } - } - - Map> auths = operation.getAuthorizations(); - - for (String securityName : auths.keySet()) { - List scopes = auths.get(securityName); - List updatedScopes = new ArrayList(); - for (AuthorizationScope s : scopes) { - updatedScopes.add(s.getScope()); - } - output.addSecurity(securityName, updatedScopes); - } - - return output; + + return output; } public ApiDeclaration readDeclaration(String input, MessageBuilder messages, List auths) { @@ -550,7 +520,7 @@ public Swagger convert(ResourceListing resourceListing, List api basePath = "/" + basePath; } } - + Swagger swagger = new Swagger() .host(host) From 9c915e13685f201ec33629dd6fac0f574272765d Mon Sep 17 00:00:00 2001 From: russellb337 Date: Fri, 26 Jun 2015 10:16:28 -0700 Subject: [PATCH 4/4] fixing tests --- .../java/io/swagger/converter/OperationConverterTest.java | 8 ++++---- modules/swagger-parser/src/test/scala/RemoteUrlTest.scala | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/swagger-compat-spec-parser/src/test/java/io/swagger/converter/OperationConverterTest.java b/modules/swagger-compat-spec-parser/src/test/java/io/swagger/converter/OperationConverterTest.java index a59bdece16..4567f7823c 100644 --- a/modules/swagger-compat-spec-parser/src/test/java/io/swagger/converter/OperationConverterTest.java +++ b/modules/swagger-compat-spec-parser/src/test/java/io/swagger/converter/OperationConverterTest.java @@ -13,9 +13,7 @@ import java.util.*; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; +import static org.testng.Assert.*; public class OperationConverterTest { SwaggerCompatConverter converter = new SwaggerCompatConverter(); @@ -89,8 +87,10 @@ public void testConvertOperation_ConsumesAndProducesInheritedFromApiDeclaration( apiDeclaration.setProduces(new ArrayList<>(expectedProduces)); io.swagger.models.apideclaration.Operation operation = new io.swagger.models.apideclaration.Operation(); + operation.setMethod(Method.GET); - Operation converted = converter.convertOperation("tag", operation, apiDeclaration); + final SwaggerCompatConverter swaggerCompatConverter = new SwaggerCompatConverter(); + Operation converted = swaggerCompatConverter.convertOperation("tag", operation, apiDeclaration); assertSetsAreEqual(expectedConsumes, converted.getConsumes()); assertSetsAreEqual(expectedProduces, converted.getProduces()); diff --git a/modules/swagger-parser/src/test/scala/RemoteUrlTest.scala b/modules/swagger-parser/src/test/scala/RemoteUrlTest.scala index 7636bc22a6..156b2c2220 100644 --- a/modules/swagger-parser/src/test/scala/RemoteUrlTest.scala +++ b/modules/swagger-parser/src/test/scala/RemoteUrlTest.scala @@ -9,13 +9,13 @@ import scala.collection.JavaConverters._ @RunWith(classOf[JUnitRunner]) class RemoteUrlTest extends FlatSpec with Matchers { it should "read a remote URL" in { - val output = RemoteUrl.urlToString("http://petstore.swagger.io/v2/pet/3", null) + val output = RemoteUrl.urlToString("http://petstore.swagger.io/v2/pet/1", null) output should not be (null) } it should "set a header" in { val av = new AuthorizationValue("accept", "application/xml", "header") - val output = RemoteUrl.urlToString("http://petstore.swagger.io/v2/pet/3", List(av).asJava) + val output = RemoteUrl.urlToString("http://petstore.swagger.io/v2/pet/1", List(av).asJava) output.trim.charAt(0) should be('<') }