Skip to content

Commit f945249

Browse files
committed
added fix for issue-1261
1 parent e9b835e commit f945249

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ 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";
98+
9799

98100
private static final String API_BATCH_PATH = "/api/batch/";
99101
private static final String PETS_PATH = "/pets";
@@ -840,4 +842,13 @@ private OpenAPI getConvertedOpenAPIFromJsonFile(String file) throws IOException,
840842
assertNotNull(result);
841843
return result.getOpenAPI();
842844
}
845+
846+
@Test(description = "")
847+
public void testissue1261() throws Exception {
848+
OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_1261_YAML);
849+
assertNotNull(oas);
850+
ComposedSchema schema = (ComposedSchema) oas.getComponents().getSchemas().get("Bar").getProperties().get("bar2");
851+
assertEquals(schema.getAllOf().get(0).get$ref(),"#/components/schemas/Foo");
852+
853+
}
843854
}
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`

0 commit comments

Comments
 (0)