Skip to content

Commit e591419

Browse files
committed
Merge pull request #72 from swagger-api/develop
Prepare for release
2 parents 7b0459c + 29ab5a5 commit e591419

File tree

12 files changed

+235
-29
lines changed

12 files changed

+235
-29
lines changed

modules/swagger-compat-spec-parser/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
<parent>
55
<groupId>io.swagger</groupId>
66
<artifactId>swagger-parser-project</artifactId>
7-
<version>1.0.8</version>
7+
<version>1.0.9</version>
88
<relativePath>../..</relativePath>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111
<groupId>io.swagger</groupId>
1212
<artifactId>swagger-compat-spec-parser</artifactId>
13-
<version>1.0.8</version>
13+
<version>1.0.9</version>
1414
<packaging>jar</packaging>
1515
<name>swagger-compat-spec-parser</name>
1616
<dependencies>

modules/swagger-compat-spec-parser/src/main/java/io/swagger/parser/SwaggerCompatConverter.java

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.swagger.parser;
22

3-
import com.fasterxml.jackson.databind.JsonNode;
4-
import com.fasterxml.jackson.databind.node.ObjectNode;
53
import io.swagger.models.ArrayModel;
64
import io.swagger.models.AuthorizationScope;
75
import io.swagger.models.Contact;
@@ -39,6 +37,7 @@
3937
import io.swagger.models.properties.Property;
4038
import io.swagger.models.properties.PropertyBuilder;
4139
import io.swagger.models.properties.RefProperty;
40+
import io.swagger.models.properties.StringProperty;
4241
import io.swagger.models.resourcelisting.ApiInfo;
4342
import io.swagger.models.resourcelisting.ApiKeyAuthorization;
4443
import io.swagger.models.resourcelisting.ApiListingReference;
@@ -60,6 +59,9 @@
6059
import java.util.List;
6160
import java.util.Map;
6261

62+
import com.fasterxml.jackson.databind.JsonNode;
63+
import com.fasterxml.jackson.databind.node.ObjectNode;
64+
6365
// legacy models
6466

6567
public class SwaggerCompatConverter implements SwaggerParserExtension {
@@ -234,6 +236,12 @@ public Parameter convertParameter(io.swagger.models.apideclaration.Parameter par
234236
p = arrayProperty;
235237
} else {
236238
p = propertyFromTypedObject(param);
239+
if (p == null) {
240+
System.out.println(String.format(
241+
"WARNING! No property detected for parameter '%s' (%s)! Falling back to string!",
242+
param.getName(), param.getParamType()));
243+
p = new StringProperty();
244+
}
237245
}
238246
if (p instanceof ArrayProperty) {
239247
ArrayProperty ap = (ArrayProperty) p;
@@ -336,15 +344,11 @@ public Property propertyFromTypedObject(ExtendedTypedObject obj) {
336344
}
337345
}
338346

339-
if (output == null) {
340-
System.out.println("WARNING! No property detected! Falling back to string!");
341-
output = PropertyBuilder.build("string", null, null);
342-
}
343-
344347
return output;
345348
}
346349

