-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Closed
Labels
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When generating an example for a model defined as a composite schema, such as:
Current (undesired) behavior:
Consider the following schema:
components:
schemas:
ExampleSchema:
type: object
properties:
example_schema_property:
type: string
example: example schema property value
ExampleComposedSchema:
type: object
allOf:
- $ref: '#/components/schemas/ExampleSchema'
- type: object
properties:
example_schema_property_composed:
type: string
example: example schema property value composedI'd expect the generated example to look like:
{
"example_schema_property": "example schema property value",
"example_schema_property_composed": "example schema property value composed"
}However, it currently generates the following:
nullWhy is this behavior happening?
It's because only examples and properties are being supported right now:
Lines 349 to 363 in a5d3fb4
| if (schema.getExample() != null) { | |
| LOGGER.debug("Using example from spec: {}", schema.getExample()); | |
| return schema.getExample(); | |
| } else if (schema.getProperties() != null) { | |
| LOGGER.debug("Creating example from model values"); | |
| for (Object propertyName : schema.getProperties().keySet()) { | |
| Schema property = (Schema) schema.getProperties().get(propertyName.toString()); | |
| values.put(propertyName.toString(), resolvePropertyToExample(propertyName.toString(), mediaType, property, processedModels)); | |
| } | |
| schema.setExample(values); | |
| return schema.getExample(); | |
| } else { | |
| // TODO log an error message as the model does not have any properties | |
| return null; | |
| } |
The message
// TODO log an error message as the model does not have any properties is wrong because composed schema may exist.
openapi-generator version
master a5d3fb4
OpenAPI declaration file content or url
openapi: 3.0.2
info:
version: 1.0.0
title: OpenAPI Petstore
license:
name: Apache-2.0
paths:
/generate_from_response_schema_with_composed_model:
get:
operationId: generateFromResponseSchemaWithComposedModel
responses:
'200':
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleComposedSchema'
components:
schemas:
ExampleSchema:
type: object
properties:
example_schema_property:
type: string
example: example schema property value
ExampleComposedSchema:
type: object
allOf:
- $ref: '#/components/schemas/ExampleSchema'
- type: object
properties:
example_schema_property_composed:
type: string
example: example schema property value composedGeneration Details
Run ExampleGeneratorTest.java
Steps to reproduce
Apply to following patch:
Subject: [PATCH] Add failing test case to ExampleGenerator for composed schema
---
Index: modules/openapi-generator/src/test/resources/3_0/example_generator_test.yaml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
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
--- a/modules/openapi-generator/src/test/resources/3_0/example_generator_test.yaml (revision a5d3fb4f601e4bced19f39cb6d82799a71531282)
+++ b/modules/openapi-generator/src/test/resources/3_0/example_generator_test.yaml (date 1703875535793)
@@ -53,7 +53,7 @@
example: primitive types example value
/generate_from_response_schema_with_model:
get:
- operationId: generateFromResponseSchemaWithArrayOfPrimitiveTypes
+ operationId: generateFromResponseSchemaWithModel
responses:
'200':
description: successful operation
@@ -61,6 +61,16 @@
application/json:
schema:
$ref: '#/components/schemas/ExampleSchema'
+ /generate_from_response_schema_with_composed_model:
+ get:
+ operationId: generateFromResponseSchemaWithComposedModel
+ responses:
+ '200':
+ description: successful operation
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/ExampleComposedSchema'
components:
schemas:
StringSchema:
@@ -72,3 +82,12 @@
example_schema_property:
type: string
example: example schema property value
+ ExampleComposedSchema:
+ type: object
+ allOf:
+ - $ref: '#/components/schemas/ExampleSchema'
+ - type: object
+ properties:
+ example_schema_property_composed:
+ type: string
+ example: example schema property value composed
Index: modules/openapi-generator/src/test/java/org/openapitools/codegen/ExampleGeneratorTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
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
--- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/ExampleGeneratorTest.java (revision a5d3fb4f601e4bced19f39cb6d82799a71531282)
+++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/ExampleGeneratorTest.java (date 1703875564840)
@@ -146,6 +146,35 @@
mediaTypeKeys
);
+ assertEquals(1, examples.size());
+ assertEquals("application/json", examples.get(0).get("contentType"));
+ assertEquals(String.format(Locale.ROOT, "{%n \"example_schema_property\" : \"example schema property value\"%n}"), examples.get(0).get("example"));
+ assertEquals("200", examples.get(0).get("statusCode"));
+ }
+
+ @Test
+ public void generateFromResponseSchemaWithComposedModel() {
+ OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/example_generator_test.yaml");
+
+ 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_composed_model")
+ .getGet()
+ .getResponses()
+ .get("200")
+ .getContent()
+ .get("application/json")
+ .getSchema(),
+ mediaTypeKeys
+ );
+
assertEquals(1, examples.size());
assertEquals("application/json", examples.get(0).get("contentType"));
assertEquals(String.format(Locale.ROOT, "{%n \"example_schema_property\" : \"example schema property value\"%n}"), examples.get(0).get("example"));Related issues/PRs
Suggest a fix
Will open a PR soon
Reactions are currently unavailable