Skip to content

Commit 7f6ed2b

Browse files
committed
Merge branch 'branch_issue_911_v1' of https://github.com/ymohdriz/swagger-parser into branch_issue_911_v1
2 parents 3c21896 + 2794e18 commit 7f6ed2b

File tree

30 files changed

+648
-19
lines changed

30 files changed

+648
-19
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
**NOTE:** If you're looking for `swagger-parser` 2.X and OpenApi 3.0, please refer to [master branch](https://github.com/swagger-api/swagger-parser)
2+
13
# Swagger Parser
24

3-
[![Build Status](https://img.shields.io/jenkins/s/https/jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-parser-master.svg)](https://jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-parser-master)
5+
[![Build Status](https://img.shields.io/jenkins/s/https/jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-parser-v1.svg)](https://jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-parser-v1)
46
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.swagger/swagger-parser/badge.svg?style=plastic)](https://maven-badges.herokuapp.com/maven-central/io.swagger/swagger-parser)
57
[![PR Stats](https://img.shields.io/github/issues-pr/swagger-api/swagger-parser.svg)](https://github.com/swagger-api/swagger-parser/pulls)
68
[![Issue Stats](https://img.shields.io/github/issues/swagger-api/swagger-parser.svg)](https://github.com/swagger-api/swagger-parser/issues)
@@ -115,7 +117,7 @@ You can include this library from Sonatype OSS for SNAPSHOTS, or Maven central f
115117
<dependency>
116118
<groupId>io.swagger</groupId>
117119
<artifactId>swagger-parser</artifactId>
118-
<version>1.0.39</version>
120+
<version>1.0.40-SNAPSHOT</version>
119121
</dependency>
120122

121123
```
@@ -125,7 +127,7 @@ To add swagger parsing support for older versions of swagger, add the `compat` m
125127
<dependency>
126128
<groupId>io.swagger</groupId>
127129
<artifactId>swagger-compat-spec-parser</artifactId>
128-
<version>1.0.39</version>
130+
<version>1.0.40-SNAPSHOT</version>
129131
</dependency>
130132

131133
```

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>io.swagger</groupId>
66
<artifactId>swagger-parser-project</artifactId>
7-
<version>1.0.39</version>
7+
<version>1.0.40-SNAPSHOT</version>
88
<relativePath>../..</relativePath>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public ResourceListing readResourceListing(String input, MessageBuilder messages
210210
jsonNode = Json.mapper().readTree(json);
211211

212212
}
213-
if (jsonNode.get("swaggerVersion") == null) {
213+
if (jsonNode == null || jsonNode.get("swaggerVersion") == null) {
214214
return null;
215215
}
216216
ResourceListingMigrator migrator = new ResourceListingMigrator();

modules/swagger-parser/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>io.swagger</groupId>
66
<artifactId>swagger-parser-project</artifactId>
7-
<version>1.0.39</version>
7+
<version>1.0.40-SNAPSHOT</version>
88
<relativePath>../..</relativePath>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.swagger.models.properties.Property;
88
import io.swagger.models.properties.RefProperty;
99
import io.swagger.models.refs.RefFormat;
10+
import io.swagger.models.refs.RefType;
1011
import io.swagger.parser.ResolverCache;
1112
import io.swagger.parser.util.RefUtils;
1213
import org.apache.commons.lang3.StringUtils;
@@ -94,7 +95,7 @@ public String processRefToExternalDefinition(String $ref, RefFormat refFormat) {
9495
if (isAnExternalRefFormat(refModel.getRefFormat())) {
9596
refModel.set$ref(processRefToExternalDefinition(refModel.get$ref(), refModel.getRefFormat()));
9697
} else {
97-
processRefToExternalDefinition(file + refModel.get$ref(), RefFormat.RELATIVE);
98+
refModel.set$ref(processRefToExternalDefinition(file + refModel.get$ref(), RefFormat.RELATIVE));
9899
}
99100

100101
}
@@ -196,7 +197,7 @@ public String processRefToExternalResponse(String $ref, RefFormat refFormat) {
196197
model = response.getResponseSchema();
197198
if (model instanceof RefModel) {
198199
RefModel refModel = (RefModel) model;
199-
if (RefUtils.isAnExternalRefFormat(refFormat)) {
200+
if (RefUtils.isAnExternalRefFormat(refModel.getRefFormat())) {
200201
processRefModel(refModel, $ref);
201202
} else {
202203
processRefToExternalDefinition(file + refModel.get$ref(), RefFormat.RELATIVE);
@@ -235,6 +236,12 @@ private void processProperties(final Map<String, Property> subProps, final Strin
235236
file);
236237
}
237238
}
239+
else if (prop.getValue() instanceof ObjectProperty){
240+
ObjectProperty objProp = (ObjectProperty) prop.getValue();
241+
if(objProp.getProperties() != null ){
242+
processProperties(objProp.getProperties(),file);
243+
}
244+
}
238245
}
239246
}
240247

@@ -244,7 +251,8 @@ private void processRefProperty(RefProperty subRef, String externalFile) {
244251
String joinedRef = join(externalFile, subRef.get$ref());
245252
subRef.set$ref(processRefToExternalDefinition(joinedRef, subRef.getRefFormat()));
246253
} else {
247-
processRefToExternalDefinition(externalFile + subRef.get$ref(), RefFormat.RELATIVE);
254+
String processRef = processRefToExternalDefinition(externalFile + subRef.get$ref(), RefFormat.RELATIVE);
255+
subRef.set$ref(RefType.DEFINITION.getInternalPrefix()+processRef);
248256
}
249257
}
250258

modules/swagger-parser/src/main/java/io/swagger/parser/processors/ModelProcessor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.swagger.models.RefModel;
88
import io.swagger.models.Swagger;
99
import io.swagger.models.properties.Property;
10+
import io.swagger.models.refs.RefType;
1011
import io.swagger.parser.ResolverCache;
1112

1213
import java.util.List;
@@ -96,7 +97,8 @@ private void processRefModel(RefModel refModel) {
9697
}
9798

9899
if (newRef != null) {
99-
refModel.set$ref(newRef);
100+
refModel.set$ref(RefType.DEFINITION.getInternalPrefix() + newRef);
101+
100102
}
101103

102104
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ public void process(URLConnection connection) {
8484
}
8585

8686
public static String cleanUrl(String url) {
87-
return url.replaceAll("\\{", "%7B").replaceAll("\\}", "%7D");
87+
return url.replaceAll("\\{", "%7B").
88+
replaceAll("\\}", "%7D").
89+
replaceAll(" ", "%20");
8890
}
8991

9092
public static String urlToString(String url, List<AuthorizationValue> auths) throws Exception {

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class SwaggerDeserializer {
3232
protected static Set<String> BODY_PARAMETER_KEYS = new LinkedHashSet<String>(Arrays.asList("name", "in", "description", "required", "schema"));
3333
protected static Set<String> SECURITY_SCHEME_KEYS = new LinkedHashSet<String>(Arrays.asList("type", "name", "in", "description", "flow", "authorizationUrl", "tokenUrl" , "scopes"));
3434

35+
private final Set<String> operationIDs = new HashSet<>();
36+
3537
public SwaggerDeserializationResult deserialize(JsonNode rootNode) {
3638
SwaggerDeserializationResult result = new SwaggerDeserializationResult();
3739
ParseResult rootParse = new ParseResult();
@@ -349,7 +351,7 @@ public Operation operation(ObjectNode obj, String location, ParseResult result)
349351
ExternalDocs docs = externalDocs(externalDocs, location, result);
350352
output.setExternalDocs(docs);
351353

352-
value = getString("operationId", obj, false, location, result);
354+
value = getString("operationId", obj, false, location, result, operationIDs);
353355
output.operationId(value);
354356

355357
array = getArray("consumes", obj, false, location, result);
@@ -522,12 +524,12 @@ else if ("formData".equals(value)) {
522524

523525
if(sp != null) {
524526
// type is mandatory when sp != null
525-
getString("type", obj, true, location, result);
527+
String paramType = getString("type", obj, true, location, result);
526528
Map<PropertyBuilder.PropertyId, Object> map = new LinkedHashMap<PropertyBuilder.PropertyId, Object>();
527529

528530
map.put(TYPE, type);
529531
map.put(FORMAT, format);
530-
String defaultValue = getString("default", obj, false, location, result);
532+
String defaultValue = parameterDefault(obj, paramType, location, result);
531533
map.put(DEFAULT, defaultValue);
532534
sp.setDefault(defaultValue);
533535

@@ -704,6 +706,15 @@ else if(!BODY_PARAMETER_KEYS.contains(key)) {
704706
return output;
705707
}
706708

709+
private String parameterDefault(ObjectNode node, String type, String location, ParseResult result) {
710+
String key = "default";
711+
if (type != null && type.equals("array")) {
712+
ArrayNode array = getArray(key, node, false, location, result);
713+
return array != null ? array.toString() : null;
714+
}
715+
return getString(key, node, false, location, result);
716+
}
717+
707718
private Property schema(Map<String, Object> schemaItems, JsonNode obj, String location, ParseResult result) {
708719
return Json.mapper().convertValue(obj, Property.class);
709720
}
@@ -816,6 +827,13 @@ public Model definition(ObjectNode node, String location, ParseResult result) {
816827
impl.setUniqueItems(bp);
817828
}
818829

830+
831+
BigDecimal bd = getBigDecimal("minimum", node, false, location, result);
832+
impl.setMinimum(bd);
833+
834+
bd = getBigDecimal("maximum", node, false, location, result);
835+
impl.setMaximum(bd);
836+
819837
bp = getBoolean("exclusiveMaximum", node, false, location, result);
820838
if(bp != null) {
821839
impl.setExclusiveMaximum(bp);
@@ -854,6 +872,7 @@ public Model definition(ObjectNode node, String location, ParseResult result) {
854872
impl.setMultipleOf(multipleOf);
855873
}
856874

875+
857876
ap = node.get("enum");
858877
if(ap != null) {
859878
ArrayNode arrayNode = getArray("enum", node, false, location, result);
@@ -1590,6 +1609,10 @@ else if(!v.isValueNode()) {
15901609
}
15911610

15921611
public String getString(String key, ObjectNode node, boolean required, String location, ParseResult result) {
1612+
return getString(key, node, required, location, result, null);
1613+
}
1614+
1615+
public String getString(String key, ObjectNode node, boolean required, String location, ParseResult result, Set<String> uniqueValues) {
15931616
String value = null;
15941617
JsonNode v = node.get(key);
15951618
if (node == null || v == null) {
@@ -1603,6 +1626,10 @@ else if(!v.isValueNode()) {
16031626
}
16041627
else {
16051628
value = v.asText();
1629+
if (uniqueValues != null && !uniqueValues.add(value)) {
1630+
result.unique(location, "operationId");
1631+
result.invalid();
1632+
}
16061633
}
16071634
return value;
16081635
}
@@ -1628,6 +1655,7 @@ protected static class ParseResult {
16281655
private Map<Location, String> invalidType = new LinkedHashMap<Location, String>();
16291656
private List<Location> warnings = new ArrayList<>();
16301657
private List<Location> missing = new ArrayList<Location>();
1658+
private List<Location> unique = new ArrayList<>();
16311659

16321660
public ParseResult() {
16331661
}
@@ -1640,6 +1668,10 @@ public void extra(String location, String key, JsonNode value) {
16401668
extra.put(new Location(location, key), value);
16411669
}
16421670

1671+
public void unique(String location, String key) {
1672+
unique.add(new Location(location, key));
1673+
}
1674+
16431675
public void missing(String location, String key) {
16441676
missing.add(new Location(location, key));
16451677
}
@@ -1718,6 +1750,11 @@ public List<String> getMessages() {
17181750
String message = "attribute " + location +l.key;
17191751
messages.add(message);
17201752
}
1753+
for (Location l : unique) {
1754+
String location = l.location.equals("") ? "" : l.location + ".";
1755+
String message = "attribute " + location + l.key + " is repeated";
1756+
messages.add(message);
1757+
}
17211758
for(Location l : unsupported.keySet()) {
17221759
String location = l.location.equals("") ? "" : l.location + ".";
17231760
String message = "attribute " + location + l.key + " is unsupported";

0 commit comments

Comments
 (0)