Repository navigation
Add option to generate unions for mutually exclusive properties - #1854
Open
george-oakling wants to merge 2 commits into
Open
george-oakling wants to merge 2 commits into
george-oakling wants to merge 2 commits into
Conversation
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 detectedLatest commit: b4d29fc The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Contributor
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A schema can use
notto forbid setting two of its properties together. This is how protobuf-to-OpenAPI converters encode a oneof. Because everynotgeneratesany, the members come out as independent optional properties, so TypeScript accepts objects that set several of them. This PR adds an opt-inexclusivePropertiesFromNotoption. It generates such a group as a union in which only one member can be set.Example
Before, and still the default:
With
exclusivePropertiesFromNot: true:{ kyc: "…", kyb: "…" }no longer type-checks, and members can still be read directly:request.kycisstring | undefined. A schema can also forbid setting none of the members:In that case the all-unset branch is dropped, so exactly one member must be set.
Option
The option is
exclusivePropertiesFromNotingenerateApiand--exclusive-properties-from-noton the CLI. It is off by default because it changes generated types, which matches howemptyNotAsNeverwas added. The changeset is a minor bump.What is recognized
notis read as a list of forbidden cases. It can be a single case or several cases undernot: { anyOf: [...] }. Three cases are recognized:{ required: [a, b] }:aandbare 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:
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:
a/bandb/c);{ required: [a, b, c] }, which forbids only all three together;not.Implementation:
src/schema-parser/util/exclusive-properties.tsfinds the groups.NotSchemaParsergenerates one union per group. Each branch is an inline object schema in which the other members arefalseschemas. Member docs,$refs andadditionalPropertiestherefore render as they do anywhere else.ComplexSchemaParserleaves the grouped properties out of the object it intersects with the result, so they are not generated twice.Tests
tests/spec/not-exclusive-propertiescovers:required, and an undeclared name.@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 noneveris generated.vitest run: 314 tests in 105 files pass (312 in 104 before), with no snapshot changes outside the new spec.biome checkandtsc --noEmitreport the same diagnostics as onmain.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
oneOfbranches before generation.tscconfirms 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
exclusivePropertiesFromNotoption that turnsnotschemas making object properties mutually exclusive into TypeScript unions, so assigning two members at once no longer type-checks.never; an all-unset branch is added unless the schema also forbids setting none.not: { anyOf }; overlapping pairs, largerrequiredlists, and undeclared names keep generating the same output as before.x-omitempty: false, booleanrequired) and keeps the grouped properties in the object when a customcomplexNotparser is used.--exclusive-properties-from-notto 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.