Skip to content

Handle required-only allOf constraints - #2850

Open
darkbasic wants to merge 1 commit into
openapi-ts:mainfrom
darkbasic:fix/required-only-allof
Open

Handle required-only allOf constraints#2850
darkbasic wants to merge 1 commit into
openapi-ts:mainfrom
darkbasic:fix/required-only-allof

Conversation

@darkbasic

Copy link
Copy Markdown
Contributor

Summary

Handle allOf members that only make existing object properties required.

Fixes #1474
Related to #1520 and #2570

Problem

Given a schema such as:

allOf:
  - $ref: "#/components/schemas/Base"
  - type: object
    required: [name]

openapi-typescript generated Base & Record<string, never>. The second member is a presence constraint, but it was transformed as an empty object, making the resulting type effectively unusable.

Solution

Exact { required: [...] } and { type: object, required: [...] } members are translated through the existing parent required flow. For referenced schemas this produces WithRequired<Base, "name"> instead of an empty-object intersection.

The implementation is intentionally conservative:

  • Every required key must exist in the retained object schemas.
  • Typed constraints require another retained, non-nullable object assertion.
  • Schemas with extra keywords, callbacks, renamed or filtered properties, unions, nullability, or early-return types keep the previous behavior.
  • Recursive allOf references are followed with cycle protection.
  • Existing discriminator handling is preserved while allowing additional non-discriminator fields to become required.

The main logic is in transform/schema-object.ts. The composition tests include generated TypeScript compilation checks, and the DigitalOcean fixture was regenerated because it contains several real examples of this schema pattern.

Validation

  • 324 package tests passed
  • TypeScript and generated-example checks passed
  • Build and package export validation passed
  • DigitalOcean generated fixture compiles

Notes

While the original implementation has been written by me, after several rounds of validation by GPT-5.6 Sol it found so many corner cases that it basically rewrote it from scratch.

Translate exact required-only allOf members through the existing required-property flow while preserving unsafe, annotated, polymorphic, and transformed schemas.

Fixes openapi-ts#1474

Related to openapi-ts#1520 and openapi-ts#2570
@darkbasic
darkbasic requested a review from a team as a code owner July 31, 2026 08:30
@darkbasic
darkbasic requested a review from drwpow July 31, 2026 08:30
@netlify

netlify Bot commented Jul 31, 2026

Copy link
Copy Markdown

👷 Deploy request for openapi-ts pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit f032c8e

@changeset-bot

changeset-bot Bot commented Jul 31, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f032c8e

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

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.

allOff with required returning Record<string, never>

1 participant