Skip to content

Conversation

@bankyadam
Copy link

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 null at 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: Improved isNullMissingInType logic 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.

@changeset-bot
Copy link

changeset-bot bot commented Dec 10, 2025

🦋 Changeset detected

Latest commit: 6db9615

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
swagger-typescript-api Patch

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nullable: true ignored if type: object and a property has nullable: true

2 participants