Skip to content

Commit 4827198

Browse files
committed
fixed merge error
2 parents a79d6e9 + 7cf86b1 commit 4827198

File tree

16 files changed

+179
-80
lines changed

16 files changed

+179
-80
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
<parent>
44
<groupId>io.swagger</groupId>
55
<artifactId>swagger-parser-project</artifactId>
6-
<version>1.0.2</version>
6+
<version>1.0.3</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010
<groupId>io.swagger</groupId>
1111
<artifactId>swagger-compat-spec-parser</artifactId>
12-
<version>1.0.2</version>
12+
<version>1.0.3</version>
1313
<packaging>jar</packaging>
1414
<name>swagger-compat-spec-parser</name>
1515
<dependencies>

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public ResourceListing readResourceListing(String input, MessageBuilder messages
124124
else {
125125
jsonNode = Json.mapper().readTree(new File(input));
126126
}
127+
if(jsonNode.get("swaggerVersion") == null)
128+
return null;
127129
ResourceListingMigrator migrator = new ResourceListingMigrator();
128130
JsonNode transformed = migrator.migrate(messages, jsonNode);
129131
output = Json.mapper().convertValue(transformed, ResourceListing.class);
@@ -314,10 +316,14 @@ else if(items.getRef() != null) {
314316
Property i = PropertyBuilder.build(type, format, args);
315317
if(i != null)
316318
output = i;
317-
else if(obj.getRef() != null)
318-
output = new RefProperty(obj.getRef());
319-
else
320-
output = new RefProperty(type);
319+
else {
320+
if(obj.getRef() != null)
321+
output = new RefProperty(obj.getRef());
322+
else if(type != null)
323+
output = new RefProperty(type);
324+
else
325+
output = new RefProperty("void");
326+
}
321327
}
322328

323329
return output;

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

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

3-
import com.fasterxml.jackson.databind.JsonNode;
3+
import com.wordnik.swagger.util.Json;
4+
45
import io.swagger.deserializer.ApiDeclarationDeserializer;
56
import io.swagger.deserializer.ResourceListingDeserializer;
67
import io.swagger.io.Authentication;
@@ -18,6 +19,8 @@
1819
import io.swagger.validate.ApiDeclarationJsonValidator;
1920
import io.swagger.validate.ResourceListingJsonValidator;
2021

22+
import com.fasterxml.jackson.databind.JsonNode;
23+
2124
import java.net.URI;
2225
import java.net.URISyntaxException;
2326

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,11 @@ public void convertSingleFile() throws Exception {
8686
assertEquals(_enum.get(1), "pending");
8787
assertEquals(_enum.get(2), "sold");
8888
}
89-
}
89+
90+
@Test
91+
public void failConversionTest() throws Exception {
92+
Swagger swagger = converter.read("src/test/resources/specs/v1_2/empty.json");
93+
94+
assertNull(swagger);
95+
}
96+
}

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

-23
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
"method": "DELETE",
189189
"summary": "Deletes a pet",
190190
"notes": "",
191-
"type": "void",
191+
"type": null,
192192
"nickname": "deletePet",
193193
"authorizations": {
194194
"oauth2": [
@@ -206,12 +206,6 @@
206206
"type": "string",
207207
"paramType": "path"
208208
}
209-
],
210-
"responseMessages": [
211-
{
212-
"code": 400,
213-
"message": "Invalid pet value"
214-
}
215209
]
216210
}
217211
]

modules/swagger-parser/pom.xml

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
<parent>
44
<groupId>io.swagger</groupId>
55
<artifactId>swagger-parser-project</artifactId>
6-
<version>1.0.2</version>
6+
<version>1.0.3</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010
<groupId>io.swagger</groupId>
1111
<artifactId>swagger-parser</artifactId>
12-
<version>1.0.2</version>
12+
<version>1.0.3</version>
1313
<packaging>jar</packaging>
1414
<name>swagger-parser</name>
1515
<dependencies>
@@ -40,5 +40,10 @@
4040
<artifactId>slf4j-api</artifactId>
4141
<version>${slf4j-version}</version>
4242
</dependency>
43+
<dependency>
44+
<groupId>commons-io</groupId>
45+
<artifactId>commons-io</artifactId>
46+
<version>${commons-io-version}</version>
47+
</dependency>
4348
</dependencies>
4449
</project>

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

