fix: nullable not respected for objects with nullable properties #533#1550
fix: nullable not respected for objects with nullable properties #533#1550bankyadam wants to merge 7 commits intoacacode:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 9f211a3 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
bugbot run |
|
@bankyadam LGTM! Thanks! |
|
Hello, @bankyadam. I am writing to check whether you are able to resolve merge conflicts in your PR? It seems like you need to reabse only one small code snippet and it won't take long time. If not, maybe I can rebase this PR for you? |
|
@smorimoto I have started the AI code review. It will take a few minutes to complete. |
There was a problem hiding this comment.
1 issue found across 11 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/schema-parser/schema-utils.ts">
<violation number="1" location="src/schema-parser/schema-utils.ts:102">
P2: The regex check treats nested `| null |` as root-level null, so nullable parent object types can still be skipped when a child property union contains `null` in the middle.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
| const hasRootLevelNull = new RegExp( | ||
| `(^|\\|)\\s*${nullKeyword}\\s*(\\||$)`, | ||
| ).test(type); |
There was a problem hiding this comment.
P2: The regex check treats nested | null | as root-level null, so nullable parent object types can still be skipped when a child property union contains null in the middle.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/schema-parser/schema-utils.ts, line 102:
<comment>The regex check treats nested `| null |` as root-level null, so nullable parent object types can still be skipped when a child property union contains `null` in the middle.</comment>
<file context>
@@ -85,14 +85,25 @@ export class SchemaUtils {
+ // Match null bounded by pipes and/or start/end of string
+ // This avoids false positives from nested nullable properties like { prop: string | null }
+ const nullKeyword = this.config.Ts.Keyword.Null;
+ const hasRootLevelNull = new RegExp(
+ `(^|\\|)\\s*${nullKeyword}\\s*(\\||$)`,
+ ).test(type);
</file context>
| const hasRootLevelNull = new RegExp( | |
| `(^|\\|)\\s*${nullKeyword}\\s*(\\||$)`, | |
| ).test(type); | |
| const hasRootLevelNull = (() => { | |
| let depth = 0; | |
| let token = ""; | |
| for (const char of type) { | |
| if (char === "{" || char === "(" || char === "[" || char === "<") depth++; | |
| if (char === "}" || char === ")" || char === "]" || char === ">") { | |
| depth = Math.max(depth - 1, 0); | |
| } | |
| if (char === "|" && depth === 0) { | |
| if (token.trim() === nullKeyword) return true; | |
| token = ""; | |
| continue; | |
| } | |
| token += char; | |
| } | |
| return token.trim() === nullKeyword; | |
| })(); |
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.