Skip to content

Keep nullability when inlining ref to primitive #21614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,11 @@ public static Schema unaliasSchema(OpenAPI openAPI,
schemaMappings);
}
} else {
return unaliasSchema(openAPI, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())), schemaMappings);
Schema unaliased = unaliasSchema(openAPI, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())), schemaMappings);
// Preserve nullable property from the original schema reference
Schema newSchema = cloneSchema(unaliased, false);
newSchema.setNullable(schema.getNullable());
return newSchema;
}
}
return schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,31 @@ public void testAliasedTypeIsNotUnaliasedIfUsedForImportMapping() {
Assert.assertEquals(stringSchema, ModelUtils.unaliasSchema(openAPI, emailSchema, new HashMap<>()));
}

/**
* This test verifies that nullable property is preserved when unaliasing a schema reference.
* See https://github.com/OpenAPITools/openapi-generator/issues/21612
*/
@Test
public void testNullableUnaliasedSchema() {
Schema refSchema = new Schema().$ref("#/components/schemas/MyString").nullable(true);
StringSchema myStringSchema = new StringSchema();
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("MyString", myStringSchema);

Schema unaliased = ModelUtils.unaliasSchema(openAPI, refSchema, new HashMap<>());

Assert.assertTrue(unaliased.getNullable(), "Nullable property should be preserved after unaliasing");
Assert.assertNotSame(unaliased, refSchema, "Unaliased schema should not be the same instance as the original reference");

Schema refSchema2 = new Schema().$ref("#/components/schemas/MyString");
StringSchema myStringSchema2 = new StringSchema();
OpenAPI openAPI2 = TestUtils.createOpenAPIWithOneSchema("MyString", myStringSchema2);

Schema unaliased2 = ModelUtils.unaliasSchema(openAPI2, refSchema2, new HashMap<>());

Assert.assertNull(unaliased2.getNullable(), "Nullable property should be preserved after unaliasing");
Assert.assertNotSame(unaliased2, refSchema2, "Unaliased schema should not be the same instance as the original reference");
}

/**
* Issue https://github.com/OpenAPITools/openapi-generator/issues/1624.
* ModelUtils.isFreeFormObject() should not throw an NPE when passed an empty
Expand Down