Skip to content

fix(what-changed): keep the direction of schemas in exclusive unions - #727

Open
frederikprijck wants to merge 7 commits into
pb33f:mainfrom
frederikprijck:fix/what-changed-oneof-response-usage
Open

frederikprijck wants to merge 7 commits into
pb33f:mainfrom
frederikprijck:fix/what-changed-oneof-response-usage

Conversation

@frederikprijck

@frederikprijck frederikprijck commented Nov 23, 2026 •

Copy link
Copy Markdown

Fixes #726.

Problem

Since #725, a required entry added to an open schema that only responses use is not reported as breaking. That does not apply inside a oneOf: the usage walk marks every branch as used by both requests and responses (schema_usage.go#L266-L267), while allOf and anyOf keep the direction of the enclosing schema.

Being careful with oneOf makes sense in general. A oneOf rejects an instance that matches more than one branch, so changing one branch can change what the union accepts: widening a branch can make an instance match a second one, and narrowing it can leave an instance that matched two branches with only one. That cannot happen when no instance can match two branches. Then oneOf is equivalent to anyOf.

Change

A oneOf keeps the direction of the enclosing schema when no instance can match two of its branches:

  • there is a single branch, or
  • the branches accept different types.

The type check follows chains of $ref and stops at a reference it has already followed. OpenAPI 3.0 ignores the siblings of $ref, so there only the referenced schema counts. In 3.1 they apply as well, so a type next to a $ref narrows the branch. That only happens when the version is known to be 3.1 or later and TransformSiblingRefs is on (the default), because without TransformSiblingRefs the schema model does not read the siblings either. Otherwise only the referenced schema counts.

It reads type and nullable the way the schema model does: a single type only from a YAML string, a type list by its items, and nullable from any YAML boolean. nullable only adds null in OpenAPI 3.0, or when the version is unknown. It only accepts the JSON Schema type names and treats integer and number as overlapping.

Everything else stays shared as before: branches with overlapping types, branches without a type, a type name it does not know, an empty list of types, boolean branches, and a type next to a $ref that has nothing in common with the referenced schema. A reference that cannot be resolved makes the usage of the whole document unknown, as before.

The check only runs when the enclosing direction is not already shared, because it cannot change a shared one.

Because the direction is kept, this applies to requests as well: a required entry removed from a schema in an exclusive union that only requests use is not reported as breaking either.

Tests

  • New schema_usage_oneof_test.go:
    • Relaxed: a single branch, array or object, branches by reference, null or object, type lists, integer and number in one branch, a chain of eight references, a nullable object or a string in OpenAPI 3.0 (also with a quoted 'true'), a nullable object or null in OpenAPI 3.1, a $ref next to a narrower type in OpenAPI 3.1, a reference without a type next to a type, a type next to a reference along the chain, and a required entry removed from a request-only union.
    • Must stay breaking: two objects, two arrays, integer and number, no type, an unquoted type: null, a boolean branch, a type name that is not a JSON type, a $ref next to a type it has nothing in common with, a $ref narrowed to the type of another branch, a $ref next to a type that is not a JSON type, a reference without a type next to a type in OpenAPI 3.0, a narrower type next to a $ref with TransformSiblingRefs off, nullable branches in OpenAPI 3.0 (also spelled True and TRUE), an empty type list with nullable, a union that a request also uses, a required entry removed in a response, and a required entry removed from a request union of two objects.
    • The usage itself for a oneOf that is not a list, a type that is not a name or a list, an empty type list (also with nullable), reference cycles of one and two schemas, a $ref next to a type when the version is unknown, and references that cannot be resolved.
  • One existing row in TestRequiredConservativeSchemaContexts changes: oneOf with a single branch now expects "not breaking" (renamed to exclusive union of one branch).
  • go test ./what-changed/... ./tests/... . passes.
  • The reproduction from what-changed: required property added to a response-only schema is reported as breaking when used inside oneOf #726 reports 0 breaking for 03-oneof with this branch, and 01-direct-open and 02-array-open are unchanged.

The second commit fixes the $ref and type name handling, which a review found after the first commit, and adds the request and OpenAPI 3.0 tests. The commits after it address the review on this PR, one comment each: skipping the check for a shared direction, reading nullable as the model does, reading type as the model does, stopping at a repeated reference instead of after eight, and narrowing by the type next to a $ref in 3.1.

Not covered

Branches of the same type that are told apart by const or enum, as in many discriminated unions, stay shared. Proving those branches exclusive would need a different analysis.

