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

Commit 53a5164

Browse files
committed
Merge remote-tracking branch 'origin/master' into 2.0-OpenAPITools
Conflicts: README.md
2 parents b588035 + 64c6edb commit 53a5164

File tree

31 files changed

+719
-90
lines changed

31 files changed

+719
-90
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,13 @@ You can include this library from Sonatype OSS for SNAPSHOTS, or Maven central f
124124
<dependency>
125125
<groupId>io.swagger.parser.v3</groupId>
126126
<artifactId>swagger-parser</artifactId>
127-
<version>2.0.12</version>
127+
<version>2.0.13</version>
128128
</dependency>
129129
```
130130

131+
## Security contact
132+
133+
Please disclose any security-related issues or vulnerabilities by emailing [[email protected]](mailto:[email protected]), instead of using the public issue tracker.
131134

132135
License
133136
-------

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import java.util.ArrayList;
7272
import java.util.Arrays;
7373
import java.util.HashMap;
74+
import java.util.LinkedHashMap;
7475
import java.util.List;
7576
import java.util.Map;
7677
import java.util.Optional;
@@ -419,7 +420,7 @@ private List<Server> convert(List<Scheme> schemes, String host, String basePath)
419420
servers.add(server);
420421
}
421422
} else {
422-
if (!"/".equals(baseUrl)) {
423+
if (!StringUtils.startsWith(baseUrl, "/") && !"/".equals(baseUrl)) {
423424
baseUrl = "//" + baseUrl;
424425
}
425426
Server server = new Server();
@@ -921,7 +922,7 @@ private Schema convert(Property schema) {
921922
result.setExample(schema.getExample());
922923

923924
if ("object".equals(schema.getType()) && (result.getProperties() != null) && (result.getProperties().size() > 0)) {
924-
Map<String, Schema> properties = new HashMap<>();
925+
Map<String, Schema> properties = new LinkedHashMap<>();
925926

926927
((ObjectProperty) schema).getProperties().forEach((k, v) -> properties.put(k, convert(v)));
927928

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public class V2ConverterTest {
9090
private static final String ISSUE_768_JSON = "issue-786.json";
9191
private static final String ISSUE_820_YAML = "issue-820.yaml";
9292
private static final String ISSUE_1032_YAML = "issue-1032.yaml";
93+
private static final String ISSUE_1113_YAML = "issue-1113.yaml";
9394

9495
private static final String API_BATCH_PATH = "/api/batch/";
9596
private static final String PETS_PATH = "/pets";
@@ -786,6 +787,16 @@ public void testIssue1032() throws Exception {
786787
assertEquals(INTEGER_TYPE, integerSchema.getType());
787788
assertEquals(INT64_FORMAT, integerSchema.getFormat());
788789
}
790+
791+
@Test(description = "OpenAPI v2 converter - converts URL correctly when it begins with forward slash")
792+
public void testIssue1113() throws Exception {
793+
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_1113_YAML);
794+
assertNotNull(oas);
795+
assertNotNull(oas.getServers());
796+
assertFalse(oas.getServers().isEmpty());
797+
assertNotNull(oas.getServers().get(0));
798+
assertEquals(oas.getServers().get(0).getUrl(), "/test");
799+
}
789800

790801
private OpenAPI getConvertedOpenAPIFromJsonFile(String file) throws IOException, URISyntaxException {
791802
SwaggerConverter converter = new SwaggerConverter();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
swagger: '2.0'
2+
info:
3+
title: Test for Issue 1113
4+
version: 1.0.0
5+
basePath: /test
6+
paths:
7+
/ping:
8+
get:
9+
summary: test
10+
description: 'test'
11+
operationId: pingOp
12+
responses:
13+
'200':
14+
description: OK

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,13 @@ public void processSchemas(Set<String> schemaKeys, Map<String, Schema> schemas)
221221

222222
schemaProcessor.processSchema(model);
223223

224-
//if we process a RefModel here, in the #/definitions table, we want to overwrite it with the referenced value
224+
//if we process a RefModel here, in the #/components/schemas table, we want to overwrite it with the referenced value
225225
if (model.get$ref() != null) {
226226
final String renamedRef = cache.getRenamedRef(originalRef);
227227

228228
if (renamedRef != null) {
229-
//we definitely resolved the referenced and shoved it in the definitions map
230-
// because the referenced model may be in the definitions map, we need to remove old instances
229+
//we definitely resolved the referenced and shoved it in the components map
230+
// because the referenced model may be in the components map, we need to remove old instances
231231
final Schema resolvedModel = schemas.get(renamedRef);
232232

233233
// ensure the reference isn't still in use

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
182182
return newRef;
183183
}
184184

185-
private void processProperty(Schema property, String file) {
185+
private void processSchema(Schema property, String file) {
186186
if (property != null) {
187187
if (StringUtils.isNotBlank(property.get$ref())) {
188188
processRefSchema(property, file);
@@ -191,10 +191,10 @@ private void processProperty(Schema property, String file) {
191191
processProperties(property.getProperties(), file);
192192
}
193193
if (property instanceof ArraySchema) {
194-
processProperty(((ArraySchema) property).getItems(), file);
194+
processSchema(((ArraySchema) property).getItems(), file);
195195
}
196196
if (property.getAdditionalProperties() instanceof Schema) {
197-
processProperty(((Schema) property.getAdditionalProperties()), file);
197+
processSchema(((Schema) property.getAdditionalProperties()), file);
198198
}
199199
if (property instanceof ComposedSchema) {
200200
ComposedSchema composed = (ComposedSchema) property;
@@ -208,7 +208,7 @@ private void processProperty(Schema property, String file) {
208208
private void processProperties(Collection<Schema> properties, String file) {
209209
if (properties != null) {
210210
for (Schema property : properties) {
211-
processProperty(property, file);
211+
processSchema(property, file);
212212
}
213213
}
214214
}
@@ -269,6 +269,8 @@ public String processRefToExternalResponse(String $ref, RefFormat refFormat) {
269269
} else {
270270
processRefToExternalSchema(file + schema.get$ref(), RefFormat.RELATIVE);
271271
}
272+
}else{
273+
processSchema(schema,file);
272274
}
273275
}
274276
}

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import io.swagger.v3.oas.models.examples.Example;
5+
import io.swagger.v3.oas.models.media.Content;
56
import io.swagger.v3.oas.models.media.MediaType;
67
import io.swagger.v3.oas.models.media.Schema;
78
import io.swagger.v3.oas.models.parameters.Parameter;
@@ -142,9 +143,43 @@ public List<Parameter> processParameters(List<Parameter> parameters) {
142143
Schema schema = parameter.getSchema();
143144
if(schema != null){
144145
schemaProcessor.processSchema(schema);
146+
}else if(parameter.getContent() != null){
147+
Map<String,MediaType> content = parameter.getContent();
148+
for( String mediaName : content.keySet()) {
149+
MediaType mediaType = content.get(mediaName);
150+
if(mediaType.getSchema()!= null) {
151+
schema = mediaType.getSchema();
152+
if (schema != null) {
153+
schemaProcessor.processSchema(schema);
154+
}
155+
}
156+
if(mediaType.getExamples() != null) {
157+
for(Example ex: mediaType.getExamples().values()){
158+
exampleProcessor.processExample(ex);
159+
}
160+
}
161+
}
162+
163+
}
164+
else if(parameter.getContent() != null){
165+
Map<String,MediaType> content = parameter.getContent();
166+
for( String mediaName : content.keySet()) {
167+
MediaType mediaType = content.get(mediaName);
168+
if(mediaType.getSchema()!= null) {
169+
schema = mediaType.getSchema();
170+
if (schema != null) {
171+
schemaProcessor.processSchema(schema);
172+
}
173+
}
174+
if(mediaType.getExamples() != null) {
175+
for(Example ex: mediaType.getExamples().values()){
176+
exampleProcessor.processExample(ex);
177+
}
178+
}
179+
}
145180
}
146181
}
147182

148183
return processedPathLevelParameters;
149184
}
150-
}
185+
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public void processPaths() {
7575
final List<Parameter> processedPathParameters = parameterProcessor.processParameters(pathItem.getParameters());
7676
pathItem.setParameters(processedPathParameters);
7777

78-
//addParametersToEachOperation(pathItem);
7978

8079
final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
8180

@@ -162,6 +161,11 @@ protected void updateLocalRefs(PathItem path, String pathRef) {
162161
}
163162

164163
protected void updateLocalRefs(ApiResponse response, String pathRef) {
164+
if (response.get$ref() != null){
165+
if(isLocalRef(response.get$ref())) {
166+
response.set$ref(computeLocalRef(response.get$ref(), pathRef));
167+
}
168+
}
165169
if(response.getContent() != null) {
166170
Map<String, MediaType> content = response.getContent();
167171
for (String key: content.keySet()) {
@@ -188,6 +192,11 @@ protected void updateLocalRefs(Example example, String pathRef) {
188192
}
189193

190194
protected void updateLocalRefs(Parameter param, String pathRef) {
195+
if (param.get$ref() != null){
196+
if(isLocalRef(param.get$ref())) {
197+
param.set$ref(computeLocalRef(param.get$ref(), pathRef));
198+
}
199+
}
191200
if(param.getSchema() != null) {
192201
updateLocalRefs(param.getSchema(), pathRef);
193202
}
@@ -204,13 +213,24 @@ protected void updateLocalRefs(Parameter param, String pathRef) {
204213
}
205214

206215
protected void updateLocalRefs(RequestBody body, String pathRef) {
216+
if (body.get$ref() != null){
217+
if(isLocalRef(body.get$ref())) {
218+
body.set$ref(computeLocalRef(body.get$ref(), pathRef));
219+
}
220+
}
207221
if(body.getContent() != null) {
208222
Map<String, MediaType> content = body.getContent();
209223
for (String key: content.keySet()) {
210224
MediaType mediaType = content.get(key);
211225
if (mediaType.getSchema() != null) {
212226
updateLocalRefs(mediaType.getSchema(), pathRef);
213227
}
228+
Map<String, Example> examples = content.get(key).getExamples();
229+
if (examples != null) {
230+
for (Example example : examples.values()) {
231+
updateLocalRefs(example, pathRef);
232+
}
233+
}
214234
}
215235
}else if(body.get$ref() != null){
216236

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.swagger.v3.parser.processors;
22

33
import io.swagger.v3.oas.models.OpenAPI;
4+
import io.swagger.v3.oas.models.examples.Example;
45
import io.swagger.v3.oas.models.media.MediaType;
56
import io.swagger.v3.oas.models.media.Schema;
67
import io.swagger.v3.oas.models.parameters.RequestBody;
@@ -18,11 +19,13 @@
1819
public class RequestBodyProcessor {
1920
private final SchemaProcessor schemaProcessor;
2021
private final ExternalRefProcessor externalRefProcessor;
22+
private final ExampleProcessor exampleProcessor;
2123
private final ResolverCache cache;
2224
private final OpenAPI openAPI;
2325

2426
public RequestBodyProcessor(ResolverCache cache, OpenAPI openAPI) {
2527
schemaProcessor = new SchemaProcessor(cache,openAPI);
28+
exampleProcessor = new ExampleProcessor(cache,openAPI);
2629
this.externalRefProcessor = new ExternalRefProcessor(cache, openAPI);
2730
this.cache = cache;
2831
this.openAPI = openAPI;
@@ -44,6 +47,11 @@ public void processRequestBody(RequestBody requestBody) {
4447
schemaProcessor.processSchema(schema);
4548
}
4649
}
50+
if(mediaType.getExamples() != null) {
51+
for(Example ex: mediaType.getExamples().values()){
52+
exampleProcessor.processExample(ex);
53+
}
54+
}
4755
}
4856
}
4957
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.databind.JsonNode;
44
import io.swagger.v3.core.util.Yaml;
55
import io.swagger.v3.core.util.Json;
6+
import org.yaml.snakeyaml.constructor.SafeConstructor;
67

78
import java.io.IOException;
89

@@ -57,12 +58,12 @@ private static boolean isJson(String contents) {
5758
}
5859

5960
public static JsonNode readYamlTree(String contents) {
60-
org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml();
61+
org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml(new SafeConstructor());
6162
return Json.mapper().convertValue(yaml.load(contents), JsonNode.class);
6263
}
6364

6465
public static <T> T readYamlValue(String contents, Class<T> expectedType) {
65-
org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml();
66+
org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml(new SafeConstructor());
6667
return Json.mapper().convertValue(yaml.load(contents), expectedType);
6768
}
6869
}

0 commit comments

Comments
 (0)