|
31 | 31 | import java.util.ArrayList; |
32 | 32 | import java.util.Arrays; |
33 | 33 | import java.util.HashMap; |
| 34 | +import java.util.LinkedHashMap; |
34 | 35 | import java.util.List; |
35 | 36 | import java.util.Map; |
36 | 37 |
|
@@ -1263,4 +1264,94 @@ public void resolveMinimumBound_nestedAllOf_mostRestrictiveAcrossAllLevels() { |
1263 | 1264 | assertEquals(bound.minBound, BigDecimal.valueOf(20)); |
1264 | 1265 | } |
1265 | 1266 |
|
| 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 | + |
1266 | 1357 | } |
0 commit comments