Skip to content

Commit 75a21d5

Browse files
authored
Merge branch 'master' into ISSUE-1745
2 parents 70106ff + 4b3863c commit 75a21d5

File tree

22 files changed

+780
-33
lines changed

22 files changed

+780
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ You can include this library from Sonatype OSS for SNAPSHOTS, or Maven central f
111111
<dependency>
112112
<groupId>io.swagger.parser.v3</groupId>
113113
<artifactId>swagger-parser</artifactId>
114-
<version>2.1.3</version>
114+
<version>2.1.8</version>
115115
</dependency>
116116
```
117117

modules/swagger-parser-cli/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>swagger-parser-project</artifactId>
77
<groupId>io.swagger.parser.v3</groupId>
8-
<version>2.1.4-SNAPSHOT</version>
8+
<version>2.1.8</version>
99
<relativePath>../..</relativePath>
1010
</parent>
1111
<modelVersion>4.0.0</modelVersion>
@@ -69,7 +69,7 @@
6969
<dependency>
7070
<groupId>io.swagger.parser.v3</groupId>
7171
<artifactId>swagger-parser-v3</artifactId>
72-
<version>2.1.4-SNAPSHOT</version>
72+
<version>2.1.8</version>
7373
<scope>compile</scope>
7474
</dependency>
7575
<dependency>

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.1.4-SNAPSHOT</version>
6+
<version>2.1.8</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.1.4-SNAPSHOT</version>
6+
<version>2.1.8</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.1.4-SNAPSHOT</version>
6+
<version>2.1.8</version>
77
<relativePath>../..</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ResolverCache.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,16 @@ else if (rootPath != null) {
180180
//a definition path is defined, meaning we need to "dig down" through the JSON tree and get the desired entity
181181
String[] jsonPathElements = definitionPath.split("/");
182182
for (String jsonPathElement : jsonPathElements) {
183-
tree = tree.get(unescapePointer(jsonPathElement));
183+
if (tree.isArray()) {
184+
try {
185+
tree = tree.get(Integer.valueOf(jsonPathElement));
186+
} catch (NumberFormatException numberFormatException) {
187+
//
188+
}
189+
} else {
190+
tree = tree.get(unescapePointer(jsonPathElement));
191+
}
192+
184193
//if at any point we do find an element we expect, print and error and abort
185194
if (tree == null) {
186195
throw new RuntimeException("Could not find " + definitionPath + " in contents of " + file);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void processOperation(Operation operation) {
5050

5151
RequestBody requestBody = operation.getRequestBody();
5252
if (requestBody != null) {
53-
// This part allows paser to put requestBody inline without the resolveFully
53+
// This part allows parser to put requestBody inline without the resolveFully
5454
// option set to true
5555
if (requestBody.get$ref() != null && cache != null && cache.getParseOptions() != null && cache.getParseOptions().isResolveRequestBody()) {
5656
requestBodyProcessor.processRequestBody(requestBody);

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/ReferenceUtils.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,17 @@ public static JsonNode jsonPointerEvaluate(String fragment, JsonNode tree, Strin
136136
String[] tokens = fragment.split("/");
137137
JsonNode node = tree;
138138
for (String token : tokens) {
139-
if (StringUtils.isNotBlank(token)) {
139+
if (StringUtils.isBlank(token)) {
140+
continue;
141+
}
142+
if (node.isArray()) {
143+
node = node.get(Integer.valueOf(token));
144+
} else {
140145
node = node.get(ReferenceUtils.unescapePointer(token));
141-
//if at any point we do find an element we expect, print and error and abort
142-
if (node == null) {
143-
throw new RuntimeException("Could not find " + fragment + " in contents of " + uri);
144-
}
146+
}
147+
//if at any point we do find an element we expect, print and error and abort
148+
if (node == null) {
149+
throw new RuntimeException("Could not find " + fragment + " in contents of " + uri);
145150
}
146151
}
147152
return node;

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.swagger.v3.oas.models.Operation;
1414
import io.swagger.v3.oas.models.PathItem;
1515
import io.swagger.v3.oas.models.Paths;
16+
import io.swagger.v3.oas.models.SpecVersion;
1617
import io.swagger.v3.oas.models.callbacks.Callback;
1718
import io.swagger.v3.oas.models.examples.Example;
1819
import io.swagger.v3.oas.models.links.Link;
@@ -313,6 +314,7 @@ public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
313314
return null;
314315
} else if (value.startsWith("3.1")) {
315316
result.openapi31(true);
317+
openAPI.setSpecVersion(SpecVersion.V31);
316318
}
317319
if (!value.startsWith("3.0.") && !value.startsWith("3.1.")){
318320
result.warning(location, "The provided definition does not specify a valid version field");
@@ -332,6 +334,10 @@ public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
332334
openAPI.setComponents(components);
333335
this.components = components;
334336
if(result.validateInternalRefs) {
337+
/* TODO currently only capable of validating if ref is to root schema withing #/components/schemas
338+
* need to evaluate json pointer instead to also allow validation of nested schemas
339+
* e.g. #/components/schemas/foo/properties/bar
340+
*/
335341
for (String schema : localSchemaRefs.keySet()) {
336342
if (components.getSchemas().get(schema) == null) {
337343
result.invalidType(localSchemaRefs.get(schema), schema, "schema", rootNode);
@@ -2763,7 +2769,11 @@ at the moment path passed as string (basePath) from upper components can be both
27632769
} else {
27642770
schema.set$ref(ref.asText());
27652771
}
2766-
if(schema.get$ref().startsWith("#/components/schemas")){// it's internal
2772+
/* TODO currently only capable of validating if ref is to root schema withing #/components/schemas
2773+
* need to evaluate json pointer instead to also allow validation of nested schemas
2774+
* e.g. #/components/schemas/foo/properties/bar
2775+
*/
2776+
if(schema.get$ref().startsWith("#/components/schemas") && StringUtils.countMatches(schema.get$ref(), "/") == 3){
27672777
String refName = schema.get$ref().substring(schema.get$ref().lastIndexOf("/")+1);
27682778
localSchemaRefs.put(refName,location);
27692779
}
@@ -2827,7 +2837,7 @@ at the moment path passed as string (basePath) from upper components can be both
28272837
schema.setType(type);
28282838
}
28292839
}
2830-
if ("array".equals(schema.getType()) && !(schema instanceof ArraySchema && ((ArraySchema) schema).getItems() != null)) {
2840+
if ("array".equals(schema.getType()) && schema.getItems() == null) {
28312841
result.missing(location, "items");
28322842
}
28332843
}

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

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.swagger.v3.oas.models.Operation;
66
import io.swagger.v3.oas.models.PathItem;
77
import io.swagger.v3.oas.models.Paths;
8+
import io.swagger.v3.oas.models.SpecVersion;
89
import io.swagger.v3.oas.models.callbacks.Callback;
910
import io.swagger.v3.oas.models.examples.Example;
1011
import io.swagger.v3.oas.models.headers.Header;
@@ -56,7 +57,7 @@ public ResolverFully(boolean aggregateCombinators) {
5657
private Map<String, Link> links;
5758
private Map<String, Schema> resolvedProperties = new IdentityHashMap<>();
5859
private Map<String, Callback> callbacks;
59-
60+
6061
public void resolveFully(OpenAPI openAPI) {
6162
Components components = openAPI.getComponents();
6263
if (components != null && components.getRequestBodies() != null) {
@@ -85,7 +86,7 @@ public void resolveFully(OpenAPI openAPI) {
8586
if (headers == null) {
8687
headers = new HashMap<>();
8788
}
88-
}
89+
}
8990

9091
if (components != null && components.getParameters() != null) {
9192
parameters = components.getParameters();
@@ -243,7 +244,7 @@ public Header resolveHeader(Header header){
243244
}
244245
return header;
245246
}
246-
247+
247248
public Link resolveLink(Link link){
248249
RefFormat refFormat = computeRefFormat(link.get$ref());
249250
String $ref = link.get$ref();
@@ -272,7 +273,7 @@ public RequestBody resolveRequestBody(RequestBody requestBody){
272273
}
273274
return requestBody;
274275
}
275-
276+
276277
public Callback resolveCallback(Callback callback){
277278
RefFormat refFormat = computeRefFormat(callback.get$ref());
278279
String $ref = callback.get$ref();
@@ -379,17 +380,30 @@ public Schema resolveSchema(Schema schema) {
379380
boolean adjacent = (hasAllOf && hasAnyOf) || (hasAllOf && hasOneOf) || (hasAnyOf && hasOneOf);
380381

381382
if (aggregateCombinators && (hasAllOf || adjacent)) {
382-
Schema combinedModel = SchemaTypeUtil.createSchema(composedSchema.getType(), composedSchema.getFormat());
383+
Schema combinedModel = null;
384+
if (SpecVersion.V30.equals(composedSchema.getSpecVersion())) {
385+
combinedModel = SchemaTypeUtil.createSchema(getSchemaType(composedSchema), composedSchema.getFormat());
386+
} else {
387+
combinedModel = new JsonSchema();
388+
combinedModel.setFormat(composedSchema.getFormat());
389+
combinedModel.setTypes(composedSchema.getTypes());
390+
}
391+
392+
// combinedModel.setDefault(composedSchema.getDefault());
383393
Set<Object> examples = new HashSet<>();
394+
Set<Object> defaultValues = new HashSet<>();
384395

385396
if (hasAllOf) {
386-
aggregateSchemaCombinators(composedSchema, combinedModel, composedSchema.getAllOf(), examples);
397+
aggregateSchemaCombinators(composedSchema, combinedModel, composedSchema.getAllOf(), examples, defaultValues);
387398
}
388399
if (hasOneOf) {
389-
aggregateSchemaCombinators(composedSchema, combinedModel, composedSchema.getOneOf(), examples);
400+
aggregateSchemaCombinators(composedSchema, combinedModel, composedSchema.getOneOf(), examples, defaultValues);
390401
}
391402
if (hasAnyOf) {
392-
aggregateSchemaCombinators(composedSchema, combinedModel, composedSchema.getAnyOf(), examples);
403+
aggregateSchemaCombinators(composedSchema, combinedModel, composedSchema.getAnyOf(), examples, defaultValues);
404+
}
405+
if (defaultValues.size() == 1) {
406+
combinedModel.setDefault(defaultValues.iterator().next());
393407
}
394408

395409
if (schema.getExample() != null) {
@@ -431,8 +445,12 @@ public Schema resolveSchema(Schema schema) {
431445
Schema property = updated.get(key);
432446

433447
if (property.getProperties() != model.getProperties()) {
434-
if (property.getType() == null) {
435-
property.setType("object");
448+
if (!hasSchemaType(property)) {
449+
if (SpecVersion.V30.equals(property.getSpecVersion())) {
450+
property.setType("object");
451+
} else {
452+
property.addType("object");
453+
}
436454
}
437455
model.addProperties(key, property);
438456
} else {
@@ -448,6 +466,22 @@ public Schema resolveSchema(Schema schema) {
448466
return result;
449467
}
450468

469+
protected String getSchemaType(Schema schema) {
470+
if (SpecVersion.V30.equals(schema.getSpecVersion())) {
471+
return schema.getType();
472+
}
473+
if (schema.getTypes() != null && schema.getTypes().size() == 1) {
474+
return (String)schema.getTypes().iterator().next();
475+
}
476+
return null;
477+
}
478+
479+
protected boolean hasSchemaType(Schema schema) {
480+
if (SpecVersion.V30.equals(schema.getSpecVersion())) {
481+
return schema.getType() != null;
482+
}
483+
return schema.getTypes() != null && schema.getTypes().size() > 0;
484+
}
451485
public Map<String,Example> resolveExample(Map<String,Example> examples){
452486

453487
Map<String,Example> resolveExamples = examples;
@@ -469,10 +503,9 @@ public Map<String,Example> resolveExample(Map<String,Example> examples){
469503
}
470504

471505
private void aggregateSchemaCombinators(ComposedSchema sourceSchema, Schema targetSchema,
472-
List<Schema> schemasToAggregate, Set<Object> examples) {
506+
List<Schema> schemasToAggregate, Set<Object> examples, Set<Object> defaultValues) {
473507

474508
Set<String> requiredProperties = new HashSet<>();
475-
476509
for (Schema innerModel : schemasToAggregate) {
477510
Schema resolved = resolveSchema(innerModel);
478511
Map<String, Schema> properties = resolved.getProperties();
@@ -496,12 +529,23 @@ private void aggregateSchemaCombinators(ComposedSchema sourceSchema, Schema targ
496529
if (resolved.getExample() != null) {
497530
examples.add(resolved.getExample());
498531
}
532+
if (sourceSchema.getDefault() != null && resolved.getDefault() == null)
533+
defaultValues.add(sourceSchema.getDefault());
534+
else
535+
defaultValues.add(resolved.getDefault());
536+
499537
if (resolved.getExtensions() != null) {
500538
Map<String, Object> extensions = resolved.getExtensions();
501539
for (String key : extensions.keySet()) {
502540
targetSchema.addExtension(key, extensions.get(key));
503541
}
504542
}
543+
if (sourceSchema.getExtensions() != null) {
544+
Map<String, Object> extensions = sourceSchema.getExtensions();
545+
for (String key : extensions.keySet()) {
546+
targetSchema.addExtension(key, sourceSchema.getExtensions().get(key));
547+
}
548+
}
505549
}
506550

507551
if (requiredProperties.size() > 0) {

0 commit comments

Comments
 (0)