Skip to content

Commit 73715ca

Browse files
authored
Merge branch 'master' into issues-1777-1802
2 parents 4785352 + 280e6c4 commit 73715ca

File tree

23 files changed

+548
-78
lines changed

23 files changed

+548
-78
lines changed

README.md

+1-1
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.6</version>
114+
<version>2.1.9</version>
115115
</dependency>
116116
```
117117

modules/swagger-parser-cli/pom.xml

+2-2
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.7-SNAPSHOT</version>
8+
<version>2.1.10-SNAPSHOT</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.7-SNAPSHOT</version>
72+
<version>2.1.10-SNAPSHOT</version>
7373
<scope>compile</scope>
7474
</dependency>
7575
<dependency>

modules/swagger-parser-core/pom.xml

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

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

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

modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java

+24-9
Original file line numberDiff line numberDiff line change
@@ -861,8 +861,8 @@ public ApiResponse convert(io.swagger.models.Response v2Response, List<String> p
861861

862862
response.setDescription(v2Response.getDescription());
863863

864-
if (v2Response.getSchema() != null) {
865-
Schema schema = convertFileSchema(convert(v2Response.getSchema()));
864+
if (v2Response.getResponseSchema() != null) {
865+
Schema schema = convertFileSchema(convert(v2Response.getResponseSchema()));
866866
for (String type : mediaTypes) {
867867
// TODO: examples
868868
MediaType mediaType = new MediaType();
@@ -930,6 +930,9 @@ private Schema convert(Property schema) {
930930
if (schema == null) {
931931
return null;
932932
}
933+
if (schema.getBooleanValue() != null) {
934+
return new Schema().booleanSchemaValue(schema.getBooleanValue());
935+
}
933936
Schema result;
934937

935938
if (schema instanceof RefProperty) {
@@ -961,8 +964,7 @@ private Schema convert(Property schema) {
961964
result = arraySchema;
962965

963966
} else if (schema instanceof FileProperty) {
964-
FileSchema fileSchema = Json.mapper().convertValue(schema, FileSchema.class);
965-
result = fileSchema;
967+
result = Json.mapper().convertValue(schema, FileSchema.class);
966968

967969
}else {
968970

@@ -982,7 +984,11 @@ private Schema convert(Property schema) {
982984
if (schema instanceof MapProperty) {
983985
MapProperty map = (MapProperty) schema;
984986

985-
result.setAdditionalProperties(convert(map.getAdditionalProperties()));
987+
if (map.getAdditionalProperties().getBooleanValue() != null) {
988+
result.setAdditionalProperties(map.getAdditionalProperties().getBooleanValue());
989+
} else {
990+
result.setAdditionalProperties(convert(map.getAdditionalProperties()));
991+
}
986992
result.setMinProperties(map.getMinProperties());
987993
result.setMaxProperties(map.getMaxProperties());
988994
}
@@ -1173,6 +1179,9 @@ public Schema convert(io.swagger.models.Model v2Model) {
11731179
if (v2Model == null) {
11741180
return null;
11751181
}
1182+
if (v2Model.getBooleanValue() != null) {
1183+
return new Schema().booleanSchemaValue(v2Model.getBooleanValue());
1184+
}
11761185
Schema result;
11771186

11781187
if (v2Model instanceof ArrayModel) {
@@ -1208,16 +1217,22 @@ public Schema convert(io.swagger.models.Model v2Model) {
12081217
v2discriminator = model.getDiscriminator();
12091218
model.setDiscriminator(null);
12101219
}
1211-
1212-
result = Json.mapper().convertValue(v2Model, Schema.class);
1213-
1220+
if (v2Model instanceof ModelImpl && ("file".equals(((ModelImpl)v2Model).getType()))) {
1221+
result = Json.mapper().convertValue(v2Model, FileSchema.class);
1222+
} else {
1223+
result = Json.mapper().convertValue(v2Model, Schema.class);
1224+
}
12141225
addProperties(v2Model, result);
12151226

12161227
if (v2Model instanceof ModelImpl) {
12171228
ModelImpl model = (ModelImpl) v2Model;
12181229

12191230
if (model.getAdditionalProperties() != null) {
1220-
result.setAdditionalProperties(convert(model.getAdditionalProperties()));
1231+
if (model.getAdditionalProperties().getBooleanValue() != null) {
1232+
result.setAdditionalProperties(model.getAdditionalProperties().getBooleanValue());
1233+
} else {
1234+
result.setAdditionalProperties(convert(model.getAdditionalProperties()));
1235+
}
12211236
}
12221237
} else if(v2Model instanceof RefModel) {
12231238
RefModel ref = (RefModel) v2Model;

modules/swagger-parser-v2-converter/src/test/java/io/swagger/parser/test/V2ConverterTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ public void testIssue745() throws Exception {
645645
OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_745_YAML);
646646
assertTrue(oas.getServers().get(0).getUrl().startsWith("//"));
647647
}
648-
648+
649649
@Test(description = "OpenAPIParser.readLocation fails when fetching valid Swagger 2.0 resource with AuthorizationValues provided")
650650
public void testIssue785() {
651651
AuthorizationValue apiKey = new AuthorizationValue("api_key", "special-key", "header");
@@ -676,7 +676,7 @@ public void testIssue758() throws Exception {
676676
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_758_JSON);
677677
assertNotNull(oas);
678678
}
679-
679+
680680
@Test(description = "OpenAPI v2 Converter: NPE when type is array and 'items' field is missing in array property")
681681
public void testIssue762() throws Exception {
682682
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_762_JSON);
@@ -733,7 +733,7 @@ public void testSwaggerParseResultHasMessage() throws Exception {
733733
assertNotNull(result.getMessages());
734734
}
735735

736-
736+
737737
@Test(description = "OpenAPI v2 converter - Migrate minLength, maxLength and pattern of String property")
738738
public void testIssue786() throws Exception {
739739
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_768_JSON);
@@ -753,7 +753,7 @@ public void testTopLevelExtensions() throws Exception {
753753
assertNotNull(oas);
754754
assertEquals((String)oas.getExtensions().get("x-some-extensions"), "hello");
755755
}
756-
756+
757757
@Test(description = "OpenAPI v2 converter - Conversion param extensions should be preserved")
758758
public void testIssue820() throws Exception {
759759
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_820_YAML);

modules/swagger-parser-v3/pom.xml

+1-1
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.7-SNAPSHOT</version>
6+
<version>2.1.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/PathsProcessor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ public void processPaths() {
6060
for (String pathStr : pathMap.keySet()) {
6161
PathItem pathItem = pathMap.get(pathStr);
6262

63-
addParametersToEachOperation(pathItem);
64-
6563
if (pathItem.get$ref() != null) {
6664

6765
PathItem resolvedPath = processReferencePath(pathItem);
@@ -76,6 +74,8 @@ public void processPaths() {
7674
}
7775
}
7876

77+
addParametersToEachOperation(pathItem);
78+
7979
//at this point we can process this path
8080
final List<Parameter> processedPathParameters = parameterProcessor.processParameters(pathItem.getParameters());
8181
pathItem.setParameters(processedPathParameters);

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

+11-1
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
}

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

+15-42
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.FileInputStream;
1212
import java.io.IOException;
1313
import java.io.InputStream;
14+
import java.net.URL;
1415
import java.nio.file.Files;
1516
import java.nio.file.Path;
1617
import java.util.List;
@@ -150,48 +151,20 @@ public static String readExternalClasspathRef(String file, RefFormat refFormat,
150151
}
151152

152153
public static String buildUrl(String rootPath, String relativePath) {
153-
String[] rootPathParts = rootPath.split("/");
154-
String [] relPathParts = relativePath.split("/");
155-
156-
if(rootPath == null || relativePath == null) {
157-
return null;
158-
}
159-
160-
int trimRoot = 0;
161-
int trimRel = 0;
162-
163-
if(!"".equals(rootPathParts[rootPathParts.length - 1])) {
164-
trimRoot = 1;
165-
}
166-
if("".equals(relPathParts[0])) {
167-
trimRel = 1; trimRoot = rootPathParts.length-3;
168-
}
169-
for(int i = 0; i < rootPathParts.length; i++) {
170-
if("".equals(rootPathParts[i])) {
171-
trimRel += 1;
172-
}
173-
else {
174-
break;
175-
}
176-
}
177-
for(int i = 0; i < relPathParts.length; i ++) {
178-
if(".".equals(relPathParts[i])) {
179-
trimRel += 1;
180-
}
181-
else if ("..".equals(relPathParts[i])) {
182-
trimRel += 1; trimRoot += 1;
183-
}
184-
}
185-
186-
String [] outputParts = new String[rootPathParts.length + relPathParts.length - trimRoot - trimRel];
187-
System.arraycopy(rootPathParts, 0, outputParts, 0, rootPathParts.length - trimRoot);
188-
System.arraycopy(relPathParts,
189-
trimRel,
190-
outputParts,
191-
rootPathParts.length - trimRoot,
192-
relPathParts.length - trimRel);
193-
194-
return StringUtils.join(outputParts, "/");
154+
if(rootPath == null || relativePath == null) {
155+
return null;
156+
}
157+
158+
try {
159+
int until = rootPath.lastIndexOf("/")+1;
160+
String root = rootPath.substring(0, until);
161+
URL rootUrl = new URL(root);
162+
URL finalUrl = new URL(rootUrl, relativePath);
163+
return finalUrl.toString();
164+
}
165+
catch(Exception e) {
166+
throw new RuntimeException(e);
167+
}
195168
}
196169

197170
public static String readExternalRef(String file, RefFormat refFormat, List<AuthorizationValue> auths,

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

+32-3
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;
@@ -379,7 +380,15 @@ 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+
383392
// combinedModel.setDefault(composedSchema.getDefault());
384393
Set<Object> examples = new HashSet<>();
385394
Set<Object> defaultValues = new HashSet<>();
@@ -454,8 +463,12 @@ public Schema resolveSchema(Schema schema) {
454463
Schema property = updated.get(key);
455464

456465
if (property.getProperties() != model.getProperties()) {
457-
if (property.getType() == null) {
458-
property.setType("object");
466+
if (!hasSchemaType(property)) {
467+
if (SpecVersion.V30.equals(property.getSpecVersion())) {
468+
property.setType("object");
469+
} else {
470+
property.addType("object");
471+
}
459472
}
460473
model.addProperties(key, property);
461474
} else {
@@ -471,6 +484,22 @@ public Schema resolveSchema(Schema schema) {
471484
return result;
472485
}
473486

487+
protected String getSchemaType(Schema schema) {
488+
if (SpecVersion.V30.equals(schema.getSpecVersion())) {
489+
return schema.getType();
490+
}
491+
if (schema.getTypes() != null && schema.getTypes().size() == 1) {
492+
return (String)schema.getTypes().iterator().next();
493+
}
494+
return null;
495+
}
496+
497+
protected boolean hasSchemaType(Schema schema) {
498+
if (SpecVersion.V30.equals(schema.getSpecVersion())) {
499+
return schema.getType() != null;
500+
}
501+
return schema.getTypes() != null && schema.getTypes().size() > 0;
502+
}
474503
public Map<String,Example> resolveExample(Map<String,Example> examples){
475504

476505
Map<String,Example> resolveExamples = examples;

0 commit comments

Comments
 (0)