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
4 changes: 2 additions & 2 deletions modules/swagger-compat-spec-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<parent>
<groupId>io.swagger</groupId>
<artifactId>swagger-parser-project</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>io.swagger</groupId>
<artifactId>swagger-compat-spec-parser</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<packaging>jar</packaging>
<name>swagger-compat-spec-parser</name>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.swagger.parser;

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;
Expand Down Expand Up @@ -39,6 +37,7 @@
import io.swagger.models.properties.Property;
import io.swagger.models.properties.PropertyBuilder;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import io.swagger.models.resourcelisting.ApiInfo;
import io.swagger.models.resourcelisting.ApiKeyAuthorization;
import io.swagger.models.resourcelisting.ApiListingReference;
Expand All @@ -60,6 +59,9 @@
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

// legacy models

public class SwaggerCompatConverter implements SwaggerParserExtension {
Expand Down Expand Up @@ -234,6 +236,12 @@ public Parameter convertParameter(io.swagger.models.apideclaration.Parameter par
p = arrayProperty;
} else {
p = propertyFromTypedObject(param);
if (p == null) {
System.out.println(String.format(
"WARNING! No property detected for parameter '%s' (%s)! Falling back to string!",
param.getName(), param.getParamType()));
p = new StringProperty();
}
}
if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
Expand Down Expand Up @@ -336,15 +344,11 @@ public Property propertyFromTypedObject(ExtendedTypedObject obj) {
}
}

