fix: recognize {type: object, nullable: true} as null-type schema in OpenAPI 3.0.x
Summary
isNullTypeSchema() does not recognize {type: "object", nullable: true} (with no properties) as a null-type schema. This causes the SIMPLIFY_ONEOF_ANYOF normalizer to fail to simplify anyOf schemas where this pattern is used as the nullable branch, producing Object or synthetic wrapper classes instead of typed nullable fields.
This is a valid OpenAPI 3.0.x idiom for expressing nullability alongside a $ref in anyOf/oneOf. It is produced by apispec >= 6.7.1 (PR #953) and potentially other spec generators.
Root cause
isNullTypeSchema() checks for null-type schemas by looking for:
- No
typefield at all type: "null"(OpenAPI 3.1)- A single-value
nullenum
It did not check for {type: "object", nullable: true} with no properties and no $ref — an empty nullable object whose only purpose is to express "this value can be null" in OpenAPI 3.0.x.
Fix
Add a check to isNullTypeSchema() for empty nullable objects, scoped to OpenAPI 3.0.x only via !(schema instanceof JsonSchema). This follows the same 3.0/3.1 discrimination pattern used elsewhere in the same method.
Once isNullTypeSchema() recognizes this pattern, the existing SIMPLIFY_ONEOF_ANYOF logic in simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema correctly identifies the anyOf as containing one real schema and one null sentinel, and simplifies it to a typed nullable field.
Before (input spec)
shippingAddress:
anyOf:
- $ref: '#/components/schemas/Address'
- type: object
nullable: true
Before (generated Java)
private OrderShippingAddress shippingAddress; // synthetic wrapper class
After (generated Java)
private JsonNullable<Address> shippingAddress; // typed nullable field
Why this pattern is spec-correct
The OpenAPI 3.0.3 spec states that nullable: true only takes effect when type is "explicitly defined within the same Schema Object." Since $ref resolves to a different Schema Object, nullable: true alongside allOf: [{$ref: ...}] is technically ineffective. The anyOf approach with a nullable empty object is the correct OpenAPI 3.0.x idiom — see analysis by Dan Ott (cited by apispec maintainers).
Test coverage
| Test | What it verifies |
|---|---|
isNullTypeSchemaTest | 3.0 sentinel → true; sentinel with properties → false |
isNullTypeSchemaTestWith31Spec | 3.1 sentinel → false (not a null sentinel in 3.1) |
isNullTypeSchemaInlineAnyOfSentinelTest | Inline anyOf sub-schema sentinel recognized |
testAnyOfNullableObjectSentinelResolvesToTypedField | End-to-end Java codegen: Address field, no OrderShippingAddress wrapper |
Impact
Affects any OpenAPI 3.0.x spec using anyOf/oneOf with {type: "object", nullable: true} as the nullable branch, including all specs generated by apispec >= 6.7.1 (current latest: 6.10.0). All language targets that rely on SIMPLIFY_ONEOF_ANYOF should benefit.
Related issues
- #16466 —
isNullTypeSchemaincorrectly equates missingtypewith nullable - #19439 — Normalizer mutates shared schema objects during
anyOfsimplification - #21042 —
SIMPLIFY_ONEOF_ANYOFskips valid single-subschema cases - apispec #952 / #953 — apispec changed output from
allOf+nullabletoanyOfwith nullable sentinel
Test plan
-
ModelUtilsTest#isNullTypeSchemaTestpasses -
ModelUtilsTest#isNullTypeSchemaTestWith31Specpasses (no 3.1 regression) -
ModelUtilsTest#isNullTypeSchemaInlineAnyOfSentinelTestpasses -
JavaClientCodegenTest#testAnyOfNullableObjectSentinelResolvesToTypedFieldpasses
🤖 Generated with Claude Code
Summary by cubic
Fixes null-type detection for OpenAPI 3.0.x when anyOf/oneOf uses {type: object, nullable: true} as the null branch. Generators now produce typed nullable fields instead of Object or synthetic wrappers.
- Bug Fixes
- Update
ModelUtils.isNullTypeSchema()to treat a 3.0.x nullable empty object with no properties, noadditionalProperties, and no$refas a null sentinel; objects with properties or maps are not treated as null. This does not apply to 3.1. - Add tests for 3.0/3.1 (including properties/maps cases) and an end-to-end Java test using
bugs/issue_anyof_bare_nullable_object.yamlvalidating$ref+ sentinel collapses to a single nullable typed field with no wrapper.
- Update