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

fix: ExampleGenerator for composed child schemas and array schemas #18479

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.utils.ModelUtils;
Expand Down Expand Up @@ -364,17 +363,7 @@ private Object resolveModelToExample(String name, String mediaType, Schema schem
return schema.getExample();
} else if (ModelUtils.isAllOf(schema) || ModelUtils.isAllOfWithProperties(schema)) {
LOGGER.debug("Resolving allOf model '{}' to example", name);
List<Schema> interfaces = schema.getAllOf();
for (Schema composed : interfaces) {
traverseSchemaProperties(mediaType, composed, processedModels, values);
if (composed.get$ref() != null) {
String ref = ModelUtils.getSimpleRef(composed.get$ref());
Schema resolved = ModelUtils.getSchema(openAPI, ref);
if (resolved != null) {
traverseSchemaProperties(mediaType, resolved, processedModels, values);
}
}
}
resolveAllOfSchemaProperties(mediaType, schema, processedModels, values);
schema.setExample(values);
return schema.getExample();
} else if (ModelUtils.isAnyOf(schema) || ModelUtils.isOneOf(schema)) {
Expand All @@ -388,6 +377,8 @@ private Object resolveModelToExample(String name, String mediaType, Schema schem
return null;
}
return resolvePropertyToExample(name, mediaType, found.get(), processedModels);
} else if (ModelUtils.isArraySchema(schema)) {
return resolvePropertyToExample(schema.getName(), mediaType, schema, processedModels);
} else {
// TODO log an error message as the model does not have any properties
return null;
Expand All @@ -400,6 +391,22 @@ private void traverseSchemaProperties(String mediaType, Schema schema, Set<Strin
Schema property = (Schema) schema.getProperties().get(propertyName.toString());
values.put(propertyName.toString(), resolvePropertyToExample(propertyName.toString(), mediaType, property, processedModels));
}
} else if (ModelUtils.isAllOf(schema) || ModelUtils.isAllOfWithProperties(schema)) {
resolveAllOfSchemaProperties(mediaType, schema, processedModels, values);
}
}

private void resolveAllOfSchemaProperties(String mediaType, Schema schema, Set<String> processedModels, Map<String, Object> values) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add a 1-liner docstring explaining what this function does?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

List<Schema> interfaces = schema.getAllOf();
for (Schema composed : interfaces) {
traverseSchemaProperties(mediaType, composed, processedModels, values);
if (composed.get$ref() != null) {
String ref = ModelUtils.getSimpleRef(composed.get$ref());
Schema resolved = ModelUtils.getSchema(openAPI, ref);
if (resolved != null) {
traverseSchemaProperties(mediaType, resolved, processedModels, values);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,35 @@ public void generateFromResponseSchemaWithArrayOfPrimitiveTypes() {
assertEquals("200", examples.get(0).get("statusCode"));
}

@Test
public void generateFromResponseSchemaWithArraySchema() {
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_array_reference")
.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}, {%n \"example_schema_property\" : \"example schema property value\"%n} ]"), examples.get(0).get("example"));
assertEquals("200", examples.get(0).get("statusCode"));
}

@Test
public void generateFromResponseSchemaWithModel() {
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/example_generator_test.yaml");
Expand Down Expand Up @@ -210,6 +239,35 @@ public void generateFromResponseSchemaWithAllOfComposedModel() {
assertEquals("200", examples.get(0).get("statusCode"));
}

@Test
public void generateFromResponseSchemaWithAllOfChildComposedModel() {
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_allOf_child_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_composed\" : \"example schema property value composed\",%n \"example_schema_property_composed_parent\" : \"example schema property value composed parent\",%n \"example_schema_property\" : \"example schema property value\"%n}"), examples.get(0).get("example"));
assertEquals("200", examples.get(0).get("statusCode"));
}

@Test
public void generateFromResponseSchemaWithOneOfComposedModel() {
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/example_generator_test.yaml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ paths:
items:
type: string
example: primitive types example value
/generate_from_response_schema_array_reference:
get:
operationId: generateFromResponseSchemaArrayReference
responses:
'200':
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleArraySchema'
/generate_from_response_schema_with_model:
get:
operationId: generateFromResponseSchemaWithModel
Expand All @@ -81,6 +91,16 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ExampleAllOfSchema'
/generate_from_response_schema_with_allOf_child_composed_model:
get:
operationId: generateFromResponseSchemaWithAllOfModel
responses:
'200':
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleAllOfParentSchema'
/generate_from_response_schema_with_anyOf_composed_model:
get:
operationId: generateFromResponseSchemaWithAnyOfModel
Expand Down Expand Up @@ -131,6 +151,15 @@ components:
example_schema_property_composed:
type: string
example: example schema property value composed
ExampleAllOfParentSchema:
type: object
allOf:
- $ref: '#/components/schemas/ExampleAllOfSchema'
- type: object
properties:
example_schema_property_composed_parent:
type: string
example: example schema property value composed parent
ExampleAnyOfSchema:
type: object
anyOf:
Expand All @@ -149,3 +178,7 @@ components:
example_schema_property_composed:
type: string
example: example schema property value composed
ExampleArraySchema:
type: array
items:
$ref: '#/components/schemas/ExampleSchema'
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,10 @@ components:
type: array
RolesReportsHash:
description: Role report Hash
example:
role:
name: name
role_uuid: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
properties:
role_uuid:
format: uuid
Expand Down Expand Up @@ -2714,6 +2718,8 @@ components:
- country
type: object
RolesReportsHash_role:
example:
name: name
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,10 @@ components:
type: array
RolesReportsHash:
description: Role report Hash
example:
role:
name: name
role_uuid: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
properties:
role_uuid:
format: uuid
Expand Down Expand Up @@ -2862,6 +2866,8 @@ components:
- country
type: object
RolesReportsHash_role:
example:
name: name
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,10 @@ components:
type: array
RolesReportsHash:
description: Role report Hash
example:
role:
name: name
role_uuid: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
properties:
role_uuid:
format: uuid
Expand Down Expand Up @@ -2862,6 +2866,8 @@ components:
- country
type: object
RolesReportsHash_role:
example:
name: name
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,10 @@ components:
type: array
RolesReportsHash:
description: Role report Hash
example:
role:
name: name
role_uuid: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
properties:
role_uuid:
format: uuid
Expand Down Expand Up @@ -2862,6 +2866,8 @@ components:
- country
type: object
RolesReportsHash_role:
example:
name: name
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,10 @@ components:
type: array
RolesReportsHash:
description: Role report Hash
example:
role:
name: name
role_uuid: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
properties:
role_uuid:
format: uuid
Expand Down Expand Up @@ -2862,6 +2866,8 @@ components:
- country
type: object
RolesReportsHash_role:
example:
name: name
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,10 @@ components:
type: array
RolesReportsHash:
description: Role report Hash
example:
role:
name: name
role_uuid: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
properties:
role_uuid:
format: uuid
Expand Down Expand Up @@ -2862,6 +2866,8 @@ components:
- country
type: object
RolesReportsHash_role:
example:
name: name
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,10 @@ components:
type: array
RolesReportsHash:
description: Role report Hash
example:
role:
name: name
role_uuid: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
properties:
role_uuid:
format: uuid
Expand Down Expand Up @@ -2894,6 +2898,8 @@ components:
- country
type: object
RolesReportsHash_role:
example:
name: name
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,10 @@ components:
type: array
RolesReportsHash:
description: Role report Hash
example:
role:
name: name
role_uuid: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
properties:
role_uuid:
format: uuid
Expand Down Expand Up @@ -2894,6 +2898,8 @@ components:
- country
type: object
RolesReportsHash_role:
example:
name: name
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,10 @@ components:
type: array
RolesReportsHash:
description: Role report Hash
example:
role:
name: name
role_uuid: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
properties:
role_uuid:
format: uuid
Expand Down Expand Up @@ -2894,6 +2898,8 @@ components:
- country
type: object
RolesReportsHash_role:
example:
name: name
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,10 @@ components:
type: array
RolesReportsHash:
description: Role report Hash
example:
role:
name: name
role_uuid: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
properties:
role_uuid:
format: uuid
Expand Down Expand Up @@ -2862,6 +2866,8 @@ components:
- country
type: object
RolesReportsHash_role:
example:
name: name
properties:
name:
type: string
Expand Down
Loading
Loading