Skip to content

Commit b004c67

Browse files
committed
test: add nullable-oneof.yaml and unit test for simplifyNullableOneOf (fix #32520)
1 parent 1545db1 commit b004c67

2 files changed

Lines changed: 53 additions & 47 deletions

File tree

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

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,56 +1253,23 @@ public void testRemoveXInternalFromInlineProperties() {
12531253

12541254
@Test
12551255
public void testSimplifyNullableOneOf() {
1256-
// 1. Define the OpenAPI spec string that contains the bug pattern
1257-
final String yaml =
1258-
"openapi: 3.0.0\n" +
1259-
"info:\n" +
1260-
" version: 1.0.0\n" +
1261-
" title: API\n" +
1262-
"paths:\n" +
1263-
" /test:\n" +
1264-
" get:\n" +
1265-
" responses:\n" +
1266-
" '200':\n" +
1267-
" description: OK\n" +
1268-
" content:\n" +
1269-
" application/json:\n" +
1270-
" schema:\n" +
1271-
" $ref: '#/components/schemas/Recipe'\n" +
1272-
"components:\n" +
1273-
" schemas:\n" +
1274-
" Category:\n" +
1275-
" type: object\n" +
1276-
" properties:\n" +
1277-
" name:\n" +
1278-
" type: string\n" +
1279-
" Recipe:\n" +
1280-
" type: object\n" +
1281-
" properties:\n" +
1282-
" category:\n" + // This is the property with the nullable oneOf
1283-
" description: 'This should become nullable'\n" +
1284-
" oneOf:\n" +
1285-
" - $ref: '#/components/schemas/Category'\n" +
1286-
" - type: 'null'\n";
1287-
1288-
// 2. Load the spec and run the normalizer (with default rules)
1289-
final OpenAPI openAPI = TestUtils.parseSpec(yaml);
1256+
// Load from YAML file for consistency
1257+
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/nullable-oneof.yaml");
1258+
// Run the normalizer
12901259
new OpenAPINormalizer(openAPI, Collections.emptyMap()).normalize();
1291-
1292-
// 3. Get the specific schema property that should have been fixed
1260+
1261+
// Case 1: Recipe -> ref + null
12931262
Schema recipeSchema = openAPI.getComponents().getSchemas().get("Recipe");
12941263
Schema categoryProperty = (Schema) recipeSchema.getProperties().get("category");
1295-
1296-
// 4. Assert that the fix worked as expected
1297-
assertNotNull(categoryProperty, "The category property should not be null.");
1298-
1299-
// Key Assertions:
1300-
assertTrue(categoryProperty.getNullable(), "The property should be marked as nullable.");
1301-
assertNull(categoryProperty.getOneOf(), "The oneOf should have been removed.");
1302-
assertEquals(categoryProperty.get$ref(), "#/components/schemas/Category", "The $ref should be preserved.");
1303-
1304-
// Also check that metadata was preserved
1305-
assertEquals(categoryProperty.getDescription(), "This should become nullable'");
1264+
assertNotNull(categoryProperty);
1265+
assertTrue(categoryProperty.getNullable());
1266+
assertNull(categoryProperty.getOneOf());
1267+
assertEquals(categoryProperty.get$ref(), "#/components/schemas/Category");
1268+
1269+
// Case 2: Inline object + null
1270+
Schema inline = openAPI.getComponents().getSchemas().get("CategoryInline");
1271+
assertTrue(inline.getNullable());
1272+
assertNull(inline.getOneOf());
13061273
}
13071274

13081275
public static class RemoveRequiredNormalizer extends OpenAPINormalizer {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: API
5+
paths:
6+
/test:
7+
get:
8+
responses:
9+
'200':
10+
description: OK
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/Recipe'
15+
16+
components:
17+
schemas:
18+
Category:
19+
type: object
20+
properties:
21+
name:
22+
type: string
23+
24+
Recipe:
25+
type: object
26+
properties:
27+
category:
28+
description: This should become nullable
29+
oneOf:
30+
- $ref: '#/components/schemas/Category'
31+
- type: 'null'
32+
33+
CategoryInline:
34+
oneOf:
35+
- type: object
36+
properties:
37+
type:
38+
type: string
39+
- type: 'null'

0 commit comments

Comments
 (0)