Add primitive type support (#1835)

This commit is contained in:
Akihito Nakano 2019-01-16 11:12:49 +09:00 committed by William Cheng
parent 03711d572f
commit dec852ca9f
3 changed files with 70 additions and 2 deletions

View File

@ -75,8 +75,7 @@ public class ExampleGenerator {
} }
if (responseSchema.getExample() != null && !(responseSchema.getExample() instanceof Map)) { if (responseSchema.getExample() != null && !(responseSchema.getExample() instanceof Map)) {
LOGGER.warn("example value (array/primitive) not handled at the moment: " + responseSchema.getExample()); return generate(responseSchema.getExample(), new ArrayList<>(producesInfo));
return null;
} }
if (ModelUtils.isArraySchema(responseSchema)) { // array of schema if (ModelUtils.isArraySchema(responseSchema)) { // array of schema
@ -190,6 +189,34 @@ public class ExampleGenerator {
return output; return output;
} }
private List<Map<String, String>> generate(Object example, List<String> mediaTypes) {
List<Map<String, String>> output = new ArrayList<>();
if (examples != null) {
if (mediaTypes == null) {
// assume application/json for this
mediaTypes = Collections.singletonList(MIME_TYPE_JSON);
}
for (String mediaType : mediaTypes) {
Map<String, String> kv = new HashMap<>();
kv.put(CONTENT_TYPE, mediaType);
if ((mediaType.startsWith(MIME_TYPE_JSON) || mediaType.contains("*/*"))) {
kv.put(EXAMPLE, Json.pretty(example));
output.add(kv);
} else if (mediaType.startsWith(MIME_TYPE_XML)) {
// TODO
LOGGER.warn("XML example value of (array/primitive) is not handled at the moment: " + example);
}
}
}
if (output.size() == 0) {
Map<String, String> kv = new HashMap<>();
kv.put(OUTPUT, NONE);
output.add(kv);
}
return output;
}
private Object resolvePropertyToExample(String propertyName, String mediaType, Schema property, Set<String> processedModels) { private Object resolvePropertyToExample(String propertyName, String mediaType, Schema property, Set<String> processedModels) {
LOGGER.debug("Resolving example for property {}...", property); LOGGER.debug("Resolving example for property {}...", property);
if (property.getExample() != null) { if (property.getExample() != null) {

View File

@ -11,6 +11,36 @@ import java.util.*;
import static org.testng.AssertJUnit.*; import static org.testng.AssertJUnit.*;
public class ExampleGeneratorTest { public class ExampleGeneratorTest {
@Test
public void generateFromResponseSchemaWithPrimitiveType() {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/example_generator_test.yaml", null, new
ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI);
ExampleGenerator exampleGenerator = new ExampleGenerator(openAPI.getComponents().getSchemas(), openAPI);
Set<String> mediaTypeKeys = new TreeSet<>();
mediaTypeKeys.add("application/json");
List<Map<String, String>> examples = exampleGenerator.generateFromResponseSchema(
"200",
openAPI
.getPaths()
.get("/generate_from_response_schema_with_primitive_type")
.getGet()
.getResponses()
.get("200")
.getContent()
.get("application/json")
.getSchema(),
mediaTypeKeys
);
assertEquals(1, examples.size());
assertEquals("application/json", examples.get(0).get("contentType"));
assertEquals("\"primitive type example value\"", examples.get(0).get("example"));
assertEquals("200", examples.get(0).get("statusCode"));
}
@Test @Test
public void generateFromResponseSchemaWithNoExample() { public void generateFromResponseSchemaWithNoExample() {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/example_generator_test.yaml", null, new OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/example_generator_test.yaml", null, new

View File

@ -15,6 +15,17 @@ paths:
application/json: application/json:
schema: schema:
type: string type: string
/generate_from_response_schema_with_primitive_type:
get:
operationId: generateFromResponseSchemaWithNoExample
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: string
example: primitive type example value
/generate_from_response_schema_with_array_of_model: /generate_from_response_schema_with_array_of_model:
get: get:
operationId: generateFromResponseSchemaWithArrayOfModel operationId: generateFromResponseSchemaWithArrayOfModel