Skip to content

Commit 9233b6c

Browse files
committed
address PR feedback
1 parent f651452 commit 9233b6c

6 files changed

Lines changed: 29 additions & 19 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,11 +2363,12 @@ public static boolean isNullTypeSchema(OpenAPI openAPI, Schema schema) {
23632363
return false;
23642364
}
23652365

2366-
// OpenAPI 3.0.x: empty nullable object is a null-type schema
2366+
// OpenAPI 3.0.x: nullable object with no properties or constraints expresses nullability
23672367
if (!(schema instanceof JsonSchema) // 3.0.x only
23682368
&& "object".equals(schema.getType())
23692369
&& Boolean.TRUE.equals(schema.getNullable())
2370-
&& schema.get$ref() == null) {
2370+
&& schema.get$ref() == null
2371+
&& schema.getAdditionalProperties() == null) {
23712372
return true;
23722373
}
23732374

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4327,9 +4327,9 @@ public void testJspecify(String library, boolean useSpringBoot4, boolean hasJspe
43274327
}
43284328

43294329
@Test(description = "anyOf with $ref and {type: object, nullable: true} should resolve to typed nullable field, not Object")
4330-
public void testAnyOfNullableObjectSentinelResolvesToTypedField() {
4330+
public void testAnyOfBareNullableObjectResolvesToTypedField() {
43314331
Map<String, File> files = generateFromContract(
4332-
"src/test/resources/bugs/issue_anyof_nullable_object_sentinel.yaml",
4332+
"src/test/resources/bugs/issue_anyof_bare_nullable_object.yaml",
43334333
JavaClientCodegen.JERSEY3);
43344334

43354335
JavaFileAssert.assertThat(files.get("Order.java"))

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -655,19 +655,23 @@ public void isNullTypeSchemaTest() {
655655
schema = openAPI.getComponents().getSchemas().get("JustDescription");
656656
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
657657

658-
// {type: "object", nullable: true} with no properties is a null sentinel (apispec 6.7.1+)
659-
schema = openAPI.getComponents().getSchemas().get("NullableObjectSentinel");
658+
// {type: "object", nullable: true} with no properties/constraints expresses nullability (OAS 3.0.3)
659+
schema = openAPI.getComponents().getSchemas().get("BareNullableObject");
660660
assertTrue(ModelUtils.isNullTypeSchema(openAPI, schema));
661661

662-
// {type: "object", nullable: true} WITH properties is a real object, not a null sentinel
662+
// {type: "object", nullable: true} WITH properties is a real object, not expressing nullability
663663
schema = openAPI.getComponents().getSchemas().get("NullableObjectWithProperties");
664664
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
665+
666+
// {type: "object", nullable: true, additionalProperties: ...} is a nullable map, not expressing nullability
667+
schema = openAPI.getComponents().getSchemas().get("NullableObjectMap");
668+
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
665669
}
666670

667671
@Test
668-
public void isNullTypeSchemaInlineAnyOfSentinelTest() {
672+
public void isNullTypeSchemaTestBareNullableObject() {
669673
OpenAPI openAPI = TestUtils.parseSpec(
670-
"src/test/resources/bugs/issue_anyof_nullable_object_sentinel.yaml");
674+
"src/test/resources/bugs/issue_anyof_bare_nullable_object.yaml");
671675
Schema order = (Schema) openAPI.getComponents().getSchemas().get("Order");
672676
Schema shippingProp = (Schema) order.getProperties().get("shippingAddress");
673677
assertNotNull(shippingProp.getAnyOf(), "shippingAddress should have anyOf");
@@ -679,10 +683,10 @@ public void isNullTypeSchemaInlineAnyOfSentinelTest() {
679683
Schema refSchema = anyOf.get(0);
680684
assertFalse(ModelUtils.isNullTypeSchema(openAPI, refSchema));
681685

682-
// second sub-schema is {type: object, nullable: true} — the sentinel
683-
Schema sentinel = anyOf.get(1);
684-
assertTrue(ModelUtils.isNullTypeSchema(openAPI, sentinel),
685-
"Should recognize {type: object, nullable: true} as null sentinel");
686+
// second sub-schema is {type: object, nullable: true} — expresses nullability
687+
Schema bareNullableObject = anyOf.get(1);
688+
assertTrue(ModelUtils.isNullTypeSchema(openAPI, bareNullableObject),
689+
"{type: object, nullable: true} with no properties/constraints expresses nullability");
686690
}
687691

688692
@Test
@@ -725,9 +729,9 @@ public void isNullTypeSchemaTestWith31Spec() {
725729
schema = openAPI.getComponents().getSchemas().get("JustDescription");
726730
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
727731

728-
// In 3.1, {type: object, nullable: true} is NOT a null sentinel — it's a real
732+
// In 3.1, {type: object, nullable: true} is NOT a null type — it's a real
729733
// nullable object. Nullability in 3.1 is expressed via type: ["object", "null"].
730-
schema = openAPI.getComponents().getSchemas().get("NullableObjectSentinel");
734+
schema = openAPI.getComponents().getSchemas().get("BareNullableObject");
731735
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
732736
}
733737

modules/openapi-generator/src/test/resources/3_0/null_schema_test.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,17 @@ components:
105105
- $ref: '#/components/schemas/StringRef'
106106
JustDescription:
107107
description: A schema with just description
108-
NullableObjectSentinel:
108+
BareNullableObject:
109109
type: object
110110
nullable: true
111111
NullableObjectWithProperties:
112112
type: object
113113
nullable: true
114114
properties:
115115
name:
116-
type: string
116+
type: string
117+
NullableObjectMap:
118+
type: object
119+
nullable: true
120+
additionalProperties:
121+
type: string

modules/openapi-generator/src/test/resources/3_1/null_schema_test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,6 @@ components:
105105
- $ref: '#/components/schemas/StringRef'
106106
JustDescription:
107107
description: A schema with just description
108-
NullableObjectSentinel:
108+
BareNullableObject:
109109
type: object
110110
nullable: true

modules/openapi-generator/src/test/resources/bugs/issue_anyof_nullable_object_sentinel.yaml renamed to modules/openapi-generator/src/test/resources/bugs/issue_anyof_bare_nullable_object.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
openapi: 3.0.3
22
info:
3-
title: anyOf nullable object sentinel test
3+
title: anyOf bare nullable object test
44
description: >
55
Tests that anyOf with a $ref and {type: object, nullable: true} (no properties)
66
is simplified to a typed nullable field, not Object.

0 commit comments

Comments
 (0)