fix: nullable not respected for objects with nullable properties #533 #1550
+261
−79
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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| nullto the parent type. This happened becauseisNullMissingInTypewas detecting the| nullfrom 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
| nulleven though the parent was already properly nullableAfter: Correctly generates
{ email?: string | null } | nullSolution
Refactored the
isNullMissingInTypemethod insrc/schema-parser/schema-utils.tsto distinguish between:nullat the top level)string | null)The fix uses regex patterns to detect only root-level null unions:
| nullnull |nullThis prevents false positives from nested nullable properties while correctly identifying when the parent type actually needs
| nulladded.Changes
Core Fix
src/schema-parser/schema-utils.ts: ImprovedisNullMissingInTypelogic with regex-based root-level null detectionBuild Configuration
package.json: Changed ESM output extensions from .js to .mjs for better module resolutionTest Coverage
tests/spec/nullable-parent-with-nullable-children/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.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/spec/nullable-parent-with-nullable-children/suite with schema and snapshot validating nullable parent + nullable children behavior.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)..changeset/giant-hats-work.md(patch).Written by Cursor Bugbot for commit 6db9615. This will update automatically on new commits. Configure here.