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

Commit 47913eb

Browse files
authored
Merge pull request swagger-api#1165 from blast-hardcheese/use-specialized-schema-subtypes-where-possible
Using specialized schema subclasses
2 parents 832e6a7 + d674677 commit 47913eb

3 files changed

Lines changed: 79 additions & 4 deletions

File tree

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import io.swagger.parser.SwaggerResolver;
3333
import io.swagger.parser.util.SwaggerDeserializationResult;
3434
import io.swagger.v3.core.util.Json;
35+
import io.swagger.v3.core.util.PrimitiveType;
3536
import io.swagger.v3.oas.models.Components;
3637
import io.swagger.v3.oas.models.ExternalDocumentation;
3738
import io.swagger.v3.oas.models.OpenAPI;
@@ -692,9 +693,20 @@ private Schema convert(SerializableParameter sp) {
692693
}
693694
schema = as;
694695
} else {
695-
schema = new Schema();
696-
schema.setType(sp.getType());
697-
schema.setFormat(sp.getFormat());
696+
PrimitiveType ptype = PrimitiveType.fromTypeAndFormat(sp.getType(), sp.getFormat());
697+
if (ptype != null) {
698+
schema = ptype.createProperty();
699+
} else {
700+
ptype = PrimitiveType.fromTypeAndFormat(sp.getType(), null);
701+
if (ptype != null) {
702+
schema = ptype.createProperty();
703+
schema.setFormat(sp.getFormat());
704+
} else {
705+
schema = new Schema();
706+
schema.setType(sp.getType());
707+
schema.setFormat(sp.getFormat());
708+
}
709+
}
698710
}
699711

700712
schema.setDescription(sp.getDescription());

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import io.swagger.v3.oas.models.headers.Header;
88
import io.swagger.v3.oas.models.info.Info;
99
import io.swagger.v3.oas.models.media.ArraySchema;
10+
import io.swagger.v3.oas.models.media.BooleanSchema;
1011
import io.swagger.v3.oas.models.media.ComposedSchema;
1112
import io.swagger.v3.oas.models.media.IntegerSchema;
13+
import io.swagger.v3.oas.models.media.StringSchema;
1214
import io.swagger.v3.oas.models.media.Schema;
1315
import io.swagger.v3.oas.models.parameters.Parameter;
1416
import io.swagger.v3.oas.models.parameters.RequestBody;
@@ -91,6 +93,7 @@ public class V2ConverterTest {
9193
private static final String ISSUE_820_YAML = "issue-820.yaml";
9294
private static final String ISSUE_1032_YAML = "issue-1032.yaml";
9395
private static final String ISSUE_1113_YAML = "issue-1113.yaml";
96+
private static final String ISSUE_1164_YAML = "issue-1164.yaml";
9497

9598
private static final String API_BATCH_PATH = "/api/batch/";
9699
private static final String PETS_PATH = "/pets";
@@ -790,7 +793,37 @@ public void testIssue1113() throws Exception {
790793
assertNotNull(oas.getServers().get(0));
791794
assertEquals(oas.getServers().get(0).getUrl(), "/test");
792795
}
793-
796+
797+
@Test(description = "OpenAPI v2 converter - uses specialized schema subclasses where available")
798+
public void testIssue1164() throws Exception {
799+
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_1164_YAML);
800+
assertNotNull(oas);
801+
assertNotNull(oas.getPaths());
802+
assertNotNull(oas.getPaths().get("/foo"));
803+
assertNotNull(oas.getPaths().get("/foo").getGet());
804+
assertNotNull(oas.getPaths().get("/foo").getGet().getRequestBody());
805+
assertNotNull(oas.getPaths().get("/foo").getGet().getRequestBody().getContent());
806+
assertNotNull(oas.getPaths().get("/foo").getGet().getRequestBody().getContent().get("multipart/form-data"));
807+
Schema formSchema = oas.getPaths().get("/foo").getGet().getRequestBody().getContent().get("multipart/form-data").getSchema();
808+
assertNotNull(formSchema);
809+
assertNotNull(formSchema.getProperties());
810+
assertEquals(4, formSchema.getProperties().size());
811+
assertTrue(formSchema.getProperties().get("first") instanceof StringSchema);
812+
813+
assertTrue(formSchema.getProperties().get("second") instanceof BooleanSchema);
814+
815+
assertTrue(formSchema.getProperties().get("third") instanceof StringSchema);
816+
StringSchema third = (StringSchema) formSchema.getProperties().get("third");
817+
assertNotNull(third.getFormat());
818+
assertTrue("password".equals(third.getFormat()));
819+
820+
assertTrue(formSchema.getProperties().get("fourth") instanceof BooleanSchema);
821+
Schema fourth = (Schema) formSchema.getProperties().get("fourth");
822+
assertNotNull(fourth.getType());
823+
assertNotNull(fourth.getFormat());
824+
assertTrue("completely-custom".equals(fourth.getFormat()));
825+
}
826+
794827
private OpenAPI getConvertedOpenAPIFromJsonFile(String file) throws IOException, URISyntaxException {
795828
SwaggerConverter converter = new SwaggerConverter();
796829
String swaggerAsString = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource(file).toURI())));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
swagger: '2.0'
2+
info:
3+
title: Test for Issue 1164
4+
version: 1.0.0
5+
paths:
6+
/foo:
7+
get:
8+
operationId: doFoo
9+
parameters:
10+
- in: formData
11+
name: first
12+
type: string
13+
required: true
14+
- in: formData
15+
name: second
16+
type: boolean
17+
required: true
18+
- in: formData
19+
name: third
20+
type: string
21+
format: password
22+
required: true
23+
- in: formData
24+
name: fourth
25+
type: boolean
26+
format: completely-custom
27+
required: true
28+
responses:
29+
'200':
30+
description: OK

0 commit comments

Comments
 (0)