+18-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.fasterxml.jackson.databind.JsonNode;
1010
import com.fasterxml.jackson.databind.ObjectMapper;
1111

12+
import org.apache.commons.io.FileUtils;
13+
1214
import java.io.File;
1315
import java.io.IOException;
1416
import java.net.URL;
@@ -19,20 +21,25 @@ public Swagger read(String location, List<AuthorizationValue> auths) throws IOEx
1921
System.out.println("reading from " + location);
2022

2123
try {
22-
// TODO make smarter
23-
ObjectMapper mapper = location.toLowerCase().endsWith(".yaml") ?
24-
Yaml.mapper() :
25-
Json.mapper();
26-
24+
ObjectMapper mapper = null;
2725
JsonNode rootNode = null;
26+
String data = null;
2827

29-
if(location.toLowerCase().startsWith("http")) {
30-
String json = RemoteUrl.urlToString(location, auths);
31-
rootNode = mapper.readTree(json);
32-
}
33-
else {
34-
rootNode = mapper.readTree(new File(location));
28+
if(location.toLowerCase().startsWith("http"))
29+
data = RemoteUrl.urlToString(location, auths);
30+
else
31+
data = FileUtils.readFileToString(new File(location), "UTF-8");
32+
33+
if(data != null) {
34+
if(data.trim().startsWith("{"))
35+
mapper = Json.mapper();
36+
else
37+
mapper = Yaml.mapper();
3538
}
39+
else
40+
return null;
41+
42+
rootNode = mapper.readTree(data);
3643

3744
// must have swagger node set
3845
JsonNode swaggerNode = rootNode.get("swagger");

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

+25-23
Original file line numberDiff line numberDiff line change
@@ -197,33 +197,35 @@ else if(model instanceof ArrayModel) {
197197
else if(model instanceof ModelImpl) {
198198
ModelImpl impl = (ModelImpl) model;
199199
Map<String, Property> properties = impl.getProperties();
200-
for(String propertyName : properties.keySet()) {
201-
Property property = properties.get(propertyName);
202-
if(property instanceof RefProperty) {
203-
RefProperty ref = (RefProperty)property;
204-
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
205-
LOGGER.debug("added reference to " + ref.get$ref());
206-
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, impl, "ref"));
207-
}
208-
}
209-
else if(property instanceof ArrayProperty) {
210-
ArrayProperty arrayProperty = (ArrayProperty) property;
211-
if(arrayProperty.getItems() != null && arrayProperty.getItems() instanceof RefProperty) {
212-
RefProperty ref = (RefProperty)arrayProperty.getItems();
200+
if(properties != null) {
201+
for(String propertyName : properties.keySet()) {
202+
Property property = properties.get(propertyName);
203+
if(property instanceof RefProperty) {
204+
RefProperty ref = (RefProperty)property;
213205
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
214206
LOGGER.debug("added reference to " + ref.get$ref());
215-
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, arrayProperty, "ref"));
207+
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, impl, "ref"));
216208
}
217209
}
218-
}
219-
else if(property instanceof MapProperty) {
220-
MapProperty mp = (MapProperty) property;
221-
if(mp.getAdditionalProperties() != null && mp.getAdditionalProperties() instanceof RefProperty) {
222-
RefProperty ref = (RefProperty)mp.getAdditionalProperties();
223-
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
224-
LOGGER.debug("added reference to " + ref.get$ref());
225-
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, mp, "ref"));
226-
}
210+
else if(property instanceof ArrayProperty) {
211+
ArrayProperty arrayProperty = (ArrayProperty) property;
212+
if(arrayProperty.getItems() != null && arrayProperty.getItems() instanceof RefProperty) {
213+
RefProperty ref = (RefProperty)arrayProperty.getItems();
214+
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
215+
LOGGER.debug("added reference to " + ref.get$ref());
216+
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, arrayProperty, "ref"));
217+
}
218+
}
219+
}
220+
else if(property instanceof MapProperty) {
221+
MapProperty mp = (MapProperty) property;
222+
if(mp.getAdditionalProperties() != null && mp.getAdditionalProperties() instanceof RefProperty) {
223+
RefProperty ref = (RefProperty)mp.getAdditionalProperties();
224+
if(ref.get$ref() != null && ref.get$ref().startsWith("http")) {
225+
LOGGER.debug("added reference to " + ref.get$ref());
226+
resolutionMap.put(ref.get$ref(), new ResolutionContext(ref, mp, "ref"));
227+
}
228+
}
227229
}
228230
}
229231
}

