Skip to content

Commit 383d802

Browse files
authored
Fix property handling in OpenAPINormalizer (#25104)
* Fix property handling in OpenAPINormalizer Fix OpenAPINormalizer to correctly handle required properties by splitting them into local and inherited lists * correct fix and add tests
1 parent 43b4130 commit 383d802

3 files changed

Lines changed: 124 additions & 5 deletions

File tree

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,21 +2179,49 @@ protected Schema processRefactorAllOfWithPropertiesOnly(Schema schema) {
21792179
return schema;
21802180
}
21812181

2182+
Map<String, Schema> originalProperties = schema.getProperties();
2183+
List<String> originalRequired = schema.getRequired();
2184+
21822185
ObjectSchema os = new ObjectSchema();
21832186
// set the properties, etc of the new schema to the properties of schema
2184-
os.setProperties(schema.getProperties());
2185-
os.setRequired(schema.getRequired());
2187+
os.setProperties(originalProperties);
21862188
os.setAdditionalProperties(schema.getAdditionalProperties());
21872189
os.setNullable(schema.getNullable());
21882190
os.setDescription(schema.getDescription());
21892191
os.setDeprecated(schema.getDeprecated());
21902192
os.setExample(schema.getExample());
21912193
os.setExamples(schema.getExamples());
21922194
os.setTitle(schema.getTitle());
2193-
schema.getAllOf().add(os); // move new schema as a child schema of allOf
2194-
// clean up by removing properties, etc
2195+
2196+
// Schema#setRequired only keeps names present in properties, so clear them first to retain inherited required names
21952197
schema.setProperties(null);
2196-
schema.setRequired(null);
2198+
2199+
// Split required properties between local and inherited
2200+
if (originalRequired != null) {
2201+
List<String> localRequired = new ArrayList<>();
2202+
List<String> inheritedRequired = new ArrayList<>();
2203+
2204+
for (String req : originalRequired) {
2205+
if (originalProperties != null && originalProperties.containsKey(req)) {
2206+
localRequired.add(req);
2207+
} else {
2208+
inheritedRequired.add(req);
2209+
}
2210+
}
2211+
2212+
if (!localRequired.isEmpty()) {
2213+
os.setRequired(localRequired);
2214+
}
2215+
2216+
if (!inheritedRequired.isEmpty()) {
2217+
schema.setRequired(inheritedRequired);
2218+
} else {
2219+
schema.setRequired(null);
2220+
}
2221+
}
2222+
2223+
schema.getAllOf().add(os); // move new schema as a child schema of allOf
2224+
// clean up by removing properties, etc
21972225
schema.setAdditionalProperties(null);
21982226
schema.setNullable(null);
21992227
schema.setDescription(null);

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,52 @@ public void testOpenAPINormalizerRefactorAllOfWithPropertiesOnly() {
624624
assertEquals(newSchema.getRequired().get(0), "isParent");
625625
}
626626

627+
@Test
628+
public void testOpenAPINormalizerRefactorAllOfWithPropertiesOnlyKeepsInheritedRequired() {
629+
// to test the rule REFACTOR_ALLOF_WITH_PROPERTIES_ONLY with required properties declared in the referenced schema
630+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/allOf_inherited_required.yaml");
631+
632+
Schema schema = openAPI.getComponents().getSchemas().get("MySchema");
633+
assertEquals(schema.getAllOf().size(), 1);
634+
assertEquals(schema.getProperties().size(), 1);
635+
636+
Map<String, String> options = new HashMap<>();
637+
options.put("REFACTOR_ALLOF_WITH_PROPERTIES_ONLY", "true");
638+
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
639+
openAPINormalizer.normalize();
640+
641+
Schema schema2 = openAPI.getComponents().getSchemas().get("MySchema");
642+
assertEquals(schema2.getAllOf().size(), 2);
643+
assertNull(schema2.getProperties());
644+
// the required property inherited from the referenced schema stays on the parent schema
645+
assertEquals(schema2.getRequired(), Arrays.asList("uuid"));
646+
647+
// the required property declared locally moves with its property
648+
Schema newSchema = (Schema) (schema2.getAllOf().get(1));
649+
assertEquals(((Schema) newSchema.getProperties().get("some_property")).getType(), "boolean");
650+
assertEquals(newSchema.getRequired(), Arrays.asList("some_property"));
651+
}
652+
653+
@Test
654+
public void testOpenAPINormalizerRefactorAllOfWithPropertiesOnlyOnlyInheritedRequired() {
655+
// to test the rule REFACTOR_ALLOF_WITH_PROPERTIES_ONLY when all required properties are inherited
656+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/allOf_inherited_required.yaml");
657+
658+
Map<String, String> options = new HashMap<>();
659+
options.put("REFACTOR_ALLOF_WITH_PROPERTIES_ONLY", "true");
660+
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
661+
openAPINormalizer.normalize();
662+
663+
Schema schema = openAPI.getComponents().getSchemas().get("MySchemaOnlyInheritedRequired");
664+
assertEquals(schema.getAllOf().size(), 2);
665+
assertNull(schema.getProperties());
666+
assertEquals(schema.getRequired(), Arrays.asList("uuid"));
667+
668+
Schema newSchema = (Schema) (schema.getAllOf().get(1));
669+
assertNotNull(newSchema.getProperties().get("some_property"));
670+
assertNull(newSchema.getRequired());
671+
}
672+
627673
@Test
628674
public void testNormalize31Schema() {
629675
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/common-parameters.yaml");
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
openapi: 3.0.3
2+
info:
3+
title: allOf with required property declared in the referenced schema
4+
version: 1.0.0
5+
paths:
6+
/my-schema:
7+
get:
8+
operationId: getMySchema
9+
responses:
10+
"200":
11+
description: ok
12+
content:
13+
application/json:
14+
schema:
15+
$ref: '#/components/schemas/MySchema'
16+
components:
17+
schemas:
18+
MySchema:
19+
required:
20+
- uuid
21+
- some_property
22+
allOf:
23+
- $ref: '#/components/schemas/MyInheritedSchema'
24+
type: object
25+
properties:
26+
some_property:
27+
type: boolean
28+
MySchemaOnlyInheritedRequired:
29+
required:
30+
- uuid
31+
allOf:
32+
- $ref: '#/components/schemas/MyInheritedSchema'
33+
type: object
34+
properties:
35+
some_property:
36+
type: boolean
37+
MyInheritedSchema:
38+
type: object
39+
properties:
40+
uuid:
41+
type: string
42+
format: uuid
43+
readOnly: true
44+
description: Unique identifier of the resource.
45+
example: 55c950dc-be2b-4f2b-81c9-62e39f590b71

0 commit comments

Comments
 (0)