Skip to content

Commit 6ab7b38

Browse files
authored
Merge branch 'master' into branch_issue_915
2 parents 4ff3854 + b3e3908 commit 6ab7b38

File tree

26 files changed

+1160
-736
lines changed

26 files changed

+1160
-736
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Swagger Parser
22

3-
[![Build Status](https://img.shields.io/jenkins/s/https/jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-parser-2.0.svg)](https://jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-parser-2.0)
3+
**NOTE:** If you're looking for `swagger-parser` 1.X and OpenApi 2.0, please refer to [v1 branch](https://github.com/swagger-api/swagger-parser/tree/v1)
4+
5+
[![Build Status](https://img.shields.io/jenkins/s/https/jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-parser-v2.svg)](https://jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-parser-v2)
46

57
## Overview
68
This is the swagger parser project, which reads OpenAPI Specifications into current Java POJOs. It also provides a simple framework to add additional converters from different formats into the Swagger objects, making the entire toolchain available.
@@ -97,7 +99,7 @@ You can include this library from Sonatype OSS for SNAPSHOTS, or Maven central f
9799
<dependency>
98100
<groupId>io.swagger.parser.v3</groupId>
99101
<artifactId>swagger-parser</artifactId>
100-
<version>2.0.5</version>
102+
<version>2.0.6-SNAPSHOT</version>
101103
</dependency>
102104

103105
```
@@ -108,7 +110,7 @@ or
108110
<dependency>
109111
<groupId>io.swagger.parser.v3</groupId>
110112
<artifactId>swagger-parser</artifactId>
111-
<version>2.0.5</version>
113+
<version>2.0.6-SNAPSHOT</version>
112114
</dependency>
113115

114116
```

modules/swagger-parser-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>io.swagger.parser.v3</groupId>
55
<artifactId>swagger-parser-project</artifactId>
6-
<version>2.0.5</version>
6+
<version>2.0.6-SNAPSHOT</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

modules/swagger-parser-v2-converter/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>io.swagger.parser.v3</groupId>
55
<artifactId>swagger-parser-project</artifactId>
6-
<version>2.0.5</version>
6+
<version>2.0.6-SNAPSHOT</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

modules/swagger-parser-v3/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>io.swagger.parser.v3</groupId>
55
<artifactId>swagger-parser-project</artifactId>
6-
<version>2.0.5</version>
6+
<version>2.0.6-SNAPSHOT</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ private void processRefSchema(Schema subRef, String externalFile) {
674674
RefFormat format = computeRefFormat(subRef.get$ref());
675675

676676
if (!isAnExternalRefFormat(format)) {
677-
processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE);
677+
subRef.set$ref(processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
678678
return;
679679
}
680680
String $ref = subRef.get$ref();
@@ -723,4 +723,4 @@ else if("".equals(uri.getPath()) && !fragment.startsWith("/")) {
723723
}
724724

725725

726-
}
726+
}

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,7 @@
5252
import java.net.URI;
5353
import java.net.URISyntaxException;
5454
import java.net.URL;
55-
import java.util.ArrayList;
56-
import java.util.Arrays;
57-
import java.util.Iterator;
58-
import java.util.LinkedHashMap;
59-
import java.util.LinkedHashSet;
60-
import java.util.List;
61-
import java.util.Map;
62-
import java.util.Optional;
63-
import java.util.Set;
55+
import java.util.*;
6456
import java.util.stream.Collectors;
6557
import java.util.stream.Stream;
6658

@@ -98,6 +90,7 @@ public class OpenAPIDeserializer {
9890
private static final String PATH_PARAMETER = "path";
9991
private static final String HEADER_PARAMETER = "header";
10092

93+
private final Set<String> operationIDs = new HashSet<>();
10194

10295
public SwaggerParseResult deserialize(JsonNode rootNode) {
10396
return deserialize(rootNode, null);
@@ -300,11 +293,18 @@ public List<Tag> getTagList(ArrayNode obj, String location, ParseResult result)
300293
return null;
301294
}
302295
List<Tag> tags = new ArrayList<>();
296+
Set<String> tagsTracker = new HashSet<>();
303297
for (JsonNode item : obj) {
304298
if (item.getNodeType().equals(JsonNodeType.OBJECT)) {
305299
Tag tag = getTag((ObjectNode) item, location, result);
306300
if (tag != null) {
307301
tags.add(tag);
302+
303+
if(tagsTracker.contains((String)tag.getName())) {
304+
result.uniqueTags(location,tag.getName());
305+
}
306+
307+
tagsTracker.add(tag.getName());
308308
}
309309
}
310310
}
@@ -524,6 +524,9 @@ public Paths getPaths(ObjectNode obj, String location, ParseResult result) {
524524
if (!pathValue.getNodeType().equals(JsonNodeType.OBJECT)) {
525525
result.invalidType(location, pathName, "object", pathValue);
526526
} else {
527+
if(!pathName.startsWith("/")){
528+
result.warning(location," Resource "+pathName+ " should start with /");
529+
}
527530
ObjectNode path = (ObjectNode) pathValue;
528531
PathItem pathObj = getPathItem(path,String.format("%s.'%s'", location,pathName), result);
529532
paths.put(pathName, pathObj);
@@ -687,7 +690,7 @@ public ExternalDocumentation getExternalDocs(ObjectNode node, String location, P
687690
}
688691

689692

690-
public String getString(String key, ObjectNode node, boolean required, String location, ParseResult result) {
693+
public String getString(String key, ObjectNode node, boolean required, String location, ParseResult result, Set<String> uniqueValues) {
691694
String value = null;
692695
JsonNode v = node.get(key);
693696
if (node == null || v == null) {
@@ -699,10 +702,18 @@ public String getString(String key, ObjectNode node, boolean required, String lo
699702
result.invalidType(location, key, "string", node);
700703
} else {
701704
value = v.asText();
705+
if (uniqueValues != null && !uniqueValues.add(value)) {
706+
result.unique(location, "operationId");
707+
result.invalid();
708+
}
702709
}
703710
return value;
704711
}
705712

713+
public String getString(String key, ObjectNode node, boolean required, String location, ParseResult result) {
714+
return getString(key, node, required, location, result, null);
715+
}
716+
706717
public Set<String> getKeys(ObjectNode node) {
707718
Set<String> keys = new LinkedHashSet<>();
708719
if (node == null) {
@@ -802,6 +813,12 @@ public License getLicense(ObjectNode node, String location, ParseResult result)
802813

803814
value = getString("url", node, false, location, result);
804815
if(StringUtils.isNotBlank(value)) {
816+
try {
817+
new URL(value);
818+
}
819+
catch (Exception e) {
820+
result.warning(location,value);
821+
}
805822
license.setUrl(value);
806823
}
807824

@@ -833,6 +850,12 @@ public Contact getContact(ObjectNode node, String location, ParseResult result)
833850

834851
value = getString("url", node, false, location, result);
835852
if(StringUtils.isNotBlank(value)) {
853+
try {
854+
new URL(value);
855+
}
856+
catch (Exception e) {
857+
result.warning(location,value);
858+
}
836859
contact.setUrl(value);
837860
}
838861

@@ -1050,6 +1073,11 @@ public Link getLink(ObjectNode linkNode, String location, ParseResult result) {
10501073
link.setParameters(getLinkParameters(parametersObject, location, result));
10511074
}
10521075

1076+
String requestBody = getString("requestBody",linkNode,false,location,result);
1077+
if (requestBody!= null) {
1078+
link.setRequestBody(requestBody);
1079+
}
1080+
10531081
ObjectNode headerObject = getObject("headers",linkNode,false,location,result);
10541082
if (headerObject!= null) {
10551083
link.setHeaders(getHeaders(headerObject, location, result));
@@ -1122,14 +1150,13 @@ public Callback getCallback(ObjectNode node,String location, ParseResult result)
11221150
JsonNode ref = node.get("$ref");
11231151
if (ref != null) {
11241152
if (ref.getNodeType().equals(JsonNodeType.STRING)) {
1125-
PathItem pathItem = new PathItem();
11261153
String mungedRef = mungedRef(ref.textValue());
11271154
if (mungedRef != null) {
1128-
pathItem.set$ref(mungedRef);
1155+
callback.set$ref(mungedRef);
11291156
}else{
1130-
pathItem.set$ref(ref.textValue());
1157+
callback.set$ref(ref.textValue());
11311158
}
1132-
return callback.addPathItem(name,pathItem);
1159+
return callback;
11331160
} else {
11341161
result.invalidType(location, "$ref", "string", node);
11351162
return null;
@@ -2455,7 +2482,7 @@ public Operation getOperation(ObjectNode obj, String location, ParseResult resul
24552482
if(docs != null) {
24562483
operation.setExternalDocs(docs);
24572484
}
2458-
value = getString("operationId", obj, false, location, result);
2485+
value = getString("operationId", obj, false, location, result, operationIDs);
24592486
if (StringUtils.isNotBlank(value)) {
24602487
operation.operationId(value);
24612488
}
@@ -2670,6 +2697,9 @@ protected static class ParseResult {
26702697
private Map<Location, JsonNode> unsupported = new LinkedHashMap<>();
26712698
private Map<Location, String> invalidType = new LinkedHashMap<>();
26722699
private List<Location> missing = new ArrayList<>();
2700+
private List<Location> warnings = new ArrayList<>();
2701+
private List<Location> unique = new ArrayList<>();
2702+
private List<Location> uniqueTags = new ArrayList<>();
26732703

26742704
public ParseResult() {
26752705
}
@@ -2685,6 +2715,17 @@ public void extra(String location, String key, JsonNode value) {
26852715
public void missing(String location, String key) {
26862716
missing.add(new Location(location, key));
26872717
}
2718+
2719+
public void warning(String location, String key) {
2720+
warnings.add(new Location(location, key));
2721+
}
2722+
2723+
public void unique(String location, String key) {
2724+
unique.add(new Location(location, key));
2725+
2726+
}
2727+
2728+
public void uniqueTags(String location, String key) {uniqueTags.add(new Location(location,key));}
26882729

26892730
public void invalidType(String location, String key, String expectedType, JsonNode value) {
26902731
invalidType.put(new Location(location, key), expectedType);
@@ -2715,11 +2756,26 @@ public List<String> getMessages() {
27152756
String message = "attribute " + location + l.key + " is missing";
27162757
messages.add(message);
27172758
}
2759+
for (Location l : warnings) {
2760+
String location = l.location.equals("") ? "" : l.location + ".";
2761+
String message = "attribute " + location +l.key;
2762+
messages.add(message);
2763+
}
27182764
for (Location l : unsupported.keySet()) {
27192765
String location = l.location.equals("") ? "" : l.location + ".";
27202766
String message = "attribute " + location + l.key + " is unsupported";
27212767
messages.add(message);
27222768
}
2769+
for (Location l : unique) {
2770+
String location = l.location.equals("") ? "" : l.location + ".";
2771+
String message = "attribute " + location + l.key + " is repeated";
2772+
messages.add(message);
2773+
}
2774+
for (Location l : uniqueTags) {
2775+
String location = l.location.equals("") ? "" : l.location + ".";
2776+
String message = "attribute " + location + l.key + " is repeated";
2777+
messages.add(message);
2778+
}
27232779
return messages;
27242780
}
27252781
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ public void process(URLConnection connection) {
9494
public static String cleanUrl(String url) {
9595
String result = null;
9696
try {
97-
result =url.replaceAll("\\{", "%7B").replaceAll("\\}", "%7D");
97+
result =url.replaceAll("\\{", "%7B").
98+
replaceAll("\\}", "%7D").
99+
replaceAll(" ", "%20");
98100
}catch (Exception t){
99101
t.printStackTrace();
100102
}

0 commit comments

Comments
 (0)