Skip to content

Commit 2a307ae

Browse files
committed
Merge branch 'branch_test' of https://github.com/r-sreesaran/swagger-parser into branch_test
2 parents 31a9263 + 5a7d858 commit 2a307ae

File tree

18 files changed

+338
-7
lines changed

18 files changed

+338
-7
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/models/RefType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
public enum RefType {
77
COMPONENTS("#/components/"),
8+
SCHEMAS("#/components/schemas/"),
89
PATH("#/paths/");
910

1011
private final String internalPrefix;

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.swagger.v3.oas.models.media.ArraySchema;
1111
import io.swagger.v3.oas.models.media.ComposedSchema;
1212
import io.swagger.v3.oas.models.media.MediaType;
13+
import io.swagger.v3.oas.models.media.ObjectSchema;
1314
import io.swagger.v3.oas.models.media.Schema;
1415
import io.swagger.v3.oas.models.parameters.Parameter;
1516
import io.swagger.v3.oas.models.parameters.RequestBody;
@@ -18,6 +19,7 @@
1819
import io.swagger.v3.oas.models.security.SecurityScheme;
1920
import io.swagger.v3.parser.ResolverCache;
2021
import io.swagger.v3.parser.models.RefFormat;
22+
import io.swagger.v3.parser.models.RefType;
2123
import org.apache.commons.lang3.StringUtils;
2224
import org.slf4j.LoggerFactory;
2325

@@ -199,6 +201,11 @@ private void processProperties(Map<String,Schema> subProps, String file) {
199201
&& StringUtils.isNotBlank(((ArraySchema) mapProp.getAdditionalProperties()).getItems().get$ref())) {
200202
processRefSchema(((ArraySchema) mapProp.getAdditionalProperties()).getItems(), file);
201203
}
204+
}else if (prop.getValue() instanceof ObjectSchema){
205+
ObjectSchema objProp = (ObjectSchema) prop.getValue();
206+
if(objProp.getProperties() != null ){
207+
processProperties(objProp.getProperties(),file);
208+
}
202209
}
203210
}
204211
}
@@ -674,16 +681,17 @@ private void processRefSchema(Schema subRef, String externalFile) {
674681
RefFormat format = computeRefFormat(subRef.get$ref());
675682

676683
if (!isAnExternalRefFormat(format)) {
677-
subRef.set$ref(processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
684+
subRef.set$ref(RefType.SCHEMAS.getInternalPrefix()+ processRefToExternalSchema(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
678685
return;
679686
}
680687
String $ref = subRef.get$ref();
681688

682689
if (format.equals(RefFormat.RELATIVE)) {
683690
$ref = constructRef(subRef, externalFile);
684691
subRef.set$ref($ref);
692+
}else {
693+
processRefToExternalSchema($ref, format);
685694
}
686-
processRefToExternalSchema($ref, computeRefFormat(subRef.get$ref()));
687695
}
688696

689697

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.swagger.v3.oas.models.media.Schema;
1010
import io.swagger.v3.parser.ResolverCache;
1111
import io.swagger.v3.parser.models.RefFormat;
12+
import io.swagger.v3.parser.models.RefType;
1213
import io.swagger.v3.parser.util.RefUtils;
1314

1415
import java.util.HashMap;
@@ -201,7 +202,7 @@ private void processReferenceSchema(Schema schema) {
201202
final String newRef = externalRefProcessor.processRefToExternalSchema($ref, refFormat);
202203

203204
if (newRef != null) {
204-
schema.set$ref(newRef);
205+
schema.set$ref(RefType.SCHEMAS.getInternalPrefix() + newRef);
205206
}
206207
}
207208
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ public void process(URLConnection connection) {
9494
public static String cleanUrl(String url) {
9595
String result = null;
9696
try {
97-
result =url.replaceAll("\\{", "%7B").replaceAll("\\}", "%7D");
97+
result =url.replaceAll("\\{", "%7B").
98+
replaceAll("\\}", "%7D").
99+
replaceAll(" ", "%20");
98100
}catch (Exception t){
99101
t.printStackTrace();
100102
}

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.swagger.v3.oas.models.PathItem;
66
import io.swagger.v3.oas.models.callbacks.Callback;
77
import io.swagger.v3.oas.models.examples.Example;
8+
import io.swagger.v3.oas.models.headers.Header;
89
import io.swagger.v3.oas.models.links.Link;
910
import io.swagger.v3.oas.models.media.ArraySchema;
1011
import io.swagger.v3.oas.models.media.ComposedSchema;
@@ -20,7 +21,6 @@
2021
import org.slf4j.LoggerFactory;
2122

2223
import java.util.ArrayList;
23-
import java.util.Arrays;
2424
import java.util.HashMap;
2525
import java.util.HashSet;
2626
import java.util.LinkedHashMap;
@@ -51,6 +51,7 @@ public ResolverFully(boolean aggregateCombinators) {
5151
private Map<String, Example> examples;
5252
private Map<String, Parameter> parameters;
5353
private Map<String, RequestBody> requestBodies;
54+
private Map<String, Header> headers;
5455
private Map<String, Link> links;
5556

5657
public void resolveFully(OpenAPI openAPI) {
@@ -75,6 +76,12 @@ public void resolveFully(OpenAPI openAPI) {
7576
}
7677
}
7778

79+
if (openAPI.getComponents() != null && openAPI.getComponents().getHeaders() != null) {
80+
headers = openAPI.getComponents().getHeaders();
81+
if (headers == null) {
82+
headers = new HashMap<>();
83+
}
84+
}
7885

7986
if (openAPI.getComponents() != null && openAPI.getComponents().getParameters() != null) {
8087
parameters = openAPI.getComponents().getParameters();
@@ -86,7 +93,6 @@ public void resolveFully(OpenAPI openAPI) {
8693
links = openAPI.getComponents().getLinks();
8794
if (links == null) {
8895
links = new HashMap<>();
89-
9096
}
9197
}
9298

@@ -175,6 +181,21 @@ public void resolvePath(PathItem pathItem){
175181
}
176182
}
177183
}
184+
185+
Map<String, Header> headers = response.getHeaders();
186+
if (headers != null) {
187+
for (Map.Entry<String, Header> header : headers.entrySet()) {
188+
Header value = header.getValue();
189+
Header resolvedValue = value.get$ref() != null ? resolveHeader(value) : value;
190+
Map<String, Example> examples = resolvedValue.getExamples();
191+
if(examples != null) {
192+
Map<String,Example> resolved = resolveExample(examples);
193+
resolvedValue.setExamples(resolved);
194+
}
195+
header.setValue(resolvedValue);
196+
}
197+
}
198+
178199
Map<String, Link> links = response.getLinks();
179200
if (links != null) {
180201
for (Map.Entry<String, Link> link : links.entrySet()) {
@@ -188,6 +209,18 @@ public void resolvePath(PathItem pathItem){
188209
}
189210
}
190211

212+
public Header resolveHeader(Header header){
213+
RefFormat refFormat = computeRefFormat(header.get$ref());
214+
String $ref = header.get$ref();
215+
if (!isAnExternalRefFormat(refFormat)){
216+
if (headers != null && !headers.isEmpty()) {
217+
String referenceKey = computeDefinitionName($ref);
218+
return headers.getOrDefault(referenceKey, header);
219+
}
220+
}
221+
return header;
222+
}
223+
191224
public Link resolveLink(Link link){
192225
RefFormat refFormat = computeRefFormat(link.get$ref());
193226
String $ref = link.get$ref();
@@ -406,7 +439,11 @@ public Schema resolveSchema(Schema schema) {
406439
}
407440
}
408441
}
409-
model.setExample(examples);
442+
if (schema.getExample() != null) {
443+
model.setExample(schema.getExample());
444+
} else if (!examples.isEmpty()) {
445+
model.setExample(examples);
446+
}
410447
return model;
411448
} else {
412449
// User don't want to aggregate composed schema, we only solve refs

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package io.swagger.v3.parser.test;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import com.fasterxml.jackson.databind.JsonNode;
45
import com.fasterxml.jackson.databind.ObjectMapper;
56
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
67
import com.github.tomakehurst.wiremock.WireMockServer;
78
import com.github.tomakehurst.wiremock.client.WireMock;
89
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
10+
import io.swagger.v3.core.util.Json;
911
import io.swagger.v3.oas.models.Components;
1012
import io.swagger.v3.oas.models.OpenAPI;
1113
import io.swagger.v3.oas.models.Operation;
@@ -740,6 +742,20 @@ public void testComposedSchemaAdjacentWithExamples(@Injectable final List<Author
740742
Assert.assertTrue(examples.size() == 2);
741743
}
742744

745+
@Test
746+
public void allOfExampleGeneration(@Injectable final List<AuthorizationValue> auths) throws JsonProcessingException {
747+
ParseOptions options = new ParseOptions();
748+
options.setResolve(true);
749+
options.setResolveFully(true);
750+
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/simpleAllOf.yaml", null, options);
751+
752+
Assert.assertNotNull(openAPI);
753+
Object withoutExample = openAPI.getPaths().get("/foo").getGet().getResponses().get("200").getContent().get("application/json").getSchema().getExample();
754+
Assert.assertNull(withoutExample);
755+
756+
Object withExample = openAPI.getPaths().get("/bar").getGet().getResponses().get("200").getContent().get("application/json").getSchema().getExample();
757+
Assert.assertEquals("{\"someProperty\":\"ABC\",\"someOtherProperty\":42}", Json.mapper().writeValueAsString(withExample));
758+
}
743759

744760
@Test
745761
public void testSwaggerResolver(@Injectable final OpenAPI swagger,

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.swagger.v3.oas.models.OpenAPI;
1313
import io.swagger.v3.oas.models.Operation;
1414
import io.swagger.v3.oas.models.PathItem;
15+
import io.swagger.v3.oas.models.headers.Header;
1516
import io.swagger.v3.oas.models.links.Link;
1617
import io.swagger.v3.oas.models.media.*;
1718
import io.swagger.v3.oas.models.examples.Example;
@@ -57,6 +58,45 @@ public class OpenAPIV3ParserTest {
5758
protected WireMockServer wireMockServer;
5859

5960

61+
@Test
62+
public void testIssue913() {
63+
OpenAPIV3Parser parser = new OpenAPIV3Parser();
64+
ParseOptions options = new ParseOptions();
65+
options.setResolve(true);
66+
final OpenAPI openAPI = parser.readLocation("issue-913/BS/ApiSpecification.yaml", null, options).getOpenAPI();
67+
Assert.assertNotNull(openAPI);
68+
Assert.assertNotNull(openAPI.getComponents().getSchemas().get("indicatorType"));
69+
Assert.assertEquals(openAPI.getComponents().getSchemas().get("indicatorType").getProperties().size(),1);
70+
}
71+
72+
@Test
73+
public void testIssue901_2() {
74+
ParseOptions options = new ParseOptions();
75+
options.setResolve(true);
76+
OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-901/spec2.yaml",null,options).getOpenAPI();
77+
assertNotNull(openAPI);
78+
assertNotNull(openAPI.getComponents());
79+
ArraySchema arraySchema = (ArraySchema) openAPI.getComponents().getSchemas().get("Test.Definition").getProperties().get("stuff");
80+
String internalRef = arraySchema.getItems().get$ref();
81+
assertEquals(internalRef,"#/components/schemas/TEST.THING.OUT.Stuff");
82+
83+
84+
85+
86+
87+
}
88+
89+
@Test
90+
public void testIssue901() {
91+
ParseOptions options = new ParseOptions();
92+
options.setResolve(true);
93+
OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-901/spec.yaml",null,options).getOpenAPI();
94+
assertNotNull(openAPI);
95+
String internalRef = openAPI.getPaths().get("/test").getPut().getResponses().get("200").getContent().get("application/json").getSchema().get$ref();
96+
assertEquals(internalRef,"#/components/schemas/Test.Definition");
97+
assertNotNull(openAPI.getComponents());
98+
}
99+
60100
@Test
61101
public void testIssue853() {
62102
ParseOptions options = new ParseOptions();
@@ -1539,6 +1579,15 @@ public void shouldParseRequestBody() {
15391579
}
15401580

15411581
@Test
1582+
public void testIssue915() {
1583+
ParseOptions parseOptions = new ParseOptions();
1584+
parseOptions.setResolveFully(true);
1585+
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/issue_918.yaml", null, parseOptions);
1586+
Map<String, Header> headers = openAPI.getPaths().get("/2.0/users/").getGet().getResponses().get("200").getHeaders();
1587+
String description = headers.get("X-Rate-Limit").getDescription();
1588+
assertEquals(description, "The number of allowed requests in the current period");
1589+
}
1590+
15421591
public void shouldParseParameters() {
15431592
ParseOptions parseOptions = new ParseOptions();
15441593
parseOptions.setResolveFully(true);

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/RemoteUrlTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public void setUp() throws Exception {
4343
public void testCleanUrl() {
4444
String cleaned = RemoteUrl.cleanUrl("http://foo/bar/com/{v2}/fun");
4545
assertEquals(cleaned, "http://foo/bar/com/%7Bv2%7D/fun");
46+
47+
cleaned = RemoteUrl.cleanUrl("http://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/export?DocumentFormat=Swagger&ApiName=Face API - V1.0");
48+
assertEquals(cleaned, "http://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/export?DocumentFormat=Swagger&ApiName=Face%20API%20-%20V1.0");
4649
}
4750

4851
@Test
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
### Domains, a place to put your reusable components
2+
3+
4+
info:
5+
title: "aa"
6+
description: "swos552"
7+
version: '1.0.0'
8+
9+
components:
10+
schemas:
11+
Test.Definition:
12+
type: object
13+
properties:
14+
stuff:
15+
type: array
16+
items:
17+
$ref: '#/components/schemas/TESTTHING'
18+
TESTTHING:
19+
type: object
20+
properties:
21+
prop:
22+
type: string
23+
pathitems:
24+
path-test:
25+
put:
26+
description: test ref
27+
operationId: test
28+
responses:
29+
'200':
30+
description: successful operation
31+
content:
32+
application/json:
33+
schema:
34+
$ref: '#/components/schemas/Test.Definition'
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
### Domains, a place to put your reusable components
2+
3+
4+
info:
5+
title: "aa"
6+
description: "swos55"
7+
version: '1.0.0'
8+
9+
components:
10+
schemas:
11+
Test.Definition:
12+
type: object
13+
properties:
14+
stuff:
15+
type: array
16+
items:
17+
$ref: '#/components/schemas/TEST.THING.OUT.Stuff'
18+
TEST.THING.OUT.Stuff:
19+
type: object
20+
properties:
21+
prop:
22+
type: string
23+
pathitems:
24+
path-test:
25+
put:
26+
description: test ref
27+
operationId: test
28+
responses:
29+
'200':
30+
description: successful operation
31+
content:
32+
application/json:
33+
schema:
34+
$ref: '#/components/schemas/Test.Definition'

0 commit comments

Comments
 (0)