Skip to content

fix(openapi-typescript): emit \ as optional property (#2664)#2835

Open
EduardF1 wants to merge 1 commit into
openapi-ts:mainfrom
EduardF1:fix/defs-optional-property
Open

fix(openapi-typescript): emit \ as optional property (#2664)#2835
EduardF1 wants to merge 1 commit into
openapi-ts:mainfrom
EduardF1:fix/defs-optional-property

Conversation

@EduardF1

Copy link
Copy Markdown

Changes

Fixes #2664.

The generated $defs container is currently emitted as a required property of the surrounding object type. Because $defs only hosts reusable subschema definitions (it never appears in actual request/response data), requiring it forces consumers to supply a $defs value whenever they use the generated schema as an input type, producing spurious type errors.

This change marks the emitted $defs property as optional (adds a ? question token). $refs that index into $defs (e.g. components["schemas"]["OtherObject"]["$defs"]["nestedDef"]) continue to resolve, because indexed-access types work identically on optional properties — so ref resolution is fully preserved.

Before

Object: {
    rootDef?: $defs["StringType"];
    $defs: {            // required — must be supplied on input
        hasDefs: boolean;
    };
};

After

Object: {
    rootDef?: $defs["StringType"];
    $defs?: {           // optional — no longer required on input
        hasDefs: boolean;
    };
};

How to review

  • packages/openapi-typescript/src/transform/schema-object.ts — the emitted $defs PropertySignature now uses QUESTION_TOKEN instead of undefined.
  • Existing snapshots in test/transform/schema-object/object.test.ts and test/index.test.ts (including the JSONSchema > $defs are respected case that exercises refs into $defs) updated from $defs: to $defs?:. These assertions only pass with the fix in place.

Tests run

  • vitest run test/transform/schema-object/object.test.ts test/index.test.ts56 passed
  • vitest run test/transform test/discriminators.test.ts test/node-api.test.ts194 passed (validated before submission)
  • biome check on the changed files → clean

A changeset (openapi-typescript patch) is included.

…2664)

The generated $defs container was emitted as a required property,
forcing consumers to supply it when using a schema as an input type
even though $defs only hosts reusable subschema definitions. Mark it
optional so it is no longer required on input; $refs that index into
$defs continue to resolve.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@EduardF1 EduardF1 requested a review from a team as a code owner June 26, 2026 14:22
@EduardF1 EduardF1 requested a review from gzm0 June 26, 2026 14:22
@changeset-bot

changeset-bot Bot commented Jun 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 004105f

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

This PR includes changesets to release 1 package
Name Type
openapi-typescript 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

@netlify

netlify Bot commented Jun 26, 2026

Copy link
Copy Markdown

👷 Deploy request for openapi-ts pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 004105f

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.

$defs is getting required part of the schema

1 participant