Skip to content

Commit e51a539

Browse files
authored
Merge pull request #1302 from r-sreesaran/issue-1261
Issue 1261
2 parents 2a30061 + 1acf137 commit e51a539

File tree

9 files changed

+4333
-12
lines changed

9 files changed

+4333
-12
lines changed

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,7 @@
2020
import io.swagger.models.parameters.BodyParameter;
2121
import io.swagger.models.parameters.RefParameter;
2222
import io.swagger.models.parameters.SerializableParameter;
23-
import io.swagger.models.properties.AbstractNumericProperty;
24-
import io.swagger.models.properties.ArrayProperty;
25-
import io.swagger.models.properties.FileProperty;
26-
import io.swagger.models.properties.MapProperty;
27-
import io.swagger.models.properties.ObjectProperty;
28-
import io.swagger.models.properties.Property;
29-
import io.swagger.models.properties.RefProperty;
30-
import io.swagger.models.properties.StringProperty;
23+
import io.swagger.models.properties.*;
3124
import io.swagger.parser.SwaggerParser;
3225
import io.swagger.parser.SwaggerResolver;
3326
import io.swagger.parser.util.SwaggerDeserializationResult;
@@ -210,9 +203,27 @@ public SwaggerParseResult convert(SwaggerDeserializationResult parse) {
210203
ref.set$ref(updatedRef);
211204
}
212205
}
206+
207+
if (property instanceof ComposedProperty) {
208+
ComposedProperty comprop = (ComposedProperty) property;
209+
if (comprop.getAllOf() != null) {
210+
for (Property item : comprop.getAllOf()) {
211+
if (item instanceof RefProperty) {
212+
RefProperty ref = (RefProperty) item;
213+
if (ref.get$ref().indexOf("#/definitions") == 0) {
214+
String updatedRef = "#/components/schemas" + ref.get$ref().substring("#/definitions".length());
215+
ref.set$ref(updatedRef);
216+
}
217+
218+
219+
}
220+
221+
}
222+
}
223+
}
213224
}
214225