modules/swagger-parser/src/main/java/io/swagger/parser/util/RemoteUrl.java

+43-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,49 @@
55
import java.net.*;
66
import java.io.*;
77
import java.util.*;
8+
import java.security.*;
9+
import javax.net.ssl.*;
10+
import java.security.cert.X509Certificate;
811

912
public class RemoteUrl {
13+
static {
14+
disableSslVerification();
15+
}
16+
17+
private static void disableSslVerification() {
18+
try {
19+
// Create a trust manager that does not validate certificate chains
20+
TrustManager[] trustAllCerts = new TrustManager[] {
21+
new X509TrustManager() {
22+
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
23+
return null;
24+
}
25+
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
26+
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
27+
}
28+
};
29+
30+
// Install the all-trusting trust manager
31+
SSLContext sc = SSLContext.getInstance("SSL");
32+
sc.init(null, trustAllCerts, new java.security.SecureRandom());
33+
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
34+
35+
// Create all-trusting host name verifier
36+
HostnameVerifier allHostsValid = new HostnameVerifier() {
37+
public boolean verify(String hostname, SSLSession session) {
38+
return true;
39+
}
40+
};
41+
42+
// Install the all-trusting host verifier
43+
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
44+
} catch (NoSuchAlgorithmException e) {
45+
e.printStackTrace();
46+
} catch (KeyManagementException e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
1051
public static String urlToString(String url, List<AuthorizationValue> auths) throws Exception {
1152
InputStream is = null;
1253
URLConnection conn = null;
@@ -44,9 +85,9 @@ public static String urlToString(String url, List<AuthorizationValue> auths) thr
4485

4586
String line;
4687
is = conn.getInputStream();
47-
br = new BufferedReader(new InputStreamReader(is));
88+
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
4889
while ((line = br.readLine()) != null) {
49-
sb.append(line);
90+
sb.append(line).append("\n");
5091
}
5192
return sb.toString();
5293
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"swagger": "2.0",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "Pets Store"
6+
},
7+
"paths": {
8+
"/pets": {
9+
"get": {
10+
"parameters": [],
11+
"responses": {
12+
"200": {
13+
"description": "Returns all the pets"
14+
}
15+
}
16+
}
17+
}
18+
}
19+
}

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ import scala.collection.JavaConverters._
1414
class RemoteUrlTest extends FlatSpec with Matchers {
1515
it should "read a remote URL" in {
1616
val output = RemoteUrl.urlToString("http://petstore.swagger.io/v2/pet/3", null)
17-
println(output)
17+
output should not be (null)
1818
}
19+
1920
it should "set a header" in {
2021
val av = new AuthorizationValue("accept", "application/xml", "header")
2122
val output = RemoteUrl.urlToString("http://petstore.swagger.io/v2/pet/3", List(av).asJava)
22-
println(output)
23+
output.trim.charAt(0) should be ('<')
24+
}
25+
26+
it should "read yaml" in {
27+
val output = RemoteUrl.urlToString("http://petstore.swagger.io/v2/swagger.yaml", null)
28+
output.indexOf("swagger: \"2.0\"") should be > (0)
2329
}
2430
}

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,23 @@ import org.scalatest.FlatSpec
88
import org.scalatest.Matchers
99

1010
@RunWith(classOf[JUnitRunner])
11-
class SwaggerReaderTestTest extends FlatSpec with Matchers {
11+
class SwaggerReaderTest extends FlatSpec with Matchers {
1212
val m = Json.mapper()
1313

1414
it should "read the uber api" in {
1515
val parser = new SwaggerParser()
1616
val swagger = parser.read("./src/test/resources/uber.json")
1717
}
18+
19+
it should "detect yaml" in {
20+
val parser = new SwaggerParser()
21+
val swagger = parser.read("./src/test/resources/minimal_y")
22+
swagger.getSwagger() should be ("2.0")
23+
}
24+
25+
it should "detect json" in {
26+
val parser = new SwaggerParser()
27+
val swagger = parser.read("./src/test/resources/minimal_y")
28+
swagger.getSwagger() should be ("2.0")
29+
}
1830
}

0 commit comments

Comments
 (0)