Skip to content

Commit 0d92dc3

Browse files
authored
Merge branch 'master' into master
2 parents 0b12da5 + a876a6f commit 0d92dc3

File tree

41 files changed

+2733
-1429
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2733
-1429
lines changed

README.md

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
# Swagger Parser
1+
# Swagger Parser <img src="https://raw.githubusercontent.com/swagger-api/swagger.io/wordpress/images/assets/SW-logo-clr.png" height="50" align="right">
22

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)
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)
44

55
[![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)
66

77
## Overview
8-
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.
8+
This is the Swagger Parser project, which reads OpenAPI definitions 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.
99

1010

1111
### Usage
12-
Using the swagger-parser is simple. Once included in your project, you can read a OpenAPI Specification from any location:
12+
Using the Swagger Parser is simple. Once included in your project, you can read a OpenAPI Specification from any location:
1313

1414
```java
1515
import io.swagger.v3.parser.OpenAPIV3Parser;
@@ -59,7 +59,7 @@ Manager. That's done by setting a system environment variable as such:
5959
export TRUST_ALL=true
6060
```
6161

62-
And then the swagger-parser will _ignore_ invalid certificates. Of course this is generally a bad idea, but if you're
62+
And then the Swagger Parser will _ignore_ invalid certificates. Of course this is generally a bad idea, but if you're
6363
working inside a firewall or really know what you're doing, well, there's your rope.
6464

6565
### Dealing with Let's Encrypt
@@ -99,27 +99,15 @@ 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.8-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.8-SNAPSHOT</version>
114-
</dependency>
115-
116104
```
117105

118106

119107
License
120108
-------
121109

122-
Copyright 2017 SmartBear Software
110+
Copyright 2018 SmartBear Software
123111

124112
Licensed under the Apache License, Version 2.0 (the "License");
125113
you may not use this file except in compliance with the License.
@@ -132,4 +120,3 @@ See the License for the specific language governing permissions and
132120
limitations under the License.
133121

134122
---
135-
<img src="http://swagger.io/wp-content/uploads/2016/02/logo.jpg"/>

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.8-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.8-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/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ public SwaggerParseResult convert(SwaggerDeserializationResult parse) {
139139

140140
Swagger swagger = parse.getSwagger();
141141

142+
if (swagger.getVendorExtensions() != null) {
143+
openAPI.setExtensions(convert(swagger.getVendorExtensions()));
144+
}
145+
142146
if (swagger.getExternalDocs() != null) {
143147
openAPI.setExternalDocs(convert(swagger.getExternalDocs()));
144148
}
@@ -1056,7 +1060,7 @@ public Parameter convert(io.swagger.models.parameters.Parameter v2Parameter) {
10561060

10571061
schema = a;
10581062
} else {
1059-
schema = new Schema();
1063+
schema = SchemaTypeUtil.createSchema(sp.getType(), sp.getFormat());
10601064
schema.setType(sp.getType());
10611065
schema.setFormat(sp.getFormat());
10621066

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.swagger.v3.oas.models.info.Info;
99
import io.swagger.v3.oas.models.media.ArraySchema;
1010
import io.swagger.v3.oas.models.media.ComposedSchema;
11+
import io.swagger.v3.oas.models.media.IntegerSchema;
1112
import io.swagger.v3.oas.models.media.Schema;
1213
import io.swagger.v3.oas.models.parameters.Parameter;
1314
import io.swagger.v3.oas.models.parameters.RequestBody;
@@ -88,6 +89,7 @@ public class V2ConverterTest {
8889
private static final String ISSUE_765_YAML = "issue-765.yaml";
8990
private static final String ISSUE_768_JSON = "issue-786.json";
9091
private static final String ISSUE_820_YAML = "issue-820.yaml";
92+
private static final String ISSUE_1032_YAML = "issue-1032.yaml";
9193

9294
private static final String API_BATCH_PATH = "/api/batch/";
9395
private static final String PETS_PATH = "/pets";
@@ -153,6 +155,9 @@ public class V2ConverterTest {
153155
private static final String ARRAY_VALUES = "[{\"id\":-1,\"name\":\"Marvin the Paranoid Android\"}," +
154156
"{\"id\":1000000,\"name\":\"Zaphod Beeblebrox\",\"friends\":[15]}]";
155157
private static final String SCHEMAS_A_REF = "#/components/schemas/A";
158+
private static final String UNIX_TIMESTAMP_QUERY_PARAM = "unixTimestampQuery";
159+
private static final String INTEGER_TYPE = "integer";
160+
private static final String INT64_FORMAT = "int64";
156161

157162
private static final int MAX_LENGTH = 60;
158163
private static final int REQUIRED_SIZE = 2;
@@ -164,7 +169,7 @@ public class V2ConverterTest {
164169
private static final int MIN_LENGTH = 3;
165170
private static final int NUMBER_VALUE_TWENTY = 20;
166171
private static final double MULTIPLE_OF_VALUE = 0.01D;
167-
private static final long DEFAULT_VALUE = 11L;
172+
private static final int DEFAULT_VALUE = 11;
168173
private static final int EXAMPLE_8_NUMBER = 8;
169174
private static final int EXAMPLE_42_NUMBER = 42;
170175

@@ -727,6 +732,13 @@ public void testIssue755() throws Exception {
727732
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_755_YAML);
728733
assertNotNull(oas);
729734
}
735+
736+
@Test(description = "OpenAPI v2 converter - top-level extensions should be preserved")
737+
public void testTopLevelExtensions() throws Exception {
738+
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(PARAMETER_CONVERSION_JSON);
739+
assertNotNull(oas);
740+
assertEquals((String)oas.getExtensions().get("x-some-extensions"), "hello");
741+
}
730742

731743
@Test(description = "OpenAPI v2 converter - Conversion param extensions should be preserved")
732744
public void testIssue820() throws Exception {
@@ -755,6 +767,19 @@ public void testIssue820() throws Exception {
755767
assertEquals(baz.getNullable(), Boolean.FALSE);
756768
}
757769

770+
@Test(description = "OpenAPI v2 converter - proper IntegerSchema parsing")
771+
public void testIssue1032() throws Exception {
772+
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_1032_YAML);
773+
assertNotNull(oas);
774+
Parameter unixTimestampQueryParameter = oas.getComponents().getParameters().get(UNIX_TIMESTAMP_QUERY_PARAM);
775+
assertNotNull(unixTimestampQueryParameter);
776+
Schema s = unixTimestampQueryParameter.getSchema();
777+
assertTrue((s instanceof IntegerSchema), "actual type: " + s);
778+
IntegerSchema integerSchema = (IntegerSchema) s;
779+
assertEquals(INTEGER_TYPE, integerSchema.getType());
780+
assertEquals(INT64_FORMAT, integerSchema.getFormat());
781+
}
782+
758783
private OpenAPI getConvertedOpenAPIFromJsonFile(String file) throws IOException, URISyntaxException {
759784
SwaggerConverter converter = new SwaggerConverter();
760785
String swaggerAsString = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource(file).toURI())));
@@ -764,4 +789,4 @@ private OpenAPI getConvertedOpenAPIFromJsonFile(String file) throws IOException,
764789
assertNotNull(result);
765790
return result.getOpenAPI();
766791
}
767-
}
792+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
swagger: '2.0'
2+
basePath: "/"
3+
info:
4+
version: "1"
5+
title: "x"
6+
7+
schemes:
8+
- https
9+
consumes:
10+
- application/json
11+
produces:
12+
- application/json
13+
paths:
14+
/data:
15+
get:
16+
operationId: "getData"
17+
parameters:
18+
- $ref: '#/parameters/unixTimestampQuery'
19+
responses:
20+
'403':
21+
description: Forbidden
22+
parameters:
23+
unixTimestampQuery:
24+
in: query
25+
name: unixTimestamp
26+
type: integer
27+
format: int64

modules/swagger-parser-v2-converter/src/test/resources/parameter-conversion.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"swagger": "2.0",
3+
"x-some-extensions": "hello",
34
"info": {
45
"version": "1.0",
56
"title": "x-example"

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.8-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/ResolverCache.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.databind.node.ObjectNode;
55
import io.swagger.v3.oas.models.OpenAPI;
66
import io.swagger.v3.oas.models.media.Schema;
7+
import io.swagger.v3.oas.models.parameters.Parameter;
78
import io.swagger.v3.oas.models.responses.ApiResponse;
89
import io.swagger.v3.parser.core.models.AuthorizationValue;
910
import io.swagger.v3.parser.models.RefFormat;
@@ -165,6 +166,12 @@ protected <T> void updateLocalRefs(String file, T result) {
165166
}
166167
}
167168
}
169+
if(result instanceof Parameter){
170+
Parameter parameter = (Parameter)result;
171+
if (parameter.getSchema() != null){
172+
updateLocalRefs(file,parameter.getSchema());
173+
}
174+
}
168175
if(result instanceof Schema && ((Schema)(result)).get$ref() != null) {
169176
Schema prop = (Schema) result;
170177
updateLocalRefs(file, prop);
@@ -194,11 +201,12 @@ protected String merge(String host, String ref) {
194201
if(StringUtils.isBlank(host)) {
195202
return ref;
196203
}
204+
197205
if(ref.startsWith("http:") || ref.startsWith("https:")) {
198206
// already an absolute ref
199207
return ref;
200208
}
201-
if(!host.startsWith("http:") && !host.startsWith("https:")) {
209+
if(!host.startsWith("http:") && !host.startsWith("https:") && !ref.startsWith("#/components")) {
202210
return ref;
203211
}
204212
if(ref.startsWith(".")) {

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

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +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;
29+
import java.util.Collection;
2730
import java.util.LinkedHashMap;
2831
import java.util.Map;
32+
import java.util.Objects;
2933

3034
import static io.swagger.v3.parser.util.RefUtils.computeDefinitionName;
3135
import static io.swagger.v3.parser.util.RefUtils.computeRefFormat;
36+
import static io.swagger.v3.parser.util.RefUtils.getExternalPath;
3237
import static io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat;
3338

3439
public final class ExternalRefProcessor {
@@ -177,37 +182,40 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
177182
return newRef;
178183
}
179184

180-
private void processProperties(Map<String,Schema> subProps, String file) {
181-
if (subProps != null) {
182-
for (Map.Entry<String, Schema> prop : subProps.entrySet()) {
183-
if (prop.getValue().get$ref() != null) {
184-
processRefSchema(prop.getValue(), file);
185-
} else if (prop.getValue() instanceof ArraySchema) {
186-
ArraySchema arrayProp = (ArraySchema) prop.getValue();
187-
if (arrayProp.getItems() != null && arrayProp.getItems().get$ref() != null &&
188-
StringUtils.isNotBlank(arrayProp.getItems().get$ref())) {
189-
processRefSchema(arrayProp.getItems(), file);
190-
}
191-
if (arrayProp.getItems() != null && arrayProp.getItems().getProperties() != null ) {
192-
processProperties(arrayProp.getItems().getProperties(), file);
193-
}
194-
} else if (prop.getValue().getAdditionalProperties() != null && prop.getValue().getAdditionalProperties() instanceof Schema) {
195-
Schema mapProp = (Schema) prop.getValue().getAdditionalProperties();
196-
if (mapProp.get$ref() != null) {
197-
processRefSchema(mapProp, file);
198-
} else if (mapProp.getAdditionalProperties() instanceof ArraySchema &&
199-
((ArraySchema) mapProp.getAdditionalProperties()).getItems()!= null &&
200-
((ArraySchema) mapProp.getAdditionalProperties()).getItems().get$ref() != null
201-
&& StringUtils.isNotBlank(((ArraySchema) mapProp.getAdditionalProperties()).getItems().get$ref())) {
202-
processRefSchema(((ArraySchema) mapProp.getAdditionalProperties()).getItems(), file);
203-
}
204-
}else if (prop.getValue() instanceof ObjectSchema){
205-
ObjectSchema objProp = (ObjectSchema) prop.getValue();
206-
if(objProp.getProperties() != null ){
207-
processProperties(objProp.getProperties(),file);
208-
}
209-
}
185+
private void processProperty(Schema property, String file) {
186+
if (property != null) {
187+
if (StringUtils.isNotBlank(property.get$ref())) {
188+
processRefSchema(property, file);
210189
}
190+
if (property.getProperties() != null) {
191+
processProperties(property.getProperties(), file);
192+
}
193+
if (property instanceof ArraySchema) {
194+
processProperty(((ArraySchema) property).getItems(), file);
195+
}
196+
if (property.getAdditionalProperties() instanceof Schema) {
197+
processProperty(((Schema) property.getAdditionalProperties()), file);
198+
}
199+
if (property instanceof ComposedSchema) {
200+
ComposedSchema composed = (ComposedSchema) property;
201+
processProperties(composed.getAllOf(), file);
202+
processProperties(composed.getAnyOf(), file);
203+
processProperties(composed.getOneOf(), file);
204+
}
205+
}
206+
}
207+
208+
private void processProperties(Collection<Schema> properties, String file) {
209+
if (properties != null) {
210+
for (Schema property : properties) {
211+
processProperty(property, file);
212+
}
213+
}
214+
}
215+
216+
private void processProperties(Map<String, Schema> properties, String file) {
217+
if (properties != null) {
218+
processProperties(properties.values(), file);
211219
}
212220
}
213221

@@ -685,8 +693,10 @@ private void processRefSchema(Schema subRef, String externalFile) {
685693
return;
686694
}
687695
String $ref = subRef.get$ref();
696+
String subRefExternalPath = getExternalPath(subRef.get$ref())
697+
.orElse(null);
688698

689-
if (format.equals(RefFormat.RELATIVE)) {
699+
if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
690700
$ref = constructRef(subRef, externalFile);
691701
subRef.set$ref($ref);
692702
}else {

0 commit comments

Comments
 (0)