Skip to content

fix(openapi-typescript): allOf composition to use required from subschemas - #2858

Open
split wants to merge 1 commit into
openapi-ts:mainfrom
split:fix-allof-required-composition
Open

fix(openapi-typescript): allOf composition to use required from subschemas#2858
split wants to merge 1 commit into
openapi-ts:mainfrom
split:fix-allof-required-composition

Conversation

@split

@split split commented Sep 4, 2026

Copy link
Copy Markdown

This is my first contribution attempt to the project, so let's start with a big thank you. This project has greatly helped us maintain trust between changes in our internal services that share the same monorepo, while getting rid of the runtime ties.

During that transition, we noticed that some of our service specs couldn't be turned into usable types, and it was often related to allOf usage. This MR is an attempt to make those resulting types reflect the spec more closely.

Changes

  • Fix allOf composition to apply required properties from $ref-resolved subschemas.
  • Omit empty members that produced meaningless & unknown intersections.

This continues the work done for the #657 and fixes most of the allOf required intersection issues that were mentioned in the comments.

How to Review

Added a bunch of unit tests that should cover most of the cases. Examples also show quite nicely real-life use cases that were broken, but now produce meaningful WithRequired types.

Couple examples:

Following schema in examples/digital-ocean-api/shared/meta.yml

type: object
properties:
  meta:
    allOf:
      - $ref: './models/meta_properties.yml'
      - required:
          - total
required:
  - meta

will now produce:

-            meta: components["schemas"]["meta_properties"] & unknown;
+            meta: WithRequired<components["schemas"]["meta_properties"], "total">;

Similar way the different versions of the domain_record now make more sense:

-        domain_record_a: components["schemas"]["domain_record"] & unknown;
-        domain_record_aaaa: components["schemas"]["domain_record"] & unknown;
-        domain_record_caa: components["schemas"]["domain_record"] & unknown;
-        domain_record_cname: components["schemas"]["domain_record"] & unknown;
-        domain_record_mx: components["schemas"]["domain_record"] & unknown;
-        domain_record_ns: components["schemas"]["domain_record"] & unknown;
-        domain_record_soa: components["schemas"]["domain_record"] & unknown;
-        domain_record_srv: components["schemas"]["domain_record"] & unknown;
-        domain_record_txt: components["schemas"]["domain_record"] & unknown;
+        domain_record_a: WithRequired<components["schemas"]["domain_record"], "type
" | "name" | "data">;
+        domain_record_aaaa: WithRequired<components["schemas"]["domain_record"], "t
ype" | "name" | "data">;
+        domain_record_caa: WithRequired<components["schemas"]["domain_record"], "ty
pe" | "name" | "data" | "flags" | "tag">;
+        domain_record_cname: WithRequired<components["schemas"]["domain_record"], "
type" | "name" | "data">;
+        domain_record_mx: WithRequired<components["schemas"]["domain_record"], "typ
e" | "data" | "priority">;
+        domain_record_ns: WithRequired<components["schemas"]["domain_record"], "typ
e" | "name" | "data" | "flags" | "tag">;
+        domain_record_soa: WithRequired<components["schemas"]["domain_record"], "ty
pe" | "ttl">;
+        domain_record_srv: WithRequired<components["schemas"]["domain_record"], "ty
pe" | "name" | "data" | "priority" | "port" | "flags" | "tag">;
+        domain_record_txt: WithRequired<components["schemas"]["domain_record"], "ty
pe" | "name" | "data" | "flags" | "tag">;

During this I noticed that the discriminators will mutate the schema, causing the snapshot updating to fail in interactive mode. I can open a separate MR to remedy that issue.

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

@split
split requested a review from a team as a code owner September 4, 2026 11:30
@split
split requested a review from gzm0 September 4, 2026 11:30
@netlify

netlify Bot commented Sep 4, 2026

Copy link
Copy Markdown

👷 Deploy request for openapi-ts pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 5e66ebe

@changeset-bot

changeset-bot Bot commented Sep 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5e66ebe

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

Fix allOf composition to apply required properties from $ref-resolved subschemas
and omit empty members that produced meaningless & unknown intersections.
@split
split force-pushed the fix-allof-required-composition branch from f363a53 to 5e66ebe Compare September 4, 2026 14:09
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.

1 participant