Skip to content

Add option to generate unions for mutually exclusive properties - #1854

Open
george-oakling wants to merge 2 commits into
acacode:mainfrom
george-oakling:exclusive-properties-from-not
Open

george-oakling wants to merge 2 commits into
acacode:mainfrom
george-oakling:exclusive-properties-from-not

Conversation

@george-oakling

@george-oakling george-oakling commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

A schema can use not to forbid setting two of its properties together. This is how protobuf-to-OpenAPI converters encode a oneof. Because every not generates any, the members come out as independent optional properties, so TypeScript accepts objects that set several of them. This PR adds an opt-in exclusivePropertiesFromNot option. It generates such a group as a union in which only one member can be set.

Example

ComplianceRequest:
  type: object
  additionalProperties: false
  properties:
    reference: { type: string }
    kyc: { type: string }
    kyb: { type: string }
  not:
    required: [kyc, kyb]

Before, and still the default:

export type ComplianceRequest = {
  reference?: string;
  kyc?: string;
  kyb?: string;
};

With exclusivePropertiesFromNot: true:

export type ComplianceRequest = (
  | {
      kyc: string;
      kyb?: never;
    }
  | {
      kyc?: never;
      kyb: string;
    }
  | {
      kyc?: never;
      kyb?: never;
    }
) & {
  reference?: string;
};

{ kyc: "…", kyb: "…" } no longer type-checks, and members can still be read directly: request.kyc is string | undefined. A schema can also forbid setting none of the members:

not:
  anyOf:
    - required: [kyc, kyb]
    - not: { anyOf: [{ required: [kyc] }, { required: [kyb] }] }

In that case the all-unset branch is dropped, so exactly one member must be set.

Option

The option is exclusivePropertiesFromNot in generateApi and --exclusive-properties-from-not on the CLI. It is off by default because it changes generated types, which matches how emptyNotAsNever was added. The changeset is a minor bump.

What is recognized

not is read as a list of forbidden cases. It can be a single case or several cases under not: { anyOf: [...] }. Three cases are recognized:

  • { required: [a, b] }: a and b are both set.
  • { allOf: [{ anyOf: [{ required: [a] }, …] }, { not: { oneOf: [{ required: [a] }, …] } }] }: some of the names are set, but not exactly one. Large groups are written this way because the number of pairs grows quadratically.
  • { not: { anyOf: [{ required: [a] }, …] } }: none of the names is set.

A set of properties forms a group only when all three of these hold:

  • the rules forbid every pair of its members together;
  • no member is forbidden together with a property outside the group;
  • every member is a declared property that is not in required.

Each group becomes one union. The other properties stay in an object type that is intersected with the union(s). Everything else generates the same output as before, and a type is narrowed only by rules it expresses exactly. That includes:

  • overlapping pairs (a/b and b/c);
  • { required: [a, b, c] }, which forbids only all three together;
  • names that are not declared properties;
  • every other not.

Implementation:

  • src/schema-parser/util/exclusive-properties.ts finds the groups.
  • NotSchemaParser generates one union per group. Each branch is an inline object schema in which the other members are false schemas. Member docs, $refs and additionalProperties therefore render as they do anywhere else.
  • ComplexSchemaParser leaves the grouped properties out of the object it intersects with the result, so they are not generated twice.

Tests

  • tests/spec/not-exclusive-properties covers:
    • an optional pair and a required pair;
    • two groups in one schema;
    • a three-member group;
    • the five-member "some but not exactly one" form, with and without the "none set" rule;
    • a one-member required group;
    • three schemas that stay unchanged: overlapping pairs, a three-name required, and an undeclared name.
  • The option-on test snapshots the output. It then type-checks the output together with assignments marked @ts-expect-error, such as two members set or a required group with no member set. With the option off, those directives are unused and the check fails. The default test checks that no never is generated.
  • vitest run: 314 tests in 105 files pass (312 in 104 before), with no snapshot changes outside the new spec. biome check and tsc --noEmit report the same diagnostics as on main.

I also ran the option on a 740-operation document generated from protobuf. It has 57 schemas that use this encoding: pairs, required groups, and two groups of 63 and 144 members in the "some but not exactly one" form. With the option on, only the types of those 57 schemas change. Before this option, we got these types by rewriting the same rules into explicit oneOf branches before generation. tsc confirms that each of the 56 component types is mutually assignable with the rewritten one and that every member is still accessible on it. The 57th schema is an inline request body; its output differs from the rewritten one only in the order of the intersection.

🤖 Generated with Claude Code


Summary by cubic

Adds an opt-in exclusivePropertiesFromNot option that turns not schemas making object properties mutually exclusive into TypeScript unions, so assigning two members at once no longer type-checks.

  • Generates one union branch per member with the others marked never; an all-unset branch is added unless the schema also forbids setting none.
  • Recognizes forbidden pairs and the "some set, but not exactly one" form, both alone and grouped under not: { anyOf }; overlapping pairs, larger required lists, and undeclared names keep generating the same output as before.
  • Leaves out group members that are required at the property level (x-omitempty: false, boolean required) and keeps the grouped properties in the object when a custom complexNot parser is used.
  • Adds --exclusive-properties-from-not to the CLI and leaves the option off by default, so existing generated types are unchanged unless it is enabled.

Migration

Existing output is unaffected. Enable the new option only if you want the new union types.

Written for commit b4d29fc. Summary will update on new commits.

View guided diff

A schema can forbid setting two of its properties together with `not`,
for example `not: { required: [a, b] }`. Protobuf-to-OpenAPI converters
write oneofs this way. NotSchemaParser generates `any` for every `not`,
so the members came out as plain optional properties and TypeScript
accepted objects that set several of them.

With `exclusivePropertiesFromNot` (`--exclusive-properties-from-not`),
a group of properties that `not` makes mutually exclusive generates a
union with one branch per member, in which the other members are
`never`. A rule that also forbids setting none of them drops the
all-unset branch. Recognized forms are forbidden pairs and "some set,
but not exactly one", alone or listed in a `not: { anyOf }`. Rules that
do not form a complete group, and every other `not`, generate what they
did before.

The option is off by default because it changes generated types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b4d29fc

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 Minor

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

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 10 files

Reply to a comment to ask cubic a question or push back. It learns from your replies.

View guided diff | Re-trigger cubic

A group member that `x-omitempty: false` or a boolean `required` marks as
required stays a required property: the group check now asks
`isPropertyRequired`, as ObjectSchemaParser does. With a custom
`complexNot` parser, which does not generate the union, the object keeps
the grouped properties.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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