fix: nullable not respected for objects with nullable properties #533
Summary
Fixes #533 - Resolves incorrect null handling for nullable parent objects containing nullable child properties.
Problem
When a schema defined a nullable object (type: object, nullable: true) with child properties that were also nullable, the generator was incorrectly adding an additional | null to the parent type. This happened because isNullMissingInType was detecting the | null from nested properties and incorrectly concluding that the parent type needed null added.
Example Issue
Given this schema:
{
"type": "object",
"nullable": true,
"properties": {
"email": { "type": "string", "nullable": true }
}
}
Before: Generated redundant | null even though the parent was already properly nullable
After: Correctly generates { email?: string | null } | null
Solution
Refactored the isNullMissingInType method in src/schema-parser/schema-utils.ts to distinguish between:
- Root-level nullable types (union with
nullat the top level) - Types with nested nullable properties (objects containing prop:
string | null)
The fix uses regex patterns to detect only root-level null unions:
- Pattern ending with
| null - Pattern starting with
null | - Exact match of
null
This prevents false positives from nested nullable properties while correctly identifying when the parent type actually needs | null added.
Changes
Core Fix
-
src/schema-parser/schema-utils.ts: ImprovedisNullMissingInTypelogic with regex-based root-level null detection
Build Configuration
-
package.json: Changed ESM output extensions from .js to .mjs for better module resolution
Test Coverage
- Added new test suite:
tests/spec/nullable-parent-with-nullable-children/ - Tests cover nullable parent objects with nullable child properties, nested nullable objects, and complex scenarios
- Updated snapshots across multiple test suites to reflect correct nullable handling
Testing
All existing tests pass with updated snapshots showing correct nullable type generation. New test suite specifically validates the fix for nested nullable scenarios.
[!NOTE] Fixes null handling by only adding
| nullat the root type level, adds targeted tests, and updates snapshots accordingly.
- Core Fix
- Refactor
src/schema-parser/schema-utils.ts:isNullMissingInTypeto detect only root-levelnullin unions (regex-based), avoiding false positives from nested nullable properties.safeAddNullToTypenow leverages the corrected check to prevent duplicate| nullon parent object types.- Tests
- Add
tests/spec/nullable-parent-with-nullable-children/suite with schema and snapshot validating nullable parent + nullable children behavior.- Update snapshots (
tests/__snapshots__/extended.test.ts.snap,tests/__snapshots__/simple.test.ts.snap,tests/spec/additional-properties-2.0/__snapshots__/basic.test.ts.snap) to reflect corrected nullability (e.g., unions withnull, nullable record values).- Meta
- Add changeset
.changeset/giant-hats-work.md(patch).Written by Cursor Bugbot for commit 6db96156cd5b5e1c6cac7cba35f7762efb777c63. This will update automatically on new commits. Configure here.