diff --git a/modules/swagger-compat-spec-parser/pom.xml b/modules/swagger-compat-spec-parser/pom.xml index 91f44632b3..95176e3f8b 100644 --- a/modules/swagger-compat-spec-parser/pom.xml +++ b/modules/swagger-compat-spec-parser/pom.xml @@ -3,13 +3,13 @@ io.swagger swagger-parser-project - 1.0.4 + 1.0.5 ../.. 4.0.0 io.swagger swagger-compat-spec-parser - 1.0.4 + 1.0.5 jar swagger-compat-spec-parser 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 bdd2a9586e..561ee75e50 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 @@ -319,10 +319,8 @@ else if(items.getRef() != null) { else { if(obj.getRef() != null) output = new RefProperty(obj.getRef()); - else if(type != null) + else if(type != null && !type.equals("void")) output = new RefProperty(type); - else - output = new RefProperty("void"); } } diff --git a/modules/swagger-parser/pom.xml b/modules/swagger-parser/pom.xml index eabdbd81cb..63daccdb4a 100644 --- a/modules/swagger-parser/pom.xml +++ b/modules/swagger-parser/pom.xml @@ -3,13 +3,13 @@ io.swagger swagger-parser-project - 1.0.4 + 1.0.5 ../.. 4.0.0 io.swagger swagger-parser - 1.0.4 + 1.0.5 jar swagger-parser diff --git a/modules/swagger-parser/src/main/java/io/swagger/parser/SwaggerResolver.java b/modules/swagger-parser/src/main/java/io/swagger/parser/SwaggerResolver.java index 56f88a829f..6725ef8cbd 100644 --- a/modules/swagger-parser/src/main/java/io/swagger/parser/SwaggerResolver.java +++ b/modules/swagger-parser/src/main/java/io/swagger/parser/SwaggerResolver.java @@ -20,7 +20,7 @@ public class SwaggerResolver { Logger LOGGER = LoggerFactory.getLogger(SwaggerResolver.class); protected Swagger swagger; - protected Map resolutionMap = new HashMap(); + protected Map> resolutionMap = new HashMap>(); protected ResolverOptions opts; public SwaggerResolver(){} @@ -57,70 +57,71 @@ public void applyResolutions(List auths) { objectList = new ArrayList(); hostToObjectMap.put(host, objectList); } - ResolutionContext ctx = resolutionMap.get(path); - - Object mapping = ctx.object; - Object target = ctx.parent; - try { - String contents = null; - if(host.startsWith("http")) - contents = new RemoteUrl().urlToString(host, auths); - else - contents = Json.mapper().writeValueAsString(swagger); - JsonNode location = null; - String locationName = null; - if(contents != null) { - location = Json.mapper().readTree(contents); - String[] objectPath = definitionPath.split("/"); - for(String objectPathPart : objectPath) { - LOGGER.debug("getting part " + objectPathPart); - if(objectPathPart.length() > 0 && location != null) { - location = location.get(objectPathPart); - locationName = objectPathPart; - } - } - } - if(location != null) { - // convert the node to the proper type - if(mapping instanceof Property) { - Model model = Json.mapper().convertValue(location, Model.class); - if(mapping instanceof RefProperty) { - RefProperty ref = (RefProperty) mapping; - ref.set$ref(locationName); - swagger.addDefinition(locationName, model); + List contexts = resolutionMap.get(path); + for(ResolutionContext ctx : contexts) { + Object mapping = ctx.object; + Object target = ctx.parent; + try { + String contents = null; + if(host.startsWith("http")) + contents = new RemoteUrl().urlToString(host, auths); + else + contents = Json.mapper().writeValueAsString(swagger); + JsonNode location = null; + String locationName = null; + if(contents != null) { + location = Json.mapper().readTree(contents); + String[] objectPath = definitionPath.split("/"); + for(String objectPathPart : objectPath) { + LOGGER.debug("getting part " + objectPathPart); + if(objectPathPart.length() > 0 && location != null) { + location = location.get(objectPathPart); + locationName = objectPathPart; + } } } - else if(target instanceof Parameter) { - if(mapping instanceof RefModel) { + if(location != null) { + // convert the node to the proper type + if(mapping instanceof Property) { Model model = Json.mapper().convertValue(location, Model.class); - RefModel ref = (RefModel) mapping; - ref.set$ref(locationName); - swagger.addDefinition(locationName, model); + if(mapping instanceof RefProperty) { + RefProperty ref = (RefProperty) mapping; + ref.set$ref(locationName); + swagger.addDefinition(locationName, model); + } } - } - else if(target instanceof Operation) { - - // get the operation position - Operation operation = (Operation) target; - int position = 0; - for(Parameter param : operation.getParameters()) { - - if(param instanceof RefParameter) { - RefParameter ref = (RefParameter) param; - if(ref.getSimpleRef().equals(locationName)) { - // found a match! - Parameter remoteParam = Json.mapper().convertValue(location, Parameter.class); - operation.getParameters().set(position, remoteParam); + else if(target instanceof Parameter) { + if(mapping instanceof RefModel) { + Model model = Json.mapper().convertValue(location, Model.class); + RefModel ref = (RefModel) mapping; + ref.set$ref(locationName); + swagger.addDefinition(locationName, model); + } + } + else if(target instanceof Operation) { + + // get the operation position + Operation operation = (Operation) target; + int position = 0; + for(Parameter param : operation.getParameters()) { + + if(param instanceof RefParameter) { + RefParameter ref = (RefParameter) param; + if(ref.getSimpleRef().equals(locationName)) { + // found a match! + Parameter remoteParam = Json.mapper().convertValue(location, Parameter.class); + operation.getParameters().set(position, remoteParam); + } } + position += 1; } - position += 1; } } } - } - catch(Exception e) { - // failed to get it - e.printStackTrace(); + catch(Exception e) { + // failed to get it + e.printStackTrace(); + } } } } @@ -140,16 +141,29 @@ public void detectOperationRefs() { BodyParameter bp = (BodyParameter) parameter; if(bp.getSchema() != null && bp.getSchema() instanceof RefModel) { RefModel ref = (RefModel)bp.getSchema(); - if(ref.get$ref().startsWith("http")) { - LOGGER.debug("added reference to " + ref.get$ref()); - resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, bp, "ref")); + String key = ref.get$ref(); + if(key.startsWith("http")) { + LOGGER.debug("added reference to " + key); + List m = resolutionMap.get(key); + if(m == null) { + m = new ArrayList(); + } + m.add(new ResolutionContext(ref, bp, "ref")); + resolutionMap.put(key, m); } } } else if(parameter instanceof RefParameter) { RefParameter ref = (RefParameter) parameter; - LOGGER.debug("added reference to " + ref.get$ref()); - resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, operation, "inline")); + String key = ref.get$ref(); + LOGGER.debug("added reference to " + ref); + + List m = resolutionMap.get(key); + if(m == null) { + m = new ArrayList(); + } + m.add(new ResolutionContext(ref, operation, "inline")); + resolutionMap.put(key, m); } } } @@ -160,8 +174,15 @@ else if(parameter instanceof RefParameter) { Property schema = response.getSchema(); if(schema instanceof RefProperty) { RefProperty ref = (RefProperty) schema; - if(ref.get$ref() != null && ref.get$ref().startsWith("http")) { - resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, response, "ref")); + String key = ref.get$ref(); + + if(key != null && key.startsWith("http")) { + List m = resolutionMap.get(key); + if(m == null) { + m = new ArrayList(); + } + m.add(new ResolutionContext(ref, response, "ref")); + resolutionMap.put(key, m); } } } @@ -179,18 +200,30 @@ public void detectModelRefs() { Model model = models.get(modelName); if(model instanceof RefModel) { RefModel ref = (RefModel) model; - if(ref.get$ref() != null && ref.get$ref().startsWith("http")) { - LOGGER.debug("added reference to " + ref.get$ref()); - resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, swagger.getDefinitions(), "ref")); + String key = ref.get$ref(); + if(key != null && key.startsWith("http")) { + LOGGER.debug("added reference to " + key); + List m = resolutionMap.get(key); + if(m == null) { + m = new ArrayList(); + } + m.add(new ResolutionContext(ref, swagger.getDefinitions(), "ref")); + resolutionMap.put(key, m); } } else if(model instanceof ArrayModel) { ArrayModel arrayModel = (ArrayModel) model; if(arrayModel.getItems() != null && arrayModel.getItems() instanceof RefProperty) { RefProperty ref = (RefProperty)arrayModel.getItems(); - if(ref.get$ref() != null && ref.get$ref().startsWith("http")) { - LOGGER.debug("added reference to " + ref.get$ref()); - resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, swagger.getDefinitions(), "ref")); + String key = ref.get$ref(); + if(key != null && key.startsWith("http")) { + LOGGER.debug("added reference to " + key); + List m = resolutionMap.get(key); + if(m == null) { + m = new ArrayList(); + } + m.add(new ResolutionContext(ref, swagger.getDefinitions(), "ref")); + resolutionMap.put(key, m); } } } @@ -202,18 +235,30 @@ else if(model instanceof ModelImpl) { Property property = properties.get(propertyName); if(property instanceof RefProperty) { RefProperty ref = (RefProperty)property; - if(ref.get$ref() != null && ref.get$ref().startsWith("http")) { - LOGGER.debug("added reference to " + ref.get$ref()); - resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, impl, "ref")); + String key = ref.get$ref(); + if(key != null && key.startsWith("http")) { + LOGGER.debug("added reference to " + key); + List m = resolutionMap.get(key); + if(m == null) { + m = new ArrayList(); + } + m.add(new ResolutionContext(ref, impl, "ref")); + resolutionMap.put(key, m); } } else if(property instanceof ArrayProperty) { ArrayProperty arrayProperty = (ArrayProperty) property; if(arrayProperty.getItems() != null && arrayProperty.getItems() instanceof RefProperty) { RefProperty ref = (RefProperty)arrayProperty.getItems(); - if(ref.get$ref() != null && ref.get$ref().startsWith("http")) { - LOGGER.debug("added reference to " + ref.get$ref()); - resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, arrayProperty, "ref")); + String key = ref.get$ref(); + if(key != null && key.startsWith("http")) { + LOGGER.debug("added reference to " + key); + List m = resolutionMap.get(key); + if(m == null) { + m = new ArrayList(); + } + m.add(new ResolutionContext(ref, arrayProperty, "ref")); + resolutionMap.put(key, m); } } } @@ -221,9 +266,15 @@ else if(property instanceof MapProperty) { MapProperty mp = (MapProperty) property; if(mp.getAdditionalProperties() != null && mp.getAdditionalProperties() instanceof RefProperty) { RefProperty ref = (RefProperty)mp.getAdditionalProperties(); - if(ref.get$ref() != null && ref.get$ref().startsWith("http")) { - LOGGER.debug("added reference to " + ref.get$ref()); - resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, mp, "ref")); + String key = ref.get$ref(); + if(key != null && key.startsWith("http")) { + LOGGER.debug("added reference to " + key); + List m = resolutionMap.get(key); + if(m == null) { + m = new ArrayList(); + } + m.add(new ResolutionContext(ref, mp, "ref")); + resolutionMap.put(key, m); } } } diff --git a/modules/swagger-parser/src/test/resources/issue_16.yaml b/modules/swagger-parser/src/test/resources/issue_16.yaml new file mode 100644 index 0000000000..049d118a12 --- /dev/null +++ b/modules/swagger-parser/src/test/resources/issue_16.yaml @@ -0,0 +1,21 @@ +swagger: "2.0" +info: + version: 0.0.0 + title: Simple API +paths: + /: + get: + responses: + "200": + description: OK + schema: + $ref: "#/definitions/InternshipResultModel" +definitions: + InternshipResultModel: + properties: + Review: + $ref: "#/definitions/ReviewModel" + ReviewModel: + properties: + Rating: + type: integer \ No newline at end of file diff --git a/modules/swagger-parser/src/test/scala/SwaggerReaderTest.scala b/modules/swagger-parser/src/test/scala/SwaggerReaderTest.scala index e9c0724ecd..92c2e1cbb6 100644 --- a/modules/swagger-parser/src/test/scala/SwaggerReaderTest.scala +++ b/modules/swagger-parser/src/test/scala/SwaggerReaderTest.scala @@ -31,6 +31,6 @@ class SwaggerReaderTest extends FlatSpec with Matchers { it should "read the issue 16 resource" in { val parser = new SwaggerParser() val swagger = parser.read("./src/test/resources/issue_16.yaml") - Json.prettyPrint(swagger) + // Json.prettyPrint(swagger) } } diff --git a/modules/swagger-parser/src/test/scala/SwaggerResolverTest.scala b/modules/swagger-parser/src/test/scala/SwaggerResolverTest.scala index 0dd1aed936..2aade31a06 100644 --- a/modules/swagger-parser/src/test/scala/SwaggerResolverTest.scala +++ b/modules/swagger-parser/src/test/scala/SwaggerResolverTest.scala @@ -73,33 +73,59 @@ class SwaggerResolverTest extends FlatSpec with Matchers { swagger.getDefinitions().get("Tag") should not be (null) } - it should "resolve response remote refs" in { + + it should "resolve operation parameter remote refs" in { val swagger = new Swagger() swagger.path("/fun", new Path() .get(new Operation() - .response(200, new Response() - .schema(new RefProperty("http://petstore.swagger.io/v2/swagger.json#/definitions/Tag"))))) + .parameter(new RefParameter("#/parameters/SampleParameter")))) + + swagger.parameter("SampleParameter", new QueryParameter() + .name("skip") + .property(new IntegerProperty())) + val resolved = new SwaggerResolver().resolve(swagger, null) - val response = swagger.getPaths().get("/fun").getGet().getResponses().get("200") - val ref = response.getSchema.asInstanceOf[RefProperty] - ref.get$ref() should equal("#/definitions/Tag") - swagger.getDefinitions().get("Tag") should not be (null) + val params = swagger.getPaths().get("/fun").getGet().getParameters() + params.size() should be (1) + val param = params.get(0).asInstanceOf[QueryParameter] + param.getName() should be ("skip") } - it should "resolve operation parameter remote refs" in { + it should "resolve operation body parameter remote refs" in { + val schema = new ModelImpl() + val swagger = new Swagger() swagger.path("/fun", new Path() .get(new Operation() .parameter(new RefParameter("#/parameters/SampleParameter")))) - swagger.parameter("SampleParameter", new QueryParameter() + swagger.path("/times", new Path() + .get(new Operation() + .parameter(new RefParameter("#/parameters/SampleParameter")))) + + swagger.parameter("SampleParameter", new BodyParameter() .name("skip") - .property(new IntegerProperty())) + .schema(schema)) + val resolved = new SwaggerResolver().resolve(swagger, null) val params = swagger.getPaths().get("/fun").getGet().getParameters() params.size() should be (1) - val param = params.get(0).asInstanceOf[QueryParameter] + val param = params.get(0).asInstanceOf[BodyParameter] param.getName() should be ("skip") } -} \ No newline at end of file + + it should "resolve response remote refs" in { + val swagger = new Swagger() + swagger.path("/fun", new Path() + .get(new Operation() + .response(200, new Response() + .schema(new RefProperty("http://petstore.swagger.io/v2/swagger.json#/definitions/Tag"))))) + val resolved = new SwaggerResolver().resolve(swagger, null) + val response = swagger.getPaths().get("/fun").getGet().getResponses().get("200") + val ref = response.getSchema.asInstanceOf[RefProperty] + ref.get$ref() should equal("#/definitions/Tag") + swagger.getDefinitions().get("Tag") should not be (null) + } +} + diff --git a/pom.xml b/pom.xml index f2f6280bd8..50efc503a5 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ 4.0.0 io.swagger swagger-parser-project - 1.0.4 + 1.0.5 pom swagger-parser-project