Skip to content

Commit 1adabdc

Browse files
committed
Keep nullability when inlining ref
1 parent 777b7ee commit 1adabdc

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,11 @@ public static Schema unaliasSchema(OpenAPI openAPI,
14101410
schemaMappings);
14111411
}
14121412
} else {
1413-
return unaliasSchema(openAPI, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())), schemaMappings);
1413+
Schema unaliased = unaliasSchema(openAPI, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())), schemaMappings);
1414+
// Preserve nullable property from the original schema reference
1415+
Schema newSchema = cloneSchema(unaliased, false);
1416+
newSchema.setNullable(schema.getNullable());
1417+
return newSchema;
14141418
}
14151419
}
14161420
return schema;

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,31 @@ public void testAliasedTypeIsNotUnaliasedIfUsedForImportMapping() {
242242
Assert.assertEquals(stringSchema, ModelUtils.unaliasSchema(openAPI, emailSchema, new HashMap<>()));
243243
}
244244

245+
/**
246+
* This test verifies that nullable property is preserved when unaliasing a schema reference.
247+
* See https://github.com/OpenAPITools/openapi-generator/issues/21612
248+
*/
249+
@Test
250+
public void testNullableUnaliasedSchema() {
251+
Schema refSchema = new Schema().$ref("#/components/schemas/MyString").nullable(true);
252+
StringSchema myStringSchema = new StringSchema();
253+
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("MyString", myStringSchema);
254+
255+
Schema unaliased = ModelUtils.unaliasSchema(openAPI, refSchema, new HashMap<>());
256+
257+
Assert.assertTrue(unaliased.getNullable(), "Nullable property should be preserved after unaliasing");
258+
Assert.assertNotSame(unaliased, refSchema, "Unaliased schema should not be the same instance as the original reference");
259+
260+
Schema refSchema2 = new Schema().$ref("#/components/schemas/MyString");
261+
StringSchema myStringSchema2 = new StringSchema();
262+
OpenAPI openAPI2 = TestUtils.createOpenAPIWithOneSchema("MyString", myStringSchema2);
263+
264+
Schema unaliased2 = ModelUtils.unaliasSchema(openAPI2, refSchema2, new HashMap<>());
265+
266+
Assert.assertNull(unaliased2.getNullable(), "Nullable property should be preserved after unaliasing");
267+
Assert.assertNotSame(unaliased2, refSchema2, "Unaliased schema should not be the same instance as the original reference");
268+
}
269+
245270
/**
246271
* Issue https://github.com/OpenAPITools/openapi-generator/issues/1624.
247272
* ModelUtils.isFreeFormObject() should not throw an NPE when passed an empty

0 commit comments

Comments
 (0)