Skip to content

Commit

Permalink
fix: refs #1552 - fix response schema conversion v2->v3
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Nov 15, 2022
1 parent 9739fc6 commit fd3bc70
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,8 @@ public ApiResponse convert(io.swagger.models.Response v2Response, List<String> p

response.setDescription(v2Response.getDescription());

if (v2Response.getSchema() != null) {
Schema schema = convertFileSchema(convert(v2Response.getSchema()));
if (v2Response.getResponseSchema() != null) {
Schema schema = convertFileSchema(convert(v2Response.getResponseSchema()));
for (String type : mediaTypes) {
// TODO: examples
MediaType mediaType = new MediaType();
Expand Down Expand Up @@ -930,6 +930,9 @@ private Schema convert(Property schema) {
if (schema == null) {
return null;
}
if (schema.getBooleanValue() != null) {
return new Schema().booleanSchemaValue(schema.getBooleanValue());
}
Schema result;

if (schema instanceof RefProperty) {
Expand Down Expand Up @@ -961,8 +964,7 @@ private Schema convert(Property schema) {
result = arraySchema;

} else if (schema instanceof FileProperty) {
FileSchema fileSchema = Json.mapper().convertValue(schema, FileSchema.class);
result = fileSchema;
result = Json.mapper().convertValue(schema, FileSchema.class);

}else {

Expand All @@ -982,7 +984,11 @@ private Schema convert(Property schema) {
if (schema instanceof MapProperty) {
MapProperty map = (MapProperty) schema;

result.setAdditionalProperties(convert(map.getAdditionalProperties()));
if (map.getAdditionalProperties().getBooleanValue() != null) {
result.setAdditionalProperties(map.getAdditionalProperties().getBooleanValue());
} else {
result.setAdditionalProperties(convert(map.getAdditionalProperties()));
}
result.setMinProperties(map.getMinProperties());
result.setMaxProperties(map.getMaxProperties());
}
Expand Down Expand Up @@ -1173,6 +1179,9 @@ public Schema convert(io.swagger.models.Model v2Model) {
if (v2Model == null) {
return null;
}
if (v2Model.getBooleanValue() != null) {
return new Schema().booleanSchemaValue(v2Model.getBooleanValue());
}
Schema result;

if (v2Model instanceof ArrayModel) {
Expand Down Expand Up @@ -1208,16 +1217,22 @@ public Schema convert(io.swagger.models.Model v2Model) {
v2discriminator = model.getDiscriminator();
model.setDiscriminator(null);
}

result = Json.mapper().convertValue(v2Model, Schema.class);

if (v2Model instanceof ModelImpl && ("file".equals(((ModelImpl)v2Model).getType()))) {
result = Json.mapper().convertValue(v2Model, FileSchema.class);
} else {
result = Json.mapper().convertValue(v2Model, Schema.class);
}
addProperties(v2Model, result);

if (v2Model instanceof ModelImpl) {
ModelImpl model = (ModelImpl) v2Model;

if (model.getAdditionalProperties() != null) {
result.setAdditionalProperties(convert(model.getAdditionalProperties()));
if (model.getAdditionalProperties().getBooleanValue() != null) {
result.setAdditionalProperties(model.getAdditionalProperties().getBooleanValue());
} else {
result.setAdditionalProperties(convert(model.getAdditionalProperties()));
}
}
} else if(v2Model instanceof RefModel) {
RefModel ref = (RefModel) v2Model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ public void testIssue745() throws Exception {
OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_745_YAML);
assertTrue(oas.getServers().get(0).getUrl().startsWith("//"));
}

@Test(description = "OpenAPIParser.readLocation fails when fetching valid Swagger 2.0 resource with AuthorizationValues provided")
public void testIssue785() {
AuthorizationValue apiKey = new AuthorizationValue("api_key", "special-key", "header");
Expand Down Expand Up @@ -676,7 +676,7 @@ public void testIssue758() throws Exception {
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_758_JSON);
assertNotNull(oas);
}

@Test(description = "OpenAPI v2 Converter: NPE when type is array and 'items' field is missing in array property")
public void testIssue762() throws Exception {
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_762_JSON);
Expand Down Expand Up @@ -733,7 +733,7 @@ public void testSwaggerParseResultHasMessage() throws Exception {
assertNotNull(result.getMessages());
}


@Test(description = "OpenAPI v2 converter - Migrate minLength, maxLength and pattern of String property")
public void testIssue786() throws Exception {
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_768_JSON);
Expand All @@ -753,7 +753,7 @@ public void testTopLevelExtensions() throws Exception {
assertNotNull(oas);
assertEquals((String)oas.getExtensions().get("x-some-extensions"), "hello");
}

@Test(description = "OpenAPI v2 converter - Conversion param extensions should be preserved")
public void testIssue820() throws Exception {
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_820_YAML);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.swagger.parser;

import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.ArraySchema;
Expand Down Expand Up @@ -665,5 +666,129 @@ public void testConvertWindowsPathsToUnixWhenResolvingServerPaths() {

assertEquals("/api/customer1/v1", result.getOpenAPI().getServers().get(0).getUrl());
}

@org.testng.annotations.Test(description = "convert response schema")
public void testIssue1552() throws Exception {
ParseOptions options = new ParseOptions();
options.setResolve(true);
OpenAPIParser parser = new OpenAPIParser();
SwaggerParseResult result = parser.readContents(
"swagger: \"2.0\"\n"
+ "info:\n"
+ " version: \"1.0\"\n"
+ " title: \"test\"\n"
+ "host: \"foo\"\n"
+ "paths:\n"
+ " /example:\n"
+ " get:\n"
+ " consumes:\n"
+ " - \"application/json\"\n"
+ " produces:\n"
+ " - \"application/json\"\n"
+ " responses:\n"
+ " 200:\n"
+ " $ref: \"#/responses/something\"\n"
+ "responses:\n"
+ " something:\n"
+ " description: \"my response\"\n"
+ " schema:\n"
+ " title: \"my schema\"\n"
+ " properties:\n"
+ " foo:\n"
+ " type: string\n"
+ "", null, options);
Schema<?> schema = result.getOpenAPI().getPaths()
.get("/example").getGet()
.getResponses().get("200").getContent()
.get("application/json").getSchema();
assertNotNull(schema.getTitle()); // found title
assertNotNull(schema.getProperties().get("foo"));
}

@org.testng.annotations.Test(description = "convert response schema")
public void testIssue1552AdditionalProps() throws Exception {
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setInferSchemaType(false);
OpenAPIParser parser = new OpenAPIParser();
SwaggerParseResult result = parser.readContents(
"swagger: '2.0'\n" +
"info:\n" +
" title: Some API\n" +
" description: >-\n" +
" This is the Some Api\n" +
" version: 2.0.0\n" +
"basePath: /somepath\n" +
"schemes:\n" +
" - https\n" +
"consumes:\n" +
" - application/json\n" +
"produces:\n" +
" - application/json\n" +
"paths:\n" +
" /somepath:\n" +
" get:\n" +
" description: >\n" +
" my description\n" +
" operationId: MyGet\n" +
" responses:\n" +
" '200':\n" +
" $ref: '#/responses/Response'\n" +
"responses:\n" +
" Response:\n" +
" description: Response\n" +
" schema:\n" +
" type: object\n" +
" required:\n" +
" - Report\n" +
" properties:\n" +
" Report:\n" +
" type: string\n" +
" additionalProperties: false", null, options);
assertEquals(Yaml.pretty(result), "messages: []\n" +
"openAPI:\n" +
" openapi: 3.0.1\n" +
" info:\n" +
" title: Some API\n" +
" description: This is the Some Api\n" +
" version: 2.0.0\n" +
" servers:\n" +
" - url: /somepath\n" +
" paths:\n" +
" /somepath:\n" +
" get:\n" +
" description: |\n" +
" my description\n" +
" operationId: MyGet\n" +
" responses:\n" +
" \"200\":\n" +
" description: Response\n" +
" content:\n" +
" application/json:\n" +
" schema:\n" +
" required:\n" +
" - Report\n" +
" type: object\n" +
" properties:\n" +
" Report:\n" +
" type: string\n" +
" additionalProperties: false\n" +
" components:\n" +
" responses:\n" +
" Response:\n" +
" description: Response\n" +
" content:\n" +
" application/json:\n" +
" schema:\n" +
" required:\n" +
" - Report\n" +
" type: object\n" +
" properties:\n" +
" Report:\n" +
" type: string\n" +
" additionalProperties: false\n" +
" x-original-swagger-version: \"2.0\"\n" +
"openapi31: false\n");
}
}

10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -403,19 +403,19 @@
</repositories>
<properties>
<snakeyaml-version>1.33</snakeyaml-version>
<swagger-parser-v2-version>1.0.63</swagger-parser-v2-version>
<swagger-parser-v2-version>1.0.64</swagger-parser-v2-version>
<commons-io-version>2.11.0</commons-io-version>
<slf4j-version>1.7.30</slf4j-version>
<swagger-core-version>2.2.6</swagger-core-version>
<swagger-core-v2-version>1.6.8</swagger-core-v2-version>
<swagger-core-version>2.2.7</swagger-core-version>
<swagger-core-v2-version>1.6.9</swagger-core-v2-version>
<junit-version>4.13.2</junit-version>
<testng-version>6.14.2</testng-version>
<jmockit-version>1.35</jmockit-version>
<wiremock-version>2.15.0</wiremock-version>
<surefire-version>2.22.2</surefire-version>
<commons-lang-version>3.2.1</commons-lang-version>
<jackson-version>2.13.4</jackson-version>
<jackson-databind-version>2.13.4.2</jackson-databind-version>
<jackson-version>2.14.0</jackson-version>
<jackson-databind-version>2.14.0</jackson-databind-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sonatypeOssDistMgmtSnapshotsUrl>https://oss.sonatype.org/content/repositories/snapshots/</sonatypeOssDistMgmtSnapshotsUrl>
</properties>
Expand Down

0 comments on commit fd3bc70

Please sign in to comment.