347-
public Operation convertOperation(String tag, io.swagger.models.apideclaration.Operation operation) {
350+
public Operation convertOperation(String tag, io.swagger.models.apideclaration.Operation operation,
351+
ApiDeclaration apiDeclaration) {
348352
Method method;
349353

350354
if (operation.getMethod() == null) {
@@ -366,16 +370,25 @@ public Operation convertOperation(String tag, io.swagger.models.apideclaration.O
366370
output.parameter(convertParameter(parameter));
367371
}
368372

369-
if (operation.getConsumes() != null) {
373+
if (operation.getConsumes() != null && !operation.getConsumes().isEmpty()) {
370374
for (String consumes : operation.getConsumes()) {
371375
output.consumes(consumes);
372376
}
377+
} else if (apiDeclaration.getConsumes() != null) {
378+
for (String consumes : apiDeclaration.getConsumes()) {
379+
output.consumes(consumes);
380+
}
373381
}
374-
if (operation.getProduces() != null) {
382+
if (operation.getProduces() != null && !operation.getProduces().isEmpty()) {
375383
for (String produces : operation.getProduces()) {
376384
output.produces(produces);
377385
}
386+
} else if (apiDeclaration.getProduces() != null) {
387+
for (String produces : apiDeclaration.getProduces()) {
388+
output.produces(produces);
389+
}
378390
}
391+
379392
for (ResponseMessage message : operation.getResponseMessages()) {
380393
Response response = new Response().description(message.getMessage());
381394

@@ -502,7 +515,7 @@ public Swagger convert(ResourceListing resourceListing, List<ApiDeclaration> api
502515
paths.put(apiPath, path);
503516
}
504517
for (io.swagger.models.apideclaration.Operation op : ops) {
505-
Operation operation = convertOperation(tag, op);
518+
Operation operation = convertOperation(tag, op, apiDeclaration);
506519

507520
if (op.getMethod() != null) {
508521
path.set(op.getMethod().toString().toLowerCase(), operation);
@@ -541,6 +554,7 @@ public Swagger convert(ResourceListing resourceListing, List<ApiDeclaration> api
541554
}
542555
}
543556

557+
544558
Swagger swagger = new Swagger()
545559
.host(host)
546560
.scheme(Scheme.forValue(scheme))

modules/swagger-compat-spec-parser/src/test/java/io/swagger/converter/LegacyConverterTest.java

+31
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@
44
import io.swagger.models.Info;
55
import io.swagger.models.License;
66
import io.swagger.models.Operation;
7+
import io.swagger.models.Path;
8+
import io.swagger.models.Response;
79
import io.swagger.models.Swagger;
810
import io.swagger.models.auth.ApiKeyAuthDefinition;
911
import io.swagger.models.auth.In;
1012
import io.swagger.models.auth.OAuth2Definition;
1113
import io.swagger.models.auth.SecuritySchemeDefinition;
14+
import io.swagger.models.parameters.Parameter;
15+
import io.swagger.models.parameters.PathParameter;
1216
import io.swagger.models.parameters.QueryParameter;
1317
import io.swagger.parser.SwaggerCompatConverter;
18+
1419
import org.testng.annotations.Test;
1520

21+
import com.google.common.base.Predicate;
22+
import com.google.common.collect.Iterables;
23+
24+
import java.io.IOException;
1625
import java.util.List;
1726
import java.util.Map;
1827

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

101110
assertNull(swagger);
102111
}
112+
113+
@Test
114+
public void testFixedProperties() throws IOException {
115+
final Swagger swagger = converter.read("src/test/resources/specs/v1_2/singleFile.json");
116+
final Path path = swagger.getPath("/pet/{petId}");
117+
assertEquals(path.getPost().getResponses().size(), 1);
118+
for (Response item : path.getPost().getResponses().values()) {
119+
assertNull(item.getSchema());
120+
}
121+
assertNull(path.getDelete().getResponses());
122+
final PathParameter id = (PathParameter) Iterables.find(path.getPatch().getParameters(),
123+
new Predicate<Parameter>() {
124+
125+
@Override
126+
public boolean apply(Parameter input) {
127+
return "petId".equals(input.getName());
128+
}
129+
});
130+
131+
assertEquals(id.getType(), "string");
132+
assertNull(id.getFormat());
133+
}
103134
}

modules/swagger-compat-spec-parser/src/test/java/io/swagger/converter/OperationConverterTest.java

+30-6
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
import io.swagger.models.Operation;
55
import io.swagger.models.ParamType;
66
import io.swagger.models.Response;
7+
import io.swagger.models.apideclaration.ApiDeclaration;
78
import io.swagger.models.apideclaration.ResponseMessage;
89
import io.swagger.models.properties.Property;
910
import io.swagger.models.properties.RefProperty;
1011
import io.swagger.parser.SwaggerCompatConverter;
1112
import org.testng.annotations.Test;
1213

13-
import java.util.ArrayList;
14-
import java.util.List;
14+
import java.util.*;
1515

16-
import static org.testng.Assert.assertEquals;
17-
import static org.testng.Assert.assertNotNull;
18-
import static org.testng.Assert.assertTrue;
16+
import static org.testng.Assert.*;
1917

2018
public class OperationConverterTest {
2119
SwaggerCompatConverter converter = new SwaggerCompatConverter();
@@ -56,7 +54,7 @@ public void convertOperation1() throws Exception {
5654
parameters.add(param);
5755
operation.setParameters(parameters);
5856

59-
Operation converted = converter.convertOperation("tag", operation);
57+
Operation converted = converter.convertOperation("tag", operation, new ApiDeclaration());
6058

6159
assertTrue(converted.getTags().size() == 1);
6260
assertEquals(converted.getTags().get(0), "tag");
@@ -78,4 +76,30 @@ public void convertOperation1() throws Exception {
7876
RefProperty ref = (RefProperty) property;
7977
assertEquals(ref.getSimpleRef(), "Cat");
8078
}
79+
80+
@Test
81+
public void testConvertOperation_ConsumesAndProducesInheritedFromApiDeclaration() throws Exception {
82+
Set<String> expectedConsumes = new HashSet<>(Arrays.asList("application/json", "application/xml"));
83+
Set<String> expectedProduces = new HashSet<>(Arrays.asList("text/plain"));
84+
85+
final ApiDeclaration apiDeclaration = new ApiDeclaration();
86+
apiDeclaration.setConsumes(new ArrayList<>(expectedConsumes));
87+
apiDeclaration.setProduces(new ArrayList<>(expectedProduces));
88+
89+
io.swagger.models.apideclaration.Operation operation = new io.swagger.models.apideclaration.Operation();
90+
operation.setMethod(Method.GET);
91+
92+
final SwaggerCompatConverter swaggerCompatConverter = new SwaggerCompatConverter();
93+
Operation converted = swaggerCompatConverter.convertOperation("tag", operation, apiDeclaration);
94+
95+
assertSetsAreEqual(expectedConsumes, converted.getConsumes());
96+
assertSetsAreEqual(expectedProduces, converted.getProduces());
97+
}
98+
99+
private void assertSetsAreEqual(Set<String> expectedConsumes, List<String> actualConsumes) {
100+
Set<String> actualConsumesSet = new HashSet<>();
101+
actualConsumesSet.addAll(actualConsumes);
102+
assertEquals(expectedConsumes.size(), actualConsumes.size());
103+
assertTrue(actualConsumesSet.containsAll(expectedConsumes));
104+
}
81105
}

modules/swagger-compat-spec-parser/src/test/resources/specs/v1_2/singleFile.json

-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@
119119
"name": "petId",
120120
"description": "ID of pet that needs to be fetched",
121121
"required": true,
122-
"type": "string",
123122
"paramType": "path"
124123
},
125124
{

modules/swagger-parser/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
<parent>
55
<groupId>io.swagger</groupId>
66
<artifactId>swagger-parser-project</artifactId>
7-
<version>1.0.8</version>
7+
<version>1.0.9</version>
88
<relativePath>../..</relativePath>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111
<groupId>io.swagger</groupId>
1212
<artifactId>swagger-parser</artifactId>
13-
<version>1.0.8</version>
13+
<version>1.0.9</version>
1414
<packaging>jar</packaging>
1515
<name>swagger-parser</name>
1616
<dependencies>

modules/swagger-parser/src/main/java/io/swagger/parser/SwaggerParser.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Swagger parse(String swaggerAsString, List<AuthorizationValue> auths) {
5555
Swagger output;
5656
try {
5757
output = new Swagger20Parser().parse(swaggerAsString);
58-
if (output != null && auths != null && auths.size() > 0) {
58+
if (output != null) {
5959
return new SwaggerResolver().resolve(output, auths);
6060
}
6161
} catch (IOException e) {
@@ -65,6 +65,10 @@ public Swagger parse(String swaggerAsString, List<AuthorizationValue> auths) {
6565
}
6666

6767
public Swagger read(JsonNode node) {
68+
return read(node, false);
69+
}
70+
71+
public Swagger read(JsonNode node, boolean resolve) {
6872
if (node == null) {
6973
return null;
7074
}
@@ -75,7 +79,12 @@ public Swagger read(JsonNode node) {
7579
try {
7680
output = new Swagger20Parser().read(node);
7781
if (output != null) {
78-
return output;
82+
if(resolve) {
83+
return new SwaggerResolver().resolve(output, new ArrayList<AuthorizationValue>());
84+
}
85+
else {
86+
return output;
87+
}
7988
}
8089
} catch (IOException e) {
8190
// continue;

modules/swagger-parser/src/main/java/io/swagger/parser/SwaggerResolver.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.swagger.parser;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
46
import io.swagger.models.ArrayModel;
57
import io.swagger.models.Model;
68
import io.swagger.models.ModelImpl;
@@ -19,6 +21,7 @@
1921
import io.swagger.models.properties.RefProperty;
2022
import io.swagger.parser.util.RemoteUrl;
2123
import io.swagger.util.Json;
24+
import io.swagger.util.Yaml;
2225
import org.slf4j.Logger;
2326
import org.slf4j.LoggerFactory;
2427

@@ -85,7 +88,13 @@ public void applyResolutions(List<AuthorizationValue> auths) {
8588
JsonNode location = null;
8689
String locationName = null;
8790
if (contents != null) {
88-
location = Json.mapper().readTree(contents);
91+
ObjectMapper mapper;
92+
if (contents.trim().startsWith("{")) {
93+
mapper = Json.mapper();
94+
} else {
95+
mapper = Yaml.mapper();
96+
}
97+
location = mapper.readTree(contents);
8998
String[] objectPath = definitionPath.split("/");
9099
for (String objectPathPart : objectPath) {
91100
LOGGER.debug("getting part " + objectPathPart);
@@ -198,6 +207,22 @@ public void detectOperationRefs() {
198207
resolutionMap.put(key, m);
199208
}
200209
}
210+
else if (schema instanceof ArrayProperty) {
211+
Property item = ((ArrayProperty)schema).getItems();
212+
if (item instanceof RefProperty) {
213+
RefProperty ref = (RefProperty) item;
214+
String key = ref.get$ref();
215+
216+
if (key != null && key.startsWith("http")) {
217+
List<ResolutionContext> m = resolutionMap.get(key);
218+
if (m == null) {
219+
m = new ArrayList<ResolutionContext>();
220+
}
221+
m.add(new ResolutionContext(ref, schema, "ref"));
222+
resolutionMap.put(key, m);
223+
}
224+
}
225+
}
201226
}
202227
}
203228
}

modules/swagger-parser/src/test/scala/RemoteUrlTest.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import scala.collection.JavaConverters._
99
@RunWith(classOf[JUnitRunner])
1010
class RemoteUrlTest extends FlatSpec with Matchers {
1111
it should "read a remote URL" in {
12-
val output = RemoteUrl.urlToString("http://petstore.swagger.io/v2/pet/3", null)
12+
val output = RemoteUrl.urlToString("http://petstore.swagger.io/v2/pet/1", null)
1313
output should not be (null)
1414
}
1515

1616
it should "set a header" in {
1717
val av = new AuthorizationValue("accept", "application/xml", "header")
18-
val output = RemoteUrl.urlToString("http://petstore.swagger.io/v2/pet/3", List(av).asJava)
18+
val output = RemoteUrl.urlToString("http://petstore.swagger.io/v2/pet/1", List(av).asJava)
1919
output.trim.charAt(0) should be('<')
2020
}
2121

modules/swagger-parser/src/test/scala/SwaggerReaderTest.scala

+17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import io.swagger.models.Swagger
12
import io.swagger.parser.SwaggerParser
23
import io.swagger.util.Json
4+
import org.apache.commons.io.FileUtils
35
import org.junit.runner.RunWith
46
import org.scalatest.{FlatSpec, Matchers}
57
import org.scalatest.junit.JUnitRunner
68

9+
import java.io.File
10+
import java.nio.charset.StandardCharsets
11+
712
@RunWith(classOf[JUnitRunner])
813
class SwaggerReaderTest extends FlatSpec with Matchers {
914
val m = Json.mapper()
@@ -41,4 +46,16 @@ class SwaggerReaderTest extends FlatSpec with Matchers {
4146
}"""
4247
)
4348
}
49+
50+
it should "read the issue 59 resource" in {
51+
val parser = new SwaggerParser()
52+
val sampleFilePath = "./src/test/resources/uber.json"
53+
54+
val swaggerFromFile = parser.parse(FileUtils.readFileToString(new File(sampleFilePath), StandardCharsets.UTF_8))
55+
val swaggerFromString = parser.read(sampleFilePath)
56+
57+
swaggerFromFile.isInstanceOf[Swagger] should be(true)
58+
swaggerFromString.isInstanceOf[Swagger] should be(true)
59+
swaggerFromFile should equal(swaggerFromString)
60+
}
4461
}

0 commit comments

Comments
 (0)