215-
if (swagger.getParameters() != null) {
226+
if (swagger.getParameters() != null) {
216227
globalV2Parameters.putAll(swagger.getParameters());
217228
swagger.getParameters().forEach((k, v) -> {
218229
if ("body".equals(v.getIn())) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public class V2ConverterTest {
9494
private static final String ISSUE_1032_YAML = "issue-1032.yaml";
9595
private static final String ISSUE_1113_YAML = "issue-1113.yaml";
9696
private static final String ISSUE_1164_YAML = "issue-1164.yaml";
97+
private static final String ISSUE_1261_YAML = "issue-1261.yaml";
9798

9899
private static final String API_BATCH_PATH = "/api/batch/";
99100
private static final String PETS_PATH = "/pets";
@@ -840,4 +841,13 @@ private OpenAPI getConvertedOpenAPIFromJsonFile(String file) throws IOException,
840841
assertNotNull(result);
841842
return result.getOpenAPI();
842843
}
844+
845+
@Test(description = "OpenAPI v2 converter - verifies the references inside composed schema is resolved")
846+
public void testissue1261() throws Exception {
847+
OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_1261_YAML);
848+
assertNotNull(oas);
849+
ComposedSchema schema = (ComposedSchema) oas.getComponents().getSchemas().get("Bar").getProperties().get("bar2");
850+
assertEquals(schema.getAllOf().get(0).get$ref(),"#/components/schemas/Foo");
851+
852+
}
843853
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
swagger: '2.0'
2+
info:
3+
title: Conversion test
4+
version: '1.0.0'
5+
paths: {}
6+
7+
definitions:
8+
Foo:
9+
type: object
10+
properties:
11+
foo:
12+
type: string
13+
Bar:
14+
type: object
15+
properties:
16+
bar1:
17+
$ref: '#/definitions/Foo' # This $ref is converted properly to `#/components/schemas`
18+
bar2:
19+
allOf:
20+
- $ref: '#/definitions/Foo' # This $ref remains as `#/definitions`
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"swagger":"2.0",
3+
"info":{
4+
"title":"sample",
5+
"version":"1.0"
6+
7+
},
8+
"host":"localhost:51552",
9+
"basePath":"/",
10+
"tags":[
11+
{
12+
"name":"foo-controller",
13+
"description":"Foo Controller"
14+
}
15+
],
16+
"paths":{
17+
"/foobar":{
18+
"get":{
19+
"tags":[
20+
"foo-controller"
21+
],
22+
"summary":"foo",
23+
"operationId":"fooUsingGET",
24+
"produces":[
25+
"*/*"
26+
],
27+
"parameters":[
28+
{
29+
"name":"bar",
30+
"in":"query",
31+
"required":false,
32+
"type":"integer",
33+
"maximum":4.0,
34+
"exclusiveMaximum":false,
35+
"minimum":3.0,
36+
"exclusiveMinimum":false,
37+
"format":"int32"
38+
},
39+
40+
{
41+
"name":"bars",
42+
"in":"query",
43+
"required":true,
44+
"type":"array",
45+
"items":{
46+
"type":"integer",
47+
"format":"int32"
48+
},
49+
"collectionFormat":"multi",
50+
"maximum":2,
51+
"minimum":1
52+
},
53+
54+
{
55+
"name":"foo",
56+
"in":"query",
57+
"required":false,
58+
"type":"string",
59+
"pattern":"foo"
60+
}
61+
],
62+
"responses":{
63+
"200":{
64+
"description":"OK"
65+
},
66+
"401":{
67+
"description":"Unauthorized"
68+
},
69+
"403":{
70+
"description":"Forbidden"
71+
},
72+
"404":{
73+
"description":"Not Found"
74+
}
75+
},
76+
"deprecated":false
77+
}
78+
}
79+
}
80+
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,15 +2261,15 @@ private static int getDynamicPort() {
22612261
}
22622262

22632263
@Test
2264+
22642265
public void testIssue1236() {
22652266
final ParseOptions options = new ParseOptions();
22662267
options.setResolve(true);
22672268

22682269
SwaggerParseResult result = new OpenAPIV3Parser()
2269-
.readLocation("src/test/resources/issue-1236/petstore.json",null,options);
2270-
assertEquals(result.getMessages().get(0),"attribute .servers. invalid url : /te st/sample.yaml");
2270+
.readLocation("src/test/resources/issue-1236/petstore.json", null, options);
2271+
assertEquals(result.getMessages().get(0), "attribute .servers. invalid url : /te st/sample.yaml");
22712272
}
2272-
22732273
public void testSampleParser() {
22742274
final String location = "src/test/resources/issue-1211.json";
22752275

@@ -2300,5 +2300,8 @@ public void testDuplicateHttpStatusCodes() {
23002300
List<String> messages = result.getMessages();
23012301
assertEquals(1, messages.size());
23022302
assertEquals(messages.get(0), "Duplicate field '200' in `src/test/resources/duplicateHttpStatusCodes.json`");
2303+
23032304
}
2305+
2306+
23042307
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"openapi":"3.0.0",
3+
"info":{
4+
"title":"data",
5+
"version":"1"
6+
},
7+
8+
"tags":[
9+
{
10+
"name":"foo-controller",
11+
"description":"Foo Controller"
12+
}
13+
],
14+
15+
16+
"paths":{
17+
"/foobar":{
18+
"get":{
19+
"tags":[
20+
"foo-controller"
21+
],
22+
"summary":"foo",
23+
"operationId":"fooUsingGET",
24+
25+
"parameters":[
26+
{
27+
"name":"bars",
28+
"in":"path",
29+
"required":true,
30+
"schema": {
31+
"type": "string",
32+
"format": "byte"
33+
}
34+
}
35+
],
36+
"responses":{
37+
"200":{
38+
"description":"OK"
39+
}
40+
},
41+
"deprecated":false
42+
}
43+
}
44+
},
45+
"components": {
46+
"securitySchemes": {
47+
"sample":{
48+
"type": "oauth2",
49+
"flows": {
50+
"implicit": {
51+
"authorizationUrl": "https://example.com/api/oauth/dialog",
52+
"scopes": {
53+
"write:pets": "modify pets in your account",
54+
"read:pets": "read your pets"
55+
}
56+
},
57+
"authorizationCode": {
58+
"authorizationUrl": "https://example.com/api/oauth/dialog",
59+
"tokenUrl": "https://example.com/api/oauth/token"
60+
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)