Skip to content

Commit 301af60

Browse files
authored
fix allOf with properties for ref as parent rule (#20083)
1 parent a95ea1f commit 301af60

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -755,15 +755,16 @@ private Schema normalizeAllOfWithProperties(Schema schema, Set<Schema> visitedSc
755755
return schema;
756756
}
757757

758+
// process rule to refactor properties into allOf sub-schema
759+
schema = processRefactorAllOfWithPropertiesOnly(schema);
760+
758761
for (Object item : schema.getAllOf()) {
759762
if (!(item instanceof Schema)) {
760763
throw new RuntimeException("Error! allOf schema is not of the type Schema: " + item);
761764
}
762765
// normalize allOf sub schemas one by one
763766
normalizeSchema((Schema) item, visitedSchemas);
764767
}
765-
// process rules here
766-
schema = processRefactorAllOfWithPropertiesOnly(schema);
767768

768769
return schema;
769770
}
@@ -1325,9 +1326,8 @@ private Schema processRefactorAllOfWithPropertiesOnly(Schema schema) {
13251326
schema.setTitle(null);
13261327

13271328
// at this point the schema becomes a simple allOf (no properties) with an additional schema containing
1328-
// the properties
1329-
1330-
return schema;
1329+
// the properties. Normalize it before returning.
1330+
return normalizeSchema(schema, new HashSet<>());
13311331
}
13321332

13331333
/**

modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,30 @@ public void testOpenAPINormalizerRefAsParentInAllOf() {
5656
assertEquals(schema5.getExtensions().get("x-parent"), "abstract");
5757
}
5858

59+
@Test
60+
public void testOpenAPINormalizerRefAsParentInAllOfAndRefactorAllOfWithProperties() {
61+
// to test the both REF_AS_PARENT_IN_ALLOF and REFACTOR_ALLOF_WITH_PROPERTIES_ONLY
62+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/allOf_extension_parent.yaml");
63+
64+
Schema schema = openAPI.getComponents().getSchemas().get("Child");
65+
assertNull(schema.getExtensions());
66+
67+
Schema schema2 = openAPI.getComponents().getSchemas().get("Ancestor");
68+
assertNull(schema2.getExtensions());
69+
70+
Map<String, String> options = new HashMap<>();
71+
options.put("REF_AS_PARENT_IN_ALLOF", "true");
72+
options.put("REFACTOR_ALLOF_WITH_PROPERTIES_ONLY", "true");
73+
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
74+
openAPINormalizer.normalize();
75+
76+
Schema schema3 = openAPI.getComponents().getSchemas().get("Ancestor");
77+
assertEquals(schema3.getExtensions().get("x-parent"), true);
78+
79+
Schema schema4 = openAPI.getComponents().getSchemas().get("Child");
80+
assertNull(schema4.getExtensions());
81+
}
82+
5983
@Test
6084
public void testOpenAPINormalizerEnableKeepOnlyFirstTagInOperation() {
6185
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/enableKeepOnlyFirstTagInOperation_test.yaml");

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,7 @@ public void testJdkHttpClientWithAndWithoutParentExtension() {
17761776
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
17771777

17781778
validateJavaSourceFiles(files);
1779-
assertThat(files).hasSize(27);
1779+
assertThat(files).hasSize(33);
17801780
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Child.java"))
17811781
.content().contains("public class Child extends Person {");
17821782
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Adult.java"))

modules/openapi-generator/src/test/resources/3_0/allOf_extension_parent.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,15 @@ components:
101101
description: allOf with a single item
102102
nullable: true
103103
allOf:
104-
- $ref: '#/components/schemas/AnotherParent'
104+
- $ref: '#/components/schemas/AnotherParent'
105+
Ancestor:
106+
type: object
107+
properties:
108+
p1:
109+
type: integer
110+
Offspring:
111+
properties:
112+
p2:
113+
type: string
114+
allOf:
115+
- $ref: '#/components/schemas/Ancestor'

0 commit comments

Comments
 (0)