Skip to content

Commit a7ae6fa

Browse files
authored
Merge branch 'master' into feature/duplicate-example-warning
2 parents e58eb59 + c121b44 commit a7ae6fa

File tree

22 files changed

+455
-1364
lines changed

22 files changed

+455
-1364
lines changed

README.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,8 @@ You can include this library from Sonatype OSS for SNAPSHOTS, or Maven central f
9999
<dependency>
100100
<groupId>io.swagger.parser.v3</groupId>
101101
<artifactId>swagger-parser</artifactId>
102-
<version>2.0.9-SNAPSHOT</version>
102+
<version>2.0.10-SNAPSHOT</version>
103103
</dependency>
104-
105-
```
106-
107-
or
108-
109-
```xml
110-
<dependency>
111-
<groupId>io.swagger.parser.v3</groupId>
112-
<artifactId>swagger-parser</artifactId>
113-
<version>2.0.9-SNAPSHOT</version>
114-
</dependency>
115-
116104
```
117105

118106

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.9-SNAPSHOT</version>
6+
<version>2.0.10-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.9-SNAPSHOT</version>
6+
<version>2.0.10-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.9-SNAPSHOT</version>
6+
<version>2.0.10-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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@
2020
import io.swagger.v3.parser.ResolverCache;
2121
import io.swagger.v3.parser.models.RefFormat;
2222
import io.swagger.v3.parser.models.RefType;
23+
import io.swagger.v3.parser.util.RefUtils;
24+
2325
import org.apache.commons.lang3.StringUtils;
2426
import org.slf4j.LoggerFactory;
2527

2628
import java.net.URI;
2729
import java.util.Collection;
2830
import java.util.LinkedHashMap;
2931
import java.util.Map;
32+
import java.util.Objects;
3033

3134
import static io.swagger.v3.parser.util.RefUtils.computeDefinitionName;
3235
import static io.swagger.v3.parser.util.RefUtils.computeRefFormat;
36+
import static io.swagger.v3.parser.util.RefUtils.getExternalPath;
3337
import static io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat;
3438

3539
public final class ExternalRefProcessor {
@@ -689,8 +693,10 @@ private void processRefSchema(Schema subRef, String externalFile) {
689693
return;
690694
}
691695
String $ref = subRef.get$ref();
696+
String subRefExternalPath = getExternalPath(subRef.get$ref())
697+
.orElse(null);
692698

693-
if (format.equals(RefFormat.RELATIVE)) {
699+
if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
694700
$ref = constructRef(subRef, externalFile);
695701
subRef.set$ref($ref);
696702
}else {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ public Schema modelFromProperty(ArraySchema object, @SuppressWarnings("unused")
422422
public Schema modelFromProperty(ObjectSchema object, String path) {
423423
String description = object.getDescription();
424424
String example = null;
425+
List<String> requiredList = object.getRequired();
426+
425427

426428
Object obj = object.getExample();
427429
if (obj != null) {
@@ -437,6 +439,7 @@ public Schema modelFromProperty(ObjectSchema object, String path) {
437439
model.setName(name);
438440
model.setXml(xml);
439441
model.setType(object.getType());
442+
model.setRequired(requiredList);
440443

441444
if (properties != null) {
442445
flattenProperties(properties, path);

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

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ private boolean isPathParamDefined(String pathParam, List<Parameter> parameters)
560560
return false;
561561
} else {
562562
Parameter pathParamDefined = parameters.stream()
563-
.filter(parameter -> pathParam.equals(parameter.getName()) && "path".equals(parameter.getIn()))
563+
.filter(parameter -> (parameter.get$ref() != null) || (pathParam.equals(parameter.getName()) && "path".equals(parameter.getIn())))
564564
.findFirst()
565565
.orElse(null);
566566
if (pathParamDefined == null) {
@@ -1657,6 +1657,7 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu
16571657
return header;
16581658
}
16591659

1660+
16601661
public Object getAnyExample(String nodeKey,ObjectNode node, String location, ParseResult result ){
16611662
JsonNode example = node.get(nodeKey);
16621663
if (example != null) {
@@ -1673,7 +1674,6 @@ public Object getAnyExample(String nodeKey,ObjectNode node, String location, Par
16731674
BigDecimal bigDecimalExample = getBigDecimal(nodeKey, node, false, location, result);
16741675
if (bigDecimalExample != null) {
16751676
return bigDecimalExample;
1676-
16771677
}
16781678
}
16791679
} else if (example.getNodeType().equals(JsonNodeType.OBJECT)) {
@@ -1686,6 +1686,11 @@ public Object getAnyExample(String nodeKey,ObjectNode node, String location, Par
16861686
if (arrayValue != null) {
16871687
return arrayValue;
16881688
}
1689+
} else if (example.getNodeType().equals(JsonNodeType.BOOLEAN)){
1690+
Boolean bool = getBoolean(nodeKey,node,false,location,result);
1691+
if (bool != null){
1692+
return bool;
1693+
}
16891694
}
16901695
}
16911696
return null;
@@ -2250,12 +2255,41 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
22502255
schema.setFormat(value);
22512256
}
22522257

2253-
value = getString("default", node, false, location, result);
2254-
if (StringUtils.isNotBlank(value)) {
2255-
schema.setDefault(value);
2258+
//sets default value according to the schema type
2259+
if(node.get("default")!= null) {
2260+
if(schema.getType().equals("array")) {
2261+
ArrayNode array = getArray("default", node, false, location, result);
2262+
if (array != null) {
2263+
schema.setDefault(array);
2264+
}
2265+
}else if(schema.getType().equals("string")) {
2266+
value = getString("default", node, false, location, result);
2267+
if (value != null) {
2268+
schema.setDefault(value);
2269+
}
2270+
}else if(schema.getType().equals("boolean")) {
2271+
bool = getBoolean("default", node, false, location, result);
2272+
if (bool != null) {
2273+
schema.setDefault(bool);
2274+
}
2275+
}else if(schema.getType().equals("object")) {
2276+
Object object = getObject("default", node, false, location, result);
2277+
if (object != null) {
2278+
schema.setDefault(object);
2279+
}
2280+
} else if(schema.getType().equals("integer")) {
2281+
Integer number = getInteger("default", node, false, location, result);
2282+
if (number != null) {
2283+
schema.setDefault(number);
2284+
}
2285+
} else if(schema.getType().equals("number")) {
2286+
BigDecimal number = getBigDecimal("default", node, false, location, result);
2287+
if (number != null) {
2288+
schema.setDefault(number);
2289+
}
2290+
}
22562291
}
22572292

2258-
//discriminator
22592293

22602294
bool = getBoolean("nullable", node, false, location, result);
22612295
if(bool != null) {

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@
55
import org.apache.commons.io.IOUtils;
66
import org.apache.commons.lang3.StringUtils;
77

8-
8+
import static java.nio.charset.StandardCharsets.UTF_8;
99

1010
import java.io.FileInputStream;
11+
import java.io.IOException;
12+
import java.io.InputStream;
1113
import java.nio.file.Files;
1214
import java.nio.file.Path;
1315
import java.util.List;
14-
import java.util.Set;
16+
import java.util.Optional;
1517

1618
public class RefUtils {
1719

20+
private static final String REFERENCE_SEPARATOR = "#/";
21+
22+
private RefUtils() {
23+
// static access only
24+
}
25+
1826
public static String computeDefinitionName(String ref) {
1927

20-
final String[] refParts = ref.split("#/");
28+
final String[] refParts = ref.split(REFERENCE_SEPARATOR);
2129

2230
if (refParts.length > 2) {
2331
throw new RuntimeException("Invalid ref format: " + ref);
@@ -43,6 +51,16 @@ public static String computeDefinitionName(String ref) {
4351
return plausibleName;
4452
}
4553

54+
public static Optional<String> getExternalPath(String ref) {
55+
if (ref == null) {
56+
return Optional.empty();
57+
}
58+
return Optional.of(ref.split(REFERENCE_SEPARATOR))
59+
.filter(it -> it.length == 2)
60+
.map(it -> it[0])
61+
.filter(it -> !it.isEmpty());
62+
}
63+
4664
public static boolean isAnExternalRefFormat(RefFormat refFormat) {
4765
return refFormat == RefFormat.URL || refFormat == RefFormat.RELATIVE;
4866
}
@@ -52,9 +70,9 @@ public static RefFormat computeRefFormat(String ref) {
5270
ref = mungedRef(ref);
5371
if(ref.startsWith("http")||ref.startsWith("https")) {
5472
result = RefFormat.URL;
55-
} else if(ref.startsWith("#/")) {
73+
} else if(ref.startsWith(REFERENCE_SEPARATOR)) {
5674
result = RefFormat.INTERNAL;
57-
} else if(ref.startsWith(".") || ref.startsWith("/") || ref.indexOf("#/") > 0) {
75+
} else if(ref.startsWith(".") || ref.startsWith("/") || ref.indexOf(REFERENCE_SEPARATOR) > 0) {
5876
result = RefFormat.RELATIVE;
5977
}
6078

@@ -159,7 +177,7 @@ public static String readExternalRef(String file, RefFormat refFormat, List<Auth
159177
final Path pathToUse = parentDirectory.resolve(file).normalize();
160178

161179
if(Files.exists(pathToUse)) {
162-
result = IOUtils.toString(new FileInputStream(pathToUse.toFile()), "UTF-8");
180+
result = readAll(pathToUse);
163181
} else {
164182
String url = file;
165183
if(url.contains("..")) {
@@ -170,7 +188,7 @@ public static String readExternalRef(String file, RefFormat refFormat, List<Auth
170188
final Path pathToUse2 = parentDirectory.resolve(url).normalize();
171189

172190
if(Files.exists(pathToUse2)) {
173-
result = IOUtils.toString(new FileInputStream(pathToUse2.toFile()), "UTF-8");
191+
result = readAll(pathToUse2);
174192
}
175193
}
176194
if (result == null){
@@ -186,4 +204,10 @@ public static String readExternalRef(String file, RefFormat refFormat, List<Auth
186204
return result;
187205

188206
}
207+
208+
private static String readAll(Path path) throws IOException {
209+
try (InputStream inputStream = new FileInputStream(path.toFile())) {
210+
return IOUtils.toString(inputStream, UTF_8);
211+
}
212+
}
189213
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.ArrayList;
2525
import java.util.HashMap;
2626
import java.util.HashSet;
27+
import java.util.IdentityHashMap;
2728
import java.util.LinkedHashMap;
2829
import java.util.List;
2930
import java.util.Map;
@@ -56,7 +57,7 @@ public ResolverFully(boolean aggregateCombinators) {
5657
private Map<String, RequestBody> requestBodies;
5758
private Map<String, Header> headers;
5859
private Map<String, Link> links;
59-
private Map<String, Schema> resolvedProperties = new HashMap<>();
60+
private Map<String, Schema> resolvedProperties = new IdentityHashMap<>();
6061

6162
public void resolveFully(OpenAPI openAPI) {
6263
Components components = openAPI.getComponents();

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.github.tomakehurst.wiremock.client.WireMock;
99
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
1010
import io.swagger.v3.core.util.Json;
11-
import io.swagger.v3.core.util.Yaml;
1211
import io.swagger.v3.oas.models.Components;
1312
import io.swagger.v3.oas.models.OpenAPI;
1413
import io.swagger.v3.oas.models.Operation;
@@ -657,7 +656,6 @@ public void referringSpecWithoutComponentsTag() throws Exception {
657656
ParseOptions resolve = new ParseOptions();
658657
resolve.setResolveFully(true);
659658
final OpenAPI openAPI = new OpenAPIV3Parser().read("./ref-without-component/a.yaml", null, resolve);
660-
661659
Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
662660
Assert.assertEquals("Example value", schemas.get("CustomerType").getExample());
663661
}
@@ -1166,6 +1164,23 @@ public void recursiveResolving2() {
11661164
}
11671165
}
11681166

1167+
@Test
1168+
public void recursiveIssue984() {
1169+
ParseOptions parseOptions = new ParseOptions();
1170+
parseOptions.setResolve(true);
1171+
parseOptions.setResolveFully(true);
1172+
OpenAPI openAPI = new OpenAPIV3Parser().read("issue-984-simple.yaml", null, parseOptions);
1173+
if (openAPI == null) fail("failed parsing issue-984");
1174+
try {
1175+
Json.pretty(openAPI);
1176+
//System.out.println(Json.pretty(openAPI));
1177+
}
1178+
catch (Exception e) {
1179+
e.printStackTrace();
1180+
fail("Recursive loop found");
1181+
}
1182+
}
1183+
11691184
@Test
11701185
public void propertyNameMixup() {
11711186
ParseOptions parseOptions = new ParseOptions();

0 commit comments

Comments
 (0)