Skip to content

Commit 6f4fe1b

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

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,12 @@ 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+
if (Boolean.TRUE.equals(schema.getNullable())) {
1416+
unaliased.setNullable(true);
1417+
}
1418+
return unaliased;
14141419
}
14151420
}
14161421
return schema;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,22 @@ 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+
245261
/**
246262
* Issue https://github.com/OpenAPITools/openapi-generator/issues/1624.
247263
* ModelUtils.isFreeFormObject() should not throw an NPE when passed an empty

0 commit comments

Comments
 (0)