diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java index 2e01191849..7a56847a07 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java @@ -75,8 +75,7 @@ public class ExampleGenerator { } if (responseSchema.getExample() != null && !(responseSchema.getExample() instanceof Map)) { - LOGGER.warn("example value (array/primitive) not handled at the moment: " + responseSchema.getExample()); - return null; + return generate(responseSchema.getExample(), new ArrayList<>(producesInfo)); } if (ModelUtils.isArraySchema(responseSchema)) { // array of schema @@ -190,6 +189,34 @@ public class ExampleGenerator { return output; } + private List> generate(Object example, List mediaTypes) { + List> 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 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 kv = new HashMap<>(); + kv.put(OUTPUT, NONE); + output.add(kv); + } + return output; + } + private Object resolvePropertyToExample(String propertyName, String mediaType, Schema property, Set processedModels) { LOGGER.debug("Resolving example for property {}...", property); if (property.getExample() != null) { diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/ExampleGeneratorTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/ExampleGeneratorTest.java index 9d6882dcce..0a12ba5167 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/ExampleGeneratorTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/ExampleGeneratorTest.java @@ -11,6 +11,36 @@ import java.util.*; import static org.testng.AssertJUnit.*; 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 mediaTypeKeys = new TreeSet<>(); + mediaTypeKeys.add("application/json"); + List> 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 public void generateFromResponseSchemaWithNoExample() { OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/example_generator_test.yaml", null, new diff --git a/modules/openapi-generator/src/test/resources/3_0/example_generator_test.yaml b/modules/openapi-generator/src/test/resources/3_0/example_generator_test.yaml index ef4232f881..c8f2187e69 100644 --- a/modules/openapi-generator/src/test/resources/3_0/example_generator_test.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/example_generator_test.yaml @@ -15,6 +15,17 @@ paths: application/json: schema: 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: get: operationId: generateFromResponseSchemaWithArrayOfModel