Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using specialized schema subclasses #1165

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -32,6 +32,7 @@
import io.swagger.parser.SwaggerResolver;
import io.swagger.parser.util.SwaggerDeserializationResult;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.PrimitiveType;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
Expand Down Expand Up @@ -692,9 +693,20 @@ private Schema convert(SerializableParameter sp) {
}
schema = as;
} else {
schema = new Schema();
schema.setType(sp.getType());
schema.setFormat(sp.getFormat());
PrimitiveType ptype = PrimitiveType.fromTypeAndFormat(sp.getType(), sp.getFormat());
if (ptype != null) {
schema = ptype.createProperty();
} else {
ptype = PrimitiveType.fromTypeAndFormat(sp.getType(), null);
if (ptype != null) {
schema = ptype.createProperty();
schema.setFormat(sp.getFormat());
} else {
schema = new Schema();
schema.setType(sp.getType());
schema.setFormat(sp.getFormat());
}
}
}

schema.setDescription(sp.getDescription());
Expand Down
Expand Up @@ -7,8 +7,10 @@
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.BooleanSchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
Expand Down Expand Up @@ -91,6 +93,7 @@ public class V2ConverterTest {
private static final String ISSUE_820_YAML = "issue-820.yaml";
private static final String ISSUE_1032_YAML = "issue-1032.yaml";
private static final String ISSUE_1113_YAML = "issue-1113.yaml";
private static final String ISSUE_1164_YAML = "issue-1164.yaml";

private static final String API_BATCH_PATH = "/api/batch/";
private static final String PETS_PATH = "/pets";
Expand Down Expand Up @@ -790,7 +793,37 @@ public void testIssue1113() throws Exception {
assertNotNull(oas.getServers().get(0));
assertEquals(oas.getServers().get(0).getUrl(), "/test");
}


@Test(description = "OpenAPI v2 converter - uses specialized schema subclasses where available")
public void testIssue1164() throws Exception {
final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_1164_YAML);
assertNotNull(oas);
assertNotNull(oas.getPaths());
assertNotNull(oas.getPaths().get("/foo"));
assertNotNull(oas.getPaths().get("/foo").getGet());
assertNotNull(oas.getPaths().get("/foo").getGet().getRequestBody());
assertNotNull(oas.getPaths().get("/foo").getGet().getRequestBody().getContent());
assertNotNull(oas.getPaths().get("/foo").getGet().getRequestBody().getContent().get("multipart/form-data"));
Schema formSchema = oas.getPaths().get("/foo").getGet().getRequestBody().getContent().get("multipart/form-data").getSchema();
assertNotNull(formSchema);
assertNotNull(formSchema.getProperties());
assertEquals(4, formSchema.getProperties().size());
assertTrue(formSchema.getProperties().get("first") instanceof StringSchema);

assertTrue(formSchema.getProperties().get("second") instanceof BooleanSchema);

assertTrue(formSchema.getProperties().get("third") instanceof StringSchema);
StringSchema third = (StringSchema) formSchema.getProperties().get("third");
assertNotNull(third.getFormat());
assertTrue("password".equals(third.getFormat()));

assertTrue(formSchema.getProperties().get("fourth") instanceof BooleanSchema);
Schema fourth = (Schema) formSchema.getProperties().get("fourth");
assertNotNull(fourth.getType());
assertNotNull(fourth.getFormat());
assertTrue("completely-custom".equals(fourth.getFormat()));
}

private OpenAPI getConvertedOpenAPIFromJsonFile(String file) throws IOException, URISyntaxException {
SwaggerConverter converter = new SwaggerConverter();
String swaggerAsString = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource(file).toURI())));
Expand Down
@@ -0,0 +1,30 @@
swagger: '2.0'
info:
title: Test for Issue 1164
version: 1.0.0
paths:
/foo:
get:
operationId: doFoo
parameters:
- in: formData
name: first
type: string
required: true
- in: formData
name: second
type: boolean
required: true
- in: formData
name: third
type: string
format: password
required: true
- in: formData
name: fourth
type: boolean
format: completely-custom
required: true
responses:
'200':
description: OK