Skip to content

Commit 43283e9

Browse files
Extend the normalizer tests to illustrate that an AllOfs with several refs have all of them marked as parents
1 parent 77a2488 commit 43283e9

3 files changed

Lines changed: 54 additions & 15 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.function.Function;
3838
import java.util.stream.Collectors;
3939

40+
import static org.openapitools.codegen.CodegenVendorExtension.X_PARENT;
4041
import static org.openapitools.codegen.utils.ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema;
4142
import static org.openapitools.codegen.utils.StringUtils.getUniqueString;
4243

@@ -1083,10 +1084,10 @@ protected void processUseAllOfRefAsParent(Schema schema) {
10831084
refSchema.setExtensions(new HashMap<>());
10841085
}
10851086

1086-
if (refSchema.getExtensions().containsKey("x-parent")) {
1087+
if (refSchema.getExtensions().containsKey(X_PARENT.getName())) {
10871088
// doing nothing as x-parent already exists
10881089
} else {
1089-
refSchema.getExtensions().put("x-parent", true);
1090+
refSchema.getExtensions().put(X_PARENT.getName(), true);
10901091
}
10911092

10921093
LOGGER.debug("processUseAllOfRefAsParent added `x-parent: true` to {}", refSchema);

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

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,47 @@
3030
import static org.testng.Assert.*;
3131

3232
public class OpenAPINormalizerTest {
33+
34+
private static final String REF_AS_PARENT_IN_ALLOF = "REF_AS_PARENT_IN_ALLOF";
35+
private static final String X_PARENT = "x-parent";
36+
3337
@Test
3438
public void testOpenAPINormalizerRefAsParentInAllOf() {
3539
// to test the rule REF_AS_PARENT_IN_ALLOF
3640
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/allOf_extension_parent.yaml");
3741

38-
Schema schema = openAPI.getComponents().getSchemas().get("AnotherPerson");
39-
assertNull(schema.getExtensions());
42+
Schema<?> anotherPerson = openAPI.getComponents().getSchemas().get("AnotherPerson");
43+
assertNull(anotherPerson.getExtensions());
4044

41-
Schema schema2 = openAPI.getComponents().getSchemas().get("Person");
42-
assertEquals(schema2.getExtensions().get("x-parent"), "abstract");
45+
Schema<?>person = openAPI.getComponents().getSchemas().get("Person");
46+
assertEquals(person.getExtensions().get(X_PARENT), "abstract");
47+
48+
Schema<?> preNormPersonA = openAPI.getComponents().getSchemas().get("PersonA");
49+
assertNull(preNormPersonA.getExtensions());
50+
Schema<?> preNormPersonB = openAPI.getComponents().getSchemas().get("PersonB");
51+
assertNull(preNormPersonB.getExtensions());
4352

4453
Map<String, String> options = new HashMap<>();
45-
options.put("REF_AS_PARENT_IN_ALLOF", "true");
54+
options.put(REF_AS_PARENT_IN_ALLOF, "true");
4655
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
4756
openAPINormalizer.normalize();
4857

49-
Schema schema3 = openAPI.getComponents().getSchemas().get("AnotherPerson");
50-
assertEquals(schema3.getExtensions().get("x-parent"), true);
58+
Schema<?>schema3 = openAPI.getComponents().getSchemas().get("AnotherPerson");
59+
assertEquals(schema3.getExtensions().get(X_PARENT), true);
60+
61+
Schema<?>schema4 = openAPI.getComponents().getSchemas().get("AnotherParent");
62+
assertEquals(schema4.getExtensions().get(X_PARENT), true);
5163

52-
Schema schema4 = openAPI.getComponents().getSchemas().get("AnotherParent");
53-
assertEquals(schema4.getExtensions().get("x-parent"), true);
64+
Schema<?>schema5 = openAPI.getComponents().getSchemas().get("Person");
65+
assertEquals(schema5.getExtensions().get(X_PARENT), "abstract");
5466

55-
Schema schema5 = openAPI.getComponents().getSchemas().get("Person");
56-
assertEquals(schema5.getExtensions().get("x-parent"), "abstract");
67+
// Verify that all allOf refs gets marked as parents
68+
Schema<?>schemaWithTwoParents = openAPI.getComponents().getSchemas().get("SchemaWithTwoParents");
69+
assertNull(schemaWithTwoParents.getExtensions());
70+
Schema<?>personA = openAPI.getComponents().getSchemas().get("PersonA");
71+
assertEquals(personA.getExtensions().get(X_PARENT), true);
72+
Schema<?>personB = openAPI.getComponents().getSchemas().get("PersonB");
73+
assertEquals(personB.getExtensions().get(X_PARENT), true);
5774
}
5875

5976
@Test
@@ -68,13 +85,13 @@ public void testOpenAPINormalizerRefAsParentInAllOfAndRefactorAllOfWithPropertie
6885
assertNull(schema2.getExtensions());
6986

7087
Map<String, String> options = new HashMap<>();
71-
options.put("REF_AS_PARENT_IN_ALLOF", "true");
88+
options.put(REF_AS_PARENT_IN_ALLOF, "true");
7289
options.put("REFACTOR_ALLOF_WITH_PROPERTIES_ONLY", "true");
7390
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
7491
openAPINormalizer.normalize();
7592

7693
Schema schema3 = openAPI.getComponents().getSchemas().get("Ancestor");
77-
assertEquals(schema3.getExtensions().get("x-parent"), true);
94+
assertEquals(schema3.getExtensions().get(X_PARENT), true);
7895

7996
Schema schema4 = openAPI.getComponents().getSchemas().get("Child");
8097
assertNull(schema4.getExtensions());

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ components:
3737
type: string
3838
firstName:
3939
type: string
40+
PersonA:
41+
type: object
42+
properties:
43+
lastName:
44+
type: string
45+
PersonB:
46+
description:
47+
type: object
48+
properties:
49+
firstName:
50+
type: string
4051
Adult:
4152
description: A representation of an adult
4253
allOf:
@@ -65,6 +76,16 @@ components:
6576
type: integer
6677
format: int32
6778
- $ref: '#/components/schemas/AnotherPerson'
79+
SchemaWithTwoParents:
80+
description: A schema that has two allOfs with refs
81+
allOf:
82+
- type: object
83+
properties:
84+
age:
85+
type: integer
86+
format: int32
87+
- $ref: '#/components/schemas/PersonA'
88+
- $ref: '#/components/schemas/PersonB'
6889
AnotherPerson:
6990
description: person object without x-parent extension
7091
type: object

0 commit comments

Comments
 (0)