Skip to content

Commit e0f8a28

Browse files
authored
Fix all ofs with nullable properties in OpenAPI 3.1 specs (#25064)
* Fix all ofs with nullable properties * Recursively restore nullability for copied 3.1 schemas * Expand tests for nullability fix
1 parent 9a3fb0a commit e0f8a28

4 files changed

Lines changed: 242 additions & 1 deletion

File tree

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2572,7 +2572,15 @@ public static boolean isParent(Schema schema) {
25722572
*/
25732573
public static Schema cloneSchema(Schema schema, boolean openapi31) {
25742574
if (openapi31) {
2575-
return AnnotationsUtils.clone(schema, openapi31);
2575+
Schema result = AnnotationsUtils.clone(schema, openapi31);
2576+
// `nullable` is not a valid OAS 3.1 keyword, so the 3.1 serializer used by
2577+
// AnnotationsUtils.clone silently drops it -- at every level of the schema, not just
2578+
// the root. OpenAPINormalizer rewrites an OAS 3.1 `type: [<type>, "null"]` declaration
2579+
// into `nullable: true` plus a plain type, so dropping it here would turn cloned
2580+
// schemas (e.g. properties merged into a model from an `allOf` parent) and every
2581+
// schema nested inside them into non-nullable ones. Carry it over explicitly.
2582+
restoreNullable(schema, result);
2583+
return result;
25762584
} else {
25772585
// AnnotationsUtils.clone doesn't support custom schema types for OpenAPI < 3.1
25782586
String schemaType = schema.getType();
@@ -2586,6 +2594,60 @@ public static Schema cloneSchema(Schema schema, boolean openapi31) {
25862594
}
25872595
}
25882596

2597+
/**
2598+
* Copies the `nullable` flag from {@code original} onto {@code cloned}, recursing through
2599+
* every nested schema (properties, array items, additionalProperties, composed sub-schemas
2600+
* and `not`). Used to repair an OpenAPI 3.1 clone, whose serializer drops the flag because
2601+
* `nullable` is not a valid OAS 3.1 keyword.
2602+
* <p>
2603+
* The two schemas are walked in parallel, so this relies on {@code cloned} having the same
2604+
* shape as {@code original} -- which holds because it is a faithful deep copy in every other
2605+
* respect. Nested lists are matched positionally and defensively bounded by the shorter of
2606+
* the two.
2607+
*
2608+
* @param original the schema that was cloned
2609+
* @param cloned the clone to repair
2610+
*/
2611+
private static void restoreNullable(Schema original, Schema cloned) {
2612+
if (original == null || cloned == null) {
2613+
return;
2614+
}
2615+
2616+
if (original.getNullable() != null) {
2617+
cloned.setNullable(original.getNullable());
2618+
}
2619+
2620+
restoreNullableInMap(original.getProperties(), cloned.getProperties());
2621+
restoreNullable(original.getItems(), cloned.getItems());
2622+
restoreNullable(original.getNot(), cloned.getNot());
2623+
2624+
if (original.getAdditionalProperties() instanceof Schema
2625+
&& cloned.getAdditionalProperties() instanceof Schema) {
2626+
restoreNullable((Schema) original.getAdditionalProperties(),
2627+
(Schema) cloned.getAdditionalProperties());
2628+
}
2629+
2630+
restoreNullableInList(original.getAllOf(), cloned.getAllOf());
2631+
restoreNullableInList(original.getOneOf(), cloned.getOneOf());
2632+
restoreNullableInList(original.getAnyOf(), cloned.getAnyOf());
2633+
}
2634+
2635+
private static void restoreNullableInMap(Map<String, Schema> original, Map<String, Schema> cloned) {
2636+
if (original == null || cloned == null) {
2637+
return;
2638+
}
2639+
original.forEach((name, originalValue) -> restoreNullable(originalValue, cloned.get(name)));
2640+
}
2641+
2642+
private static void restoreNullableInList(List<Schema> original, List<Schema> cloned) {
2643+
if (original == null || cloned == null) {
2644+
return;
2645+
}
2646+
for (int i = 0; i < Math.min(original.size(), cloned.size()); i++) {
2647+
restoreNullable(original.get(i), cloned.get(i));
2648+
}
2649+
}
2650+
25892651
/**
25902652
* Simplifies the schema by removing the oneOfAnyOf if the oneOfAnyOf only contains a single non-null sub-schema
25912653
*

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,38 @@ public void testDateTimeFormParameterHasDefaultValue() {
348348
Assertions.assertNull(codegenParameter.getSchema());
349349
}
350350

351+
@Test
352+
public void testOAS31NullableTypeArrayIsInheritedThroughAllOf() {
353+
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/allof-nullable-type-array.yaml");
354+
new OpenAPINormalizer(openAPI, Map.of("NORMALIZE_31SPEC", "true")).normalize();
355+
356+
final DefaultCodegen codegen = new DefaultCodegen();
357+
codegen.setOpenAPI(openAPI);
358+
359+
// `id`, the items of `tags` and the values of `meta` are all declared with the OAS 3.1
360+
// `type: [<type>, 'null']` form on Base, so they must stay nullable both on Base itself
361+
// and on Derived, which pulls them in via `allOf: [$ref: Base, {...}]`. Nullability must
362+
// survive at every nesting level, not just on the property itself.
363+
for (String modelName : List.of("Base", "Derived", "Control")) {
364+
Schema<?> schema = openAPI.getComponents().getSchemas().get(modelName);
365+
CodegenModel model = codegen.fromModel(modelName, schema);
366+
367+
assertTrue(varNamed(model, "id").isNullable,
368+
"`id` must be nullable on " + modelName);
369+
assertTrue(varNamed(model, "tags").items.isNullable,
370+
"the items of `tags` must be nullable on " + modelName);
371+
assertTrue(varNamed(model, "meta").additionalProperties.isNullable,
372+
"the values of `meta` must be nullable on " + modelName);
373+
}
374+
}
375+
376+
private static CodegenProperty varNamed(CodegenModel model, String baseName) {
377+
return model.vars.stream()
378+
.filter(v -> baseName.equals(v.baseName))
379+
.findFirst()
380+
.orElseThrow(() -> new AssertionError("no `" + baseName + "` var on " + model.name));
381+
}
382+
351383
@Test
352384
public void testOAS31ContentMediaTypeBinaryFormParameter() {
353385
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/binary-schema.yaml");

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.ArrayList;
3232
import java.util.Arrays;
3333
import java.util.HashMap;
34+
import java.util.LinkedHashMap;
3435
import java.util.List;
3536
import java.util.Map;
3637

@@ -1263,4 +1264,94 @@ public void resolveMinimumBound_nestedAllOf_mostRestrictiveAcrossAllLevels() {
12631264
assertEquals(bound.minBound, BigDecimal.valueOf(20));
12641265
}
12651266

1267+
/**
1268+
* `nullable` is not a valid OAS 3.1 keyword, so a naive 3.1 clone drops it. The normalizer
1269+
* relies on `nullable` to carry an OAS 3.1 `type: [<type>, "null"]` declaration, so cloning
1270+
* must preserve it.
1271+
*/
1272+
@Test
1273+
public void testCloneSchemaPreservesNullableForOpenAPI31() {
1274+
Schema<?> schema = new Schema<>();
1275+
schema.setType("string");
1276+
schema.setNullable(true);
1277+
1278+
Schema<?> cloned31 = ModelUtils.cloneSchema(schema, true);
1279+
assertTrue(Boolean.TRUE.equals(cloned31.getNullable()),
1280+
"nullable must be preserved when cloning an OpenAPI 3.1 schema");
1281+
1282+
Schema<?> cloned30 = ModelUtils.cloneSchema(schema, false);
1283+
assertTrue(Boolean.TRUE.equals(cloned30.getNullable()),
1284+
"nullable must be preserved when cloning an OpenAPI 3.0 schema");
1285+
}
1286+
1287+
/**
1288+
* The OAS 3.1 serializer drops `nullable` at every level of the schema, so cloning must
1289+
* restore it on nested schemas too, not only on the root. This covers every branch
1290+
* {@code restoreNullable} recurses into.
1291+
*/
1292+
@Test
1293+
public void testCloneSchemaPreservesNestedNullableForOpenAPI31() {
1294+
Schema<Object> root = new Schema<>();
1295+
root.setType("object");
1296+
root.setNullable(true);
1297+
1298+
Map<String, Schema> properties = new LinkedHashMap<>();
1299+
properties.put("inner", nullableStringSchema());
1300+
1301+
ArraySchema array = new ArraySchema();
1302+
array.setItems(nullableStringSchema());
1303+
properties.put("list", array);
1304+
1305+
Schema<Object> map = new Schema<>();
1306+
map.setType("object");
1307+
map.setAdditionalProperties(nullableStringSchema());
1308+
properties.put("map", map);
1309+
1310+
Schema<Object> negated = new Schema<>();
1311+
negated.setNot(nullableStringSchema());
1312+
properties.put("negated", negated);
1313+
1314+
Schema<Object> allOf = new Schema<>();
1315+
allOf.setAllOf(new ArrayList<>(List.of(nullableStringSchema())));
1316+
properties.put("allOf", allOf);
1317+
1318+
Schema<Object> oneOf = new Schema<>();
1319+
oneOf.setOneOf(new ArrayList<>(List.of(nullableStringSchema())));
1320+
properties.put("oneOf", oneOf);
1321+
1322+
Schema<Object> anyOf = new Schema<>();
1323+
anyOf.setAnyOf(new ArrayList<>(List.of(nullableStringSchema())));
1324+
properties.put("anyOf", anyOf);
1325+
1326+
root.setProperties(properties);
1327+
1328+
Schema<?> cloned = ModelUtils.cloneSchema(root, true);
1329+
1330+
assertTrue(Boolean.TRUE.equals(cloned.getNullable()), "root nullable must be preserved");
1331+
assertNestedNullable(property(cloned, "inner"), "a nested property");
1332+
assertNestedNullable(property(cloned, "list").getItems(), "array items");
1333+
assertNestedNullable((Schema<?>) property(cloned, "map").getAdditionalProperties(), "additionalProperties");
1334+
assertNestedNullable(property(cloned, "negated").getNot(), "a `not` sub-schema");
1335+
assertNestedNullable(property(cloned, "allOf").getAllOf().get(0), "an `allOf` sub-schema");
1336+
assertNestedNullable(property(cloned, "oneOf").getOneOf().get(0), "a `oneOf` sub-schema");
1337+
assertNestedNullable(property(cloned, "anyOf").getAnyOf().get(0), "an `anyOf` sub-schema");
1338+
}
1339+
1340+
private static Schema<?> property(Schema<?> schema, String name) {
1341+
return schema.getProperties().get(name);
1342+
}
1343+
1344+
private static void assertNestedNullable(Schema<?> schema, String location) {
1345+
assertNotNull(schema, "expected a cloned schema at " + location);
1346+
assertTrue(Boolean.TRUE.equals(schema.getNullable()),
1347+
"nullable must be preserved on " + location);
1348+
}
1349+
1350+
private static Schema<?> nullableStringSchema() {
1351+
Schema<?> schema = new Schema<>();
1352+
schema.setType("string");
1353+
schema.setNullable(true);
1354+
return schema;
1355+
}
1356+
12661357
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
openapi: 3.1.0
2+
info:
3+
title: allOf with OAS 3.1 nullable type array
4+
version: 1.0.0
5+
paths: {}
6+
components:
7+
schemas:
8+
Base:
9+
type: object
10+
properties:
11+
id:
12+
type:
13+
- string
14+
- 'null'
15+
tags:
16+
type: array
17+
items:
18+
type:
19+
- string
20+
- 'null'
21+
meta:
22+
type: object
23+
additionalProperties:
24+
type:
25+
- string
26+
- 'null'
27+
name:
28+
type: string
29+
Derived:
30+
allOf:
31+
- $ref: '#/components/schemas/Base'
32+
- type: object
33+
properties:
34+
email:
35+
type: string
36+
Control:
37+
type: object
38+
properties:
39+
id:
40+
type:
41+
- string
42+
- 'null'
43+
tags:
44+
type: array
45+
items:
46+
type:
47+
- string
48+
- 'null'
49+
meta:
50+
type: object
51+
additionalProperties:
52+
type:
53+
- string
54+
- 'null'
55+
email:
56+
type: string

0 commit comments

Comments
 (0)