A discriminator anywhere in a document makes the usage of the whole document unknown (#728). Such documents get neither this relaxation nor the one from #725 until that is fixed.

Note

#729 changes the same part of schemaField, and adds fields to usageWalker and functions in the same place, so whichever of the two is merged second has conflicts in schema_usage.go. Each one is resolved by keeping both changes. When the first one is merged, I will resolve it in the second one with a merge commit.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (b9309bc) to head (1f3e494).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #727   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          316       316           
  Lines        40280     40339   +59     
=========================================
+ Hits         40280     40339   +59     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@frederikprijck
frederikprijck force-pushed the fix/what-changed-oneof-response-usage branch from 275207e to 87b154d Compare November 23, 2026 10:18
@frederikprijck frederikprijck changed the title fix(what-changed): keep the response use of schemas in exclusive unions fix(what-changed): keep the direction of schemas in exclusive unions Nov 23, 2026
frederikprijck and others added 2 commits November 23, 2026 15:14
A oneOf accepts an instance only when exactly one branch matches, so the usage
walk treated every branch as used by both requests and responses. A required
entry added to a schema that only responses use stayed breaking whenever the
schema sat in a oneOf.

Branches that cannot match the same instance, because their types are disjoint
or because there is a single branch, cannot interfere with each other, so they
now keep the direction of the oneOf, as anyOf does. Branches that overlap, that
do not state a type, or that mix integer and number keep the shared result.

Co-Authored-By: Claude <[email protected]>
OpenAPI 3.0 ignores the siblings of $ref, so a branch such as
{$ref: Other, type: string} accepts what Other accepts. The exclusivity check read
the sibling type first, so it could treat overlapping branches as exclusive and
relax a change that can break a client. The check now follows the reference
first, which is an upper bound in 3.0 and in 3.1.

It also only accepts the JSON Schema type names, so an unknown name such as
Object no longer counts as a distinct type, and duplicate types inside one branch
no longer count as an overlap.

Exclusive branches keep the direction in requests as well, so a required entry
removed from a request-only union is not breaking either. Tests now cover that,
OpenAPI 3.0 documents, and the cases above.

Co-Authored-By: Claude <[email protected]>
@frederikprijck
frederikprijck force-pushed the fix/what-changed-oneof-response-usage branch from 2acbdc9 to 8969ccb Compare November 23, 2026 14:15

@daveshanley daveshanley left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for fixing #726. I verified that the original array/object example now reports zero breaking changes, and the full Go suite passed. I’d fix the YAML type and nullable handling before merging.

Comment thread what-changed/model/schema_usage.go Outdated
case v3.RefLabel:
ref = utils.NodeAlias(value).Value
case v3.NullableLabel:
nullable = utils.NodeAlias(value).Value == "true"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The 3.0 model decodes true and True to the same boolean. In an object/string oneOf with both branches nullable, changing that spelling alone took the breaking count from one to zero. Could we decode the tagged boolean as the model builder does and use ResolveDocumentVersion to apply nullable for 3.0? In 3.1+, nullable has no effect on the accepted types.

@frederikprijck frederikprijck Nov 26, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, I have fixed that in f231831. Can you take a look? I also ensured to cover it with a test, verified broken behavior before patching it.

Comment thread what-changed/model/schema_usage.go Outdated
var names []string
switch declared.Kind {
case yaml.ScalarNode:
names = []string{declared.Value}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unquoted type: null leaves the schema model’s Type unset, but this reads the scalar text as the type name "null" and reports zero breaking changes. Could we require a YAML string for each type name and a nonempty, valid declaration before adding nullable? Tests for type: 'null', type: null, and type: [] with nullable: true would cover the distinction. JSON Schema type

@frederikprijck frederikprijck Nov 26, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed it in e9ab33b, note that I did not apply the YAML string requirement to the items of a type list, so type: [null] still reads as 'null', because of:

Reading (low model): datamodel/low/base/schema_build.go#L129-L151

  • A single type is only read when it is a YAML string (utils.IsNodeStringValue, L131).
  • For a list, each item's text is taken as is, with no tag check (typeValue.Content[r].Value, L140-L145). An unquoted null inside [null] therefore becomes the string "null".
  • The high model carries that into Type: ["null"] (datamodel/high/base/schema.go#L281-L285) and renders it as type: "null".

Same as with the rest, I covered this in tests, confirmed the broken behavior, and fixed it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for explaining [null]. I confirmed it renders as "null". One type-list case still changes the diff after a round trip. With x-type: &object string and Other: {type: [*object]} beside an object Widget in a response oneOf, requiring Widget.id gives 0 breaking changes. Rendering and reparsing both documents gives 1. The walker reads string, while the low model stores the anchor name object. Please use the same alias-aware decoding in both paths and test that both comparisons report 0.

Comment thread what-changed/model/schema_usage.go Outdated
if node = w.localTarget(ref); node == nil {
return nil, false
}
continue

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In 3.1, type: string beside a $ref to {type: [string, object]} narrows that branch to strings. Beside an object branch, I got one breaking change with the reference and zero with the equivalent inline string schema. Could we intersect the sibling and referenced type constraints in 3.1+, and keep the current reference-only behavior in 3.0? JSON Schema reference semantics

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 1f3e494

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The original 3.1 narrowing case now passes. One equivalent schema still gives a different answer. In the response oneOf, keep the object branch and give the other branch a $ref with sibling type: string. Requiring a property on the object reports 0 if the referenced schema is {}, and 1 if it is true. Both targets accept every instance. Please retain the sibling’s type constraint when the referenced schema is true and test both forms together. JSON Schema boolean semantics

Comment thread what-changed/model/schema_usage.go Outdated
// It reports false when it cannot tell: a schema without a type, a type name that is not a JSON type, a boolean
// schema, a reference that cannot be resolved, or a chain of references longer than maxReferenceDepth.
func (w *usageWalker) instanceTypes(node *yaml.Node) (map[string]bool, bool) {
const maxReferenceDepth = 8

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

With an object branch and a string behind seven local $refs, a required-field addition reported zero breaking changes. Adding an eighth alias to the same string changed the count to one. Could we detect repeated nodes along the reference chain so cycles still fall back and an extra alias preserves the result?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 92729f4 with tests, let me know if this works.

Comment thread what-changed/model/schema_usage.go Outdated
// union accepts in the opposite direction: widening a branch can make an instance match a second one, and
// narrowing it can leave an instance that matched two branches with only one. Branches that no instance can
// match at the same time cannot interfere with each other, so they keep the direction, as with anyOf.
if w.exclusiveBranches(value) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When sameInstance is already sharedSchema, both paths pass the same direction. On a webhook fixture with 128 unions, the usage walk went from 37 to 805 allocations. I tried sameInstance != sharedSchema && w.exclusiveBranches(value) here. It brought the count back to 37 and the focused tests passed.

@frederikprijck frederikprijck Nov 26, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 080afa4. I reproduced it on my own test with 128 webhook unions: 72 allocations on main, 840 before the change and 72 after. That's the same increase of 768 (6 per union) that you saw from 37 to 805.

frederikprijck and others added 5 commits November 26, 2026 09:16
… already shared

When the enclosing direction is already shared, the branches get the same
direction whether they are exclusive or not, so checking them only costs
allocations. Webhooks and callbacks are always shared, and with 128 webhook
unions the usage walk went from 72 allocations to 840. The check now only runs
when its result can change the direction, which brings it back to 72.

Co-Authored-By: Claude <[email protected]>
…n OpenAPI 3.0

The exclusivity check compared the text of nullable with "true", while the
schema model accepts any YAML boolean and parses it with strconv.ParseBool. A
branch with nullable: True or TRUE was not seen as accepting null, so two
nullable branches could look exclusive and a breaking change was not reported.
A quoted 'true' went the other way: the model ignores it, the check did not.

OpenAPI 3.1 removed nullable, so it no longer adds null there. When the
version cannot be resolved, nullable still adds null, which can only make the
result less precise and never hides a breaking change.

Co-Authored-By: Claude <[email protected]>
…ore nullable on an empty type list

The schema model only reads a single type from a YAML string, so an unquoted
type: null declares no type and the branch accepts any instance. The
exclusivity check read the scalar text as the type name "null", so the branch
looked exclusive with an object branch and a breaking change was not reported.

An empty list of types already meant that the types cannot be compared, but
nullable turned it into "only null". It now stays unknown with or without
nullable.

Items of a type list are still read by their text, as the schema model reads
them: type: [null] is "null" there as well.

Co-Authored-By: Claude <[email protected]>
The exclusivity check gave up after following eight references, so a branch
behind seven references was compared by type and the same branch behind eight
was not, which changed the result. It now records the nodes it follows and
stops at one it has seen before, so the length of a chain no longer matters
and a cycle still falls back. The record has room for eight nodes and does
not escape, so common chains do not allocate.

Co-Authored-By: Claude <[email protected]>
… in OpenAPI 3.1

In OpenAPI 3.1 the keywords next to $ref apply as well, so a type next to a
reference narrows what the branch accepts. The exclusivity check only read
the referenced schema, so {$ref: X, type: string} with X accepting strings and
objects overlapped with an object branch, while the same branch written inline
did not. The types along the reference chain are now intersected, which also
gives a type to a reference whose target declares none.

This only applies when the version is known to be 3.1 or later and
TransformSiblingRefs is on, because that is the only configuration in which the
schema model reads the siblings too. Without it, the model reads only the
reference, and so does the check. OpenAPI 3.0 ignores the siblings, so nothing
changes there.

Types along the references that have nothing in common, such as a reference to
an object next to type: string, accept no instance at all. That is more likely
a mistake than a schema, so the branch stays unknown instead of being treated
as exclusive. A type next to a reference that cannot be read makes the branch
unknown as well.

Co-Authored-By: Claude <[email protected]>
@frederikprijck

frederikprijck commented Nov 26, 2026 •

Copy link
Copy Markdown
Author

Thanks for the review.

I applied the fixes in separate commits (1 per comment, so we can more easily iterate or revert if needed), I also updated the PR description to reflect the changes we made.

I will leave the resolution of the comments to you so you can confirm the fix before we resolve the comment.

@daveshanley daveshanley left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the fixes in 1f3e4942. The #726 example now reports zero breaking changes. I also verified the nullable spellings, the longer reference chains, and the return to 37 allocations on the 128-union webhook fixture.

I’m requesting changes before merge. Rendering and reparsing the same documents can still change the breaking count. Please make type-list aliases consistent across the walker and model, and preserve the sibling type when a 3.1 reference points to true. Alias round trip · Boolean reference target

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.

what-changed: required property added to a response-only schema is reported as breaking when used inside oneOf

2 participants