Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 1b1b629

Browse files
committed
Merge remote-tracking branch 'origin/master' into 2.0-OpenAPITools
2 parents 10d37b9 + aeb9def commit 1b1b629

15 files changed

Lines changed: 708 additions & 34 deletions

File tree

README.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,6 @@ You can include this library from Sonatype OSS for SNAPSHOTS, or Maven central f
126126
<artifactId>swagger-parser</artifactId>
127127
<version>2.0.10-SNAPSHOT</version>
128128
</dependency>
129-
130-
```
131-
132-
or
133-
134-
```xml
135-
<dependency>
136-
<groupId>io.swagger.parser.v3</groupId>
137-
<artifactId>swagger-parser</artifactId>
138-
<version>2.0.10-SNAPSHOT</version>
139-
</dependency>
140-
141129
```
142130

143131

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ public Parameter convert(io.swagger.models.parameters.Parameter v2Parameter) {
10601060

10611061
schema = a;
10621062
} else {
1063-
schema = new Schema();
1063+
schema = SchemaTypeUtil.createSchema(sp.getType(), sp.getFormat());
10641064
schema.setType(sp.getType());
10651065
schema.setFormat(sp.getFormat());
10661066

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

Lines changed: 20 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

@@ -769,6 +774,19 @@ public void testIssue820() throws Exception {
769774
assertEquals(baz.getNullable(), Boolean.FALSE);
770775
}
771776

777+
@Test(description = "OpenAPI v2 converter - proper IntegerSchema parsing")
778+
public void testIssue1032() throws Exception {
779+
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_1032_YAML);
780+
assertNotNull(oas);
781+
Parameter unixTimestampQueryParameter = oas.getComponents().getParameters().get(UNIX_TIMESTAMP_QUERY_PARAM);
782+
assertNotNull(unixTimestampQueryParameter);
783+
Schema s = unixTimestampQueryParameter.getSchema();
784+
assertTrue((s instanceof IntegerSchema), "actual type: " + s);
785+
IntegerSchema integerSchema = (IntegerSchema) s;
786+
assertEquals(INTEGER_TYPE, integerSchema.getType());
787+
assertEquals(INT64_FORMAT, integerSchema.getFormat());
788+
}
789+
772790
private OpenAPI getConvertedOpenAPIFromJsonFile(String file) throws IOException, URISyntaxException {
773791
SwaggerConverter converter = new SwaggerConverter();
774792
String swaggerAsString = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource(file).toURI())));
@@ -778,4 +796,4 @@ private OpenAPI getConvertedOpenAPIFromJsonFile(String file) throws IOException,
778796
assertNotNull(result);
779797
return result.getOpenAPI();
780798
}
781-
}
799+
}
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-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java

Lines changed: 8 additions & 2 deletions
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 {
@@ -73,7 +77,7 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
7377
Schema existingModel = schemas.get(possiblyConflictingDefinitionName);
7478

7579
if (existingModel != null) {
76-
LOGGER.debug("A model for " + existingModel + " already exists");
80+
LOGGER.warn("A model for " + existingModel + " already exists");
7781
if(existingModel.get$ref() != null) {
7882
// use the new model
7983
existingModel = null;
@@ -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/OpenAPIDeserializer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,8 @@ public Map<String, Parameter> getParameters(ObjectNode obj, String location, Par
13671367
return null;
13681368
}
13691369
Map<String, Parameter> parameters = new LinkedHashMap<>();
1370+
Set<String> filter = new HashSet<>();
1371+
Parameter parameter=null;
13701372

13711373
Set<String> parameterKeys = getKeys(obj);
13721374
for(String parameterName : parameterKeys) {
@@ -1379,12 +1381,13 @@ public Map<String, Parameter> getParameters(ObjectNode obj, String location, Par
13791381
if (parameterValue.getNodeType().equals(JsonNodeType.OBJECT)) {
13801382
ObjectNode parameterObj = (ObjectNode) parameterValue;
13811383
if(parameterObj != null) {
1382-
Parameter parameter = getParameter(parameterObj, String.format("%s.%s", location, parameterName), result);
1384+
parameter = getParameter(parameterObj, String.format("%s.%s", location, parameterName), result);
13831385
if (parameter != null) {
13841386
parameters.put(parameterName, parameter);
13851387
}
13861388
}
13871389
}
1390+
13881391
}
13891392
return parameters;
13901393
}
@@ -1403,6 +1406,13 @@ public List<Parameter> getParameterList(ArrayNode obj, String location, ParseRes
14031406
}
14041407
}
14051408
}
1409+
Set<String> filter = new HashSet<>();
1410+
1411+
for(Parameter param:parameters) {
1412+
if(!filter.add(param.getName()+"#"+param.getIn())) {
1413+
result.warning(location,"There are duplicate parameter values");
1414+
}
1415+
}
14061416
return parameters;
14071417
}
14081418

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

Lines changed: 36 additions & 9 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

@@ -114,6 +132,9 @@ public static String buildUrl(String rootPath, String relativePath) {
114132
if(!"".equals(rootPathParts[rootPathParts.length - 1])) {
115133
trimRoot = 1;
116134
}
135+
if("".equals(relPathParts[0])) {
136+
trimRel = 1; trimRoot = rootPathParts.length-3;
137+
}
117138
for(int i = 0; i < rootPathParts.length; i++) {
118139
if("".equals(rootPathParts[i])) {
119140
trimRel += 1;
@@ -127,7 +148,7 @@ public static String buildUrl(String rootPath, String relativePath) {
127148
trimRel += 1;
128149
}
129150
else if ("..".equals(relPathParts[i])) {
130-
trimRel += 1;
151+
trimRel += 1; trimRoot += 1;
131152
}
132153
}
133154

@@ -136,7 +157,7 @@ else if ("..".equals(relPathParts[i])) {
136157
System.arraycopy(relPathParts,
137158
trimRel,
138159
outputParts,
139-
rootPathParts.length - trimRoot + trimRel - 1,
160+
rootPathParts.length - trimRoot,
140161
relPathParts.length - trimRel);
141162

142163
return StringUtils.join(outputParts, "/");
@@ -159,7 +180,7 @@ public static String readExternalRef(String file, RefFormat refFormat, List<Auth
159180
final Path pathToUse = parentDirectory.resolve(file).normalize();
160181

161182
if(Files.exists(pathToUse)) {
162-
result = IOUtils.toString(new FileInputStream(pathToUse.toFile()), "UTF-8");
183+
result = readAll(pathToUse);
163184
} else {
164185
String url = file;
165186
if(url.contains("..")) {
@@ -170,7 +191,7 @@ public static String readExternalRef(String file, RefFormat refFormat, List<Auth
170191
final Path pathToUse2 = parentDirectory.resolve(url).normalize();
171192

172193
if(Files.exists(pathToUse2)) {
173-
result = IOUtils.toString(new FileInputStream(pathToUse2.toFile()), "UTF-8");
194+
result = readAll(pathToUse2);
174195
}
175196
}
176197
if (result == null){
@@ -186,4 +207,10 @@ public static String readExternalRef(String file, RefFormat refFormat, List<Auth
186207
return result;
187208

188209
}
210+
211+
private static String readAll(Path path) throws IOException {
212+
try (InputStream inputStream = new FileInputStream(path.toFile())) {
213+
return IOUtils.toString(inputStream, UTF_8);
214+
}
215+
}
189216
}

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/processors/ExternalRefProcessorTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
import java.util.Map;
2222

2323
import static org.hamcrest.CoreMatchers.is;
24+
import static org.hamcrest.CoreMatchers.notNullValue;
2425
import static org.junit.Assert.assertThat;
2526
import static org.testng.Assert.assertEquals;
26-
import static org.testng.AssertJUnit.assertTrue;
2727

2828

2929
public class ExternalRefProcessorTest {
@@ -137,11 +137,11 @@ public void testNestedExternalRefs(@Injectable final Schema mockedModel){
137137
times = 1;
138138
}};
139139

140-
String actualRef = new ExternalRefProcessor(cache, testedOpenAPI).processRefToExternalSchema(customerURL, refFormat);
140+
new ExternalRefProcessor(cache, testedOpenAPI).processRefToExternalSchema(customerURL, refFormat);
141141

142-
assertTrue(testedOpenAPI.getComponents().getSchemas().get("Customer") != null);
143-
assertTrue(testedOpenAPI.getComponents().getSchemas().get("Contact") != null);
144-
assertTrue(testedOpenAPI.getComponents().getSchemas().get("Address") != null);
142+
assertThat(testedOpenAPI.getComponents().getSchemas().get("Customer"), notNullValue());
143+
assertThat(testedOpenAPI.getComponents().getSchemas().get("Contact"), notNullValue());
144+
assertThat(testedOpenAPI.getComponents().getSchemas().get("Address"), notNullValue());
145145
}
146146

147147

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
import java.util.*;
5353

5454
import static com.github.tomakehurst.wiremock.client.WireMock.*;
55+
import static org.hamcrest.CoreMatchers.equalTo;
56+
import static org.hamcrest.CoreMatchers.instanceOf;
57+
import static org.hamcrest.CoreMatchers.notNullValue;
5558
import static org.junit.Assert.assertThat;
5659
import static org.testng.Assert.*;
5760

@@ -1790,6 +1793,24 @@ public void testValidationIssue() {
17901793
assertThat(result.getMessages().size(), CoreMatchers.is(0));
17911794
}
17921795

1796+
@Test
1797+
public void shouldParseExternalSchemaModelHavingReferenceToItsLocalModel() {
1798+
// given
1799+
String location = "src/test/resources/issue-1040/api.yaml";
1800+
OpenAPIV3Parser tested = new OpenAPIV3Parser();
1801+
1802+
// when
1803+
OpenAPI result = tested.read(location);
1804+
1805+
// then
1806+
Components components = result.getComponents();
1807+
Schema modelSchema = components.getSchemas().get("Value");
1808+
1809+
assertThat(modelSchema, notNullValue());
1810+
assertThat(modelSchema.getProperties().get("id"), instanceOf(Schema.class));
1811+
assertThat(((Schema) modelSchema.getProperties().get("id")).get$ref(), equalTo("#/components/schemas/ValueId"));
1812+
}
1813+
17931814
private static int getDynamicPort() {
17941815
return new Random().ints(10000, 20000).findFirst().getAsInt();
17951816
}

0 commit comments

Comments
 (0)