if (output == null) {
System.out.println("WARNING! No property detected! Falling back to string!");
output = PropertyBuilder.build("string", null, null);
}

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) {
Method method;

if (operation.getMethod() == null) {
Expand All @@ -366,16 +370,25 @@ public Operation convertOperation(String tag, io.swagger.models.apideclaration.O
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());

Expand Down Expand Up @@ -502,7 +515,7 @@ public Swagger convert(ResourceListing resourceListing, List<ApiDeclaration> api
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);
Expand Down Expand Up @@ -541,6 +554,7 @@ public Swagger convert(ResourceListing resourceListing, List<ApiDeclaration> api
}
}


Swagger swagger = new Swagger()
.host(host)
.scheme(Scheme.forValue(scheme))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@
import io.swagger.models.Info;
import io.swagger.models.License;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Response;
import io.swagger.models.Swagger;
import io.swagger.models.auth.ApiKeyAuthDefinition;
import io.swagger.models.auth.In;
import io.swagger.models.auth.OAuth2Definition;
import io.swagger.models.auth.SecuritySchemeDefinition;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.parameters.PathParameter;
import io.swagger.models.parameters.QueryParameter;
import io.swagger.parser.SwaggerCompatConverter;

import org.testng.annotations.Test;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

import java.io.IOException;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -100,4 +109,26 @@ public void failConversionTest() throws Exception {

assertNull(swagger);
}

@Test
public void testFixedProperties() throws IOException {
final Swagger swagger = converter.read("src/test/resources/specs/v1_2/singleFile.json");
final Path path = swagger.getPath("/pet/{petId}");
assertEquals(path.getPost().getResponses().size(), 1);
for (Response item : path.getPost().getResponses().values()) {
assertNull(item.getSchema());
}
assertNull(path.getDelete().getResponses());
final PathParameter id = (PathParameter) Iterables.find(path.getPatch().getParameters(),
new Predicate<Parameter>() {

@Override
public boolean apply(Parameter input) {
return "petId".equals(input.getName());
}
});

assertEquals(id.getType(), "string");
assertNull(id.getFormat());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
import io.swagger.models.Operation;
import io.swagger.models.ParamType;
import io.swagger.models.Response;
import io.swagger.models.apideclaration.ApiDeclaration;
import io.swagger.models.apideclaration.ResponseMessage;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.RefProperty;
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;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.*;

public class OperationConverterTest {
SwaggerCompatConverter converter = new SwaggerCompatConverter();
Expand Down Expand Up @@ -56,7 +54,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");
Expand All @@ -78,4 +76,30 @@ public void convertOperation1() throws Exception {
RefProperty ref = (RefProperty) property;
assertEquals(ref.getSimpleRef(), "Cat");
}

@Test
public void testConvertOperation_ConsumesAndProducesInheritedFromApiDeclaration() throws Exception {
Set<String> expectedConsumes = new HashSet<>(Arrays.asList("application/json", "application/xml"));
Set<String> 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.setMethod(Method.GET);

final SwaggerCompatConverter swaggerCompatConverter = new SwaggerCompatConverter();
Operation converted = swaggerCompatConverter.convertOperation("tag", operation, apiDeclaration);

assertSetsAreEqual(expectedConsumes, converted.getConsumes());
assertSetsAreEqual(expectedProduces, converted.getProduces());
}

private void assertSetsAreEqual(Set<String> expectedConsumes, List<String> actualConsumes) {
Set<String> actualConsumesSet = new HashSet<>();
actualConsumesSet.addAll(actualConsumes);
assertEquals(expectedConsumes.size(), actualConsumes.size());
assertTrue(actualConsumesSet.containsAll(expectedConsumes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
"name": "petId",
"description": "ID of pet that needs to be fetched",
"required": true,
"type": "string",
"paramType": "path"
},
{
Expand Down
4 changes: 2 additions & 2 deletions modules/swagger-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<parent>
<groupId>io.swagger</groupId>
<artifactId>swagger-parser-project</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>io.swagger</groupId>
<artifactId>swagger-parser</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<packaging>jar</packaging>
<name>swagger-parser</name>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Swagger parse(String swaggerAsString, List<AuthorizationValue> auths) {
Swagger output;
try {
output = new Swagger20Parser().parse(swaggerAsString);
if (output != null && auths != null && auths.size() > 0) {
if (output != null) {
return new SwaggerResolver().resolve(output, auths);
}
} catch (IOException e) {
Expand All @@ -65,6 +65,10 @@ public Swagger parse(String swaggerAsString, List<AuthorizationValue> auths) {
}

public Swagger read(JsonNode node) {
return read(node, false);
}

public Swagger read(JsonNode node, boolean resolve) {
if (node == null) {
return null;
}
Expand All @@ -75,7 +79,12 @@ public Swagger read(JsonNode node) {
try {
output = new Swagger20Parser().read(node);
if (output != null) {
return output;
if(resolve) {
return new SwaggerResolver().resolve(output, new ArrayList<AuthorizationValue>());
}
else {
return output;
}
}
} catch (IOException e) {
// continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.swagger.parser;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.swagger.models.ArrayModel;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
Expand All @@ -19,6 +21,7 @@
import io.swagger.models.properties.RefProperty;
import io.swagger.parser.util.RemoteUrl;
import io.swagger.util.Json;
import io.swagger.util.Yaml;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -85,7 +88,13 @@ public void applyResolutions(List<AuthorizationValue> auths) {
JsonNode location = null;
String locationName = null;
if (contents != null) {
location = Json.mapper().readTree(contents);
ObjectMapper mapper;
if (contents.trim().startsWith("{")) {
mapper = Json.mapper();
} else {
mapper = Yaml.mapper();
}
location = mapper.readTree(contents);
String[] objectPath = definitionPath.split("/");
for (String objectPathPart : objectPath) {
LOGGER.debug("getting part " + objectPathPart);
Expand Down Expand Up @@ -198,6 +207,22 @@ public void detectOperationRefs() {
resolutionMap.put(key, m);
}
}
else if (schema instanceof ArrayProperty) {
Property item = ((ArrayProperty)schema).getItems();
if (item instanceof RefProperty) {
RefProperty ref = (RefProperty) item;
String key = ref.get$ref();

if (key != null && key.startsWith("http")) {
List<ResolutionContext> m = resolutionMap.get(key);
if (m == null) {
m = new ArrayList<ResolutionContext>();
}
m.add(new ResolutionContext(ref, schema, "ref"));
resolutionMap.put(key, m);
}
}
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions modules/swagger-parser/src/test/scala/RemoteUrlTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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('<')
}

Expand Down
17 changes: 17 additions & 0 deletions modules/swagger-parser/src/test/scala/SwaggerReaderTest.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import io.swagger.models.Swagger
import io.swagger.parser.SwaggerParser
import io.swagger.util.Json
import org.apache.commons.io.FileUtils
import org.junit.runner.RunWith
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.junit.JUnitRunner

import java.io.File
import java.nio.charset.StandardCharsets

@RunWith(classOf[JUnitRunner])
class SwaggerReaderTest extends FlatSpec with Matchers {
val m = Json.mapper()
Expand Down Expand Up @@ -41,4 +46,16 @@ class SwaggerReaderTest extends FlatSpec with Matchers {
}"""
)
}

it should "read the issue 59 resource" in {
val parser = new SwaggerParser()
val sampleFilePath = "./src/test/resources/uber.json"

val swaggerFromFile = parser.parse(FileUtils.readFileToString(new File(sampleFilePath), StandardCharsets.UTF_8))
val swaggerFromString = parser.read(sampleFilePath)

swaggerFromFile.isInstanceOf[Swagger] should be(true)
swaggerFromString.isInstanceOf[Swagger] should be(true)
swaggerFromFile should equal(swaggerFromString)
}
}
Loading