Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -406,14 +406,27 @@ private void traverseSchemaProperties(String mediaType, Schema schema, Set<Strin
*/
private void resolveAllOfSchemaProperties(String mediaType, Schema schema, Set<String> processedModels, Map<String, Object> values) {
List<Schema> interfaces = schema.getAllOf();
if (interfaces == null) {
return;
}

for (Schema composed : interfaces) {
traverseSchemaProperties(mediaType, composed, processedModels, values);
if (composed.get$ref() != null) {
String ref = ModelUtils.getSimpleRef(composed.get$ref());

if (processedModels.contains(ref)) {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
LOGGER.warn("Circular reference detected in allOf for $ref: {}. Skipping.", ref);
continue;
}

Schema resolved = ModelUtils.getSchema(openAPI, ref);
if (resolved != null) {
processedModels.add(ref);
traverseSchemaProperties(mediaType, resolved, processedModels, values);
processedModels.remove(ref);
}
} else {
traverseSchemaProperties(mediaType, composed, processedModels, values);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.util.*;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import static org.testng.AssertJUnit.assertEquals;
Expand Down Expand Up @@ -249,6 +248,68 @@ public void generateFromResponseSchemaWithAllOfComposedModel() throws Exception{
assertEquals("200", examples.get(0).get("statusCode"));
}

@Test
public void generateFromResponseSchemaWithAllOfCircularSchema() throws Exception{
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_circular_model")
.getGet()
.getResponses()
.get("200")
.getContent()
.get("application/json")
.getSchema(),
mediaTypeKeys
);

ObjectMapper mapper = new ObjectMapper();

assertEquals(1, examples.size());
assertEquals("application/json", examples.get(0).get("contentType"));
assertEquals(mapper.readTree(String.format(Locale.ROOT, "{%n \"example_schema_property_alloff_circular\" : \"example schema property allOff circular\"%n}")), mapper.readTree(examples.get(0).get("example")));
assertEquals("200", examples.get(0).get("statusCode"));
}

@Test
public void generateFromResponseSchemaWithAllOfSiblingSchema() throws Exception{
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_sibling_model")
.getGet()
.getResponses()
.get("200")
.getContent()
.get("application/json")
.getSchema(),
mediaTypeKeys
);

ObjectMapper mapper = new ObjectMapper();

assertEquals(1, examples.size());
assertEquals("application/json", examples.get(0).get("contentType"));
assertEquals(mapper.readTree(String.format(Locale.ROOT, "{%n \"base\":{\"example_schema_property\":\"example schema property value\",\"example_schema_property_composed\":\"example schema property value composed\"}, \"sibling\":{\"example_schema_property\":\"example schema property value\",\"example_schema_property_composed\":\"example schema property value composed\"} %n}")), mapper.readTree(examples.get(0).get("example")));
assertEquals("200", examples.get(0).get("statusCode"));
}

@Test
public void generateFromResponseSchemaWithAllOfChildComposedModel() throws Exception {
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 @@ -101,6 +101,26 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ExampleAllOfParentSchema'
/generate_from_response_schema_with_allOf_circular_model:
get:
operationId: generateFromResponseSchemaWithAllOfCircularModel
responses:
'200':
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleAllOfCircularSchema'
/generate_from_response_schema_with_allOf_sibling_model:
get:
operationId: generateFromResponseSchemaWithAllOfSiblingModel
responses:
'200':
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleAllOfSiblingContainerSchema'
/generate_from_response_schema_with_anyOf_composed_model:
get:
operationId: generateFromResponseSchemaWithAnyOfModel
Expand Down Expand Up @@ -151,6 +171,15 @@ components:
example_schema_property_composed:
type: string
example: example schema property value composed
ExampleAllOfSiblingSchema:
type: object
allOf:
- $ref: '#/components/schemas/ExampleSchema'
- type: object
properties:
example_schema_property_composed:
type: string
example: example schema property value composed
ExampleAllOfParentSchema:
type: object
allOf:
Expand All @@ -160,6 +189,22 @@ components:
example_schema_property_composed_parent:
type: string
example: example schema property value composed parent
ExampleAllOfCircularSchema:
type: object
allOf:
- $ref: '#/components/schemas/ExampleAllOfCircularSchema'
- type: object
properties:
example_schema_property_alloff_circular:
type: string
example: example schema property allOff circular
ExampleAllOfSiblingContainerSchema:
type: object
properties:
base:
$ref: '#/components/schemas/ExampleAllOfSchema'
sibling:
$ref: '#/components/schemas/ExampleAllOfSiblingSchema'
ExampleAnyOfSchema:
type: object
anyOf:
Expand Down