Repository navigation
fix(what-changed): keep the direction of schemas in exclusive unions - #727
frederikprijck wants to merge 11 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #727 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 316 316
Lines 40280 40351 +71
=========================================
+ Hits 40280 40351 +71
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
275207e to
87b154d
Compare
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
2acbdc9 to
8969ccb
Compare
daveshanley
left a comment
There was a problem hiding this comment.
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.
| case v3.RefLabel: | ||
| ref = utils.NodeAlias(value).Value | ||
| case v3.NullableLabel: | ||
| nullable = utils.NodeAlias(value).Value == "true" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| var names []string | ||
| switch declared.Kind { | ||
| case yaml.ScalarNode: | ||
| names = []string{declared.Value} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 astype: "null".
Same as with the rest, I covered this in tests, confirmed the broken behavior, and fixed it.
There was a problem hiding this comment.
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.
| if node = w.localTarget(ref); node == nil { | ||
| return nil, false | ||
| } | ||
| continue |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
| // 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Fixed in 92729f4 with tests, let me know if this works.
| // 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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
… 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 <noreply@anthropic.com>
…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 <noreply@anthropic.com>
…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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
… 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 <noreply@anthropic.com>
|
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
left a comment
There was a problem hiding this comment.
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
An alias node keeps the name of its anchor as its value, and the schema model read each item of a type list from that value. With x-type: &object string, type: [*object] was read as object instead of string, so the schema hashed differently from type: [string] and rendered as type: [object]. The exclusivity check of a oneOf already followed the alias, so a branch read as string there while the model stored object. Requiring a property of an object branch next to it was not breaking, but became breaking once both documents were rendered and read back. The model now follows the alias as well, as it already does for a single type and for an alias to a whole list. Co-Authored-By: Claude <noreply@anthropic.com>
…e type next to it
The boolean schema true accepts every instance, as {} does. In OpenAPI 3.1 a
reference to {} next to type: string was read as a string branch, but a
reference to true made the branch unknown, because the check gave up on any
target that is not a mapping. Requiring a property of an object branch next
to it was therefore breaking with true and not with {}.
A reference to true now narrows nothing, like {}. The boolean schema false
accepts nothing, which is more likely a mistake than a branch that no instance
matches, so it still makes the branch unknown, as types that have nothing in
common do.
Co-Authored-By: Claude <noreply@anthropic.com>
|
Marking as draft, found a few things I want to look at first. |
…ds next to its $ref
In OpenAPI 3.1 with TransformSiblingRefs, the check read the type next to every
$ref along the chain of references of a branch, and next to the $ref of a
branch that is an alias. The schema model reads neither. When it reaches a
schema through a $ref, it follows the $ref of that schema and drops the type
next to it. The sibling transform skips an alias in a list, so an aliased
branch is read the same way. It does not resolve a $ref whose value is an
alias at all.
With Narrowed: {$ref: StringOrObject, type: string}, the model reads a branch
{$ref: Narrowed}, or an alias of Narrowed, as [string, object], which overlaps
an object branch. The check read it as string, so requiring a property of the
object branch was not breaking. It is breaking on main, where every oneOf
branch is shared.
The type next to a $ref now only narrows the branch itself, written in place.
A branch whose $ref value is an alias cannot be told apart from the others. A
type along the chain that has nothing in common with the referenced schema no
longer makes the branch unknown either, because the model does not read it.
The test for a type next to a reference along the chain now expects a breaking
change. New tests cover a type along the chain that has nothing in common with
the referenced schema, a branch that is an alias, and a branch whose $ref value
is an alias.
Co-Authored-By: Claude <noreply@anthropic.com>
…odel does not read
The walk follows the keywords next to a $ref, as it does on main. The schema
model drops them without TransformSiblingRefs, when it reaches a schema through
a $ref, for an alias in a list, and always next to the $ref of a response,
request body, parameter, header or path item. It also does not resolve a $ref
whose value is an alias, which the walk follows. The walk can therefore give a
schema a direction that the model does not have. On main that did not matter
inside a oneOf, because every branch was shared.
With this PR, an exclusive union could make it matter, for example:
- a oneOf next to the $ref of Holder: {$ref: Other, oneOf: [...]} reached
through a $ref, or in the keywords next to it,
- an exclusive branch that leads to such keywords,
- a union in the content next to the $ref of a response,
- Holder reached in the response direction outside a union before the walk
reaches it in an exclusive branch, so that the branch skips it.
Requiring a property of a schema that only those keywords reach was then not
breaking, while it is breaking on main.
The walk now records whether it reached a schema through what the model does
not read, and whether it kept the direction of a oneOf. When both happen, the
document is walked again with every oneOf shared, which gives the same usage
as on main, whatever the order. Documents without such keywords keep the
direction of their exclusive unions. In OpenAPI 3.0 the model still reads the
keywords next to a $ref where the schema is written, so treating them as
dropped there only shares more unions.
Tests cover a oneOf written next to a $ref, next to a $ref reached through a
$ref, in the keywords next to it, in an alias, and such keywords in an
exclusive branch, with the transform on and off. Others cover a reference to
Holder before and after the union, a union next to the $ref of a response and
of a path item, and a $ref whose value is an alias elsewhere, each with a
document without such keywords that keeps the direction.
Co-Authored-By: Claude <noreply@anthropic.com>
|
Since your last review I found that the check could rely on things the schema model does not read. I fixed that in two commits:
Open question: in OpenAPI 3.0 the check ignores a |
Fixes #726.
Problem
Since #725, a
requiredentry added to an open schema that only responses use is not reported as breaking. That does not apply inside aoneOf: the usage walk marks every branch as used by both requests and responses (schema_usage.go#L266-L267), whileallOfandanyOfkeep the direction of the enclosing schema.Being careful with
oneOfmakes sense in general. AoneOfrejects 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. ThenoneOfis equivalent toanyOf.Change
A
oneOfkeeps the direction of the enclosing schema when no instance can match two of its branches:The type check follows chains of
$refand 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 atypenext to a$refnarrows the branch. That only happens when the version is known to be 3.1 or later andTransformSiblingRefsis on (the default), because withoutTransformSiblingRefsthe schema model does not read the siblings either. It also only happens next to the$refof the branch itself, written in place, because that is the only place the model reads it: when the model reaches a schema through a$refit follows the$refof that schema and drops thetypenext to it, and it does the same for a branch that is a YAML alias. Otherwise only the referenced schema counts. A$reftotruenarrows nothing, like{}.It reads
typeandnullablethe way the schema model does: a single type only from a YAML string, a type list by its items, andnullablefrom any YAML boolean.nullableonly addsnullin OpenAPI 3.0, or when the version is unknown. It only accepts the JSON Schema type names and treats integer and number as overlapping.The schema model itself read an alias in a type list as its anchor name (
type: [*object]becameobject). It now reads the value the alias points to, as it already did for a single type, soSchema.Type, its hash and the rendered output change for such lists.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, a$reftofalse, atypenext to a$refthat has nothing in common with the referenced schema, and a branch whose$refvalue is a YAML alias, which the model does not resolve. 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
requiredentry removed from a schema in an exclusive union that only requests use is not reported as breaking either.Keywords the model drops next to a
$refThe walk follows the keywords next to a
$ref, as it does on main, but the model does not always read them. It drops them withoutTransformSiblingRefs, when it reaches a schema through a$ref, for an alias in a list, and always next to the$refof a response, request body, parameter, header or path item. It also does not resolve a$refwhose value is an alias. The walk can therefore give a schema a direction that the model does not have. On main that did not matter inside aoneOf, because every branch was shared, but an exclusive union could make it matter.The walk now records whether it reached a schema through something the model does not read, and whether it kept the direction of a
oneOf. When both happen, the document is walked again with everyoneOfshared, which gives the same usage as main. Documents without such keywords keep the direction of their exclusive unions. In OpenAPI 3.0 the model still reads the keywords next to a$refwhere the schema is written, so treating them as dropped there only shares more unions.Tests
schema_usage_oneof_test.gocovers:nullablein 3.0 and 3.1, and a narrowertypenext to a$refin 3.1;$reftofalse,$refsiblings the model does not read (3.0,TransformSiblingRefsoff, along a chain, in an alias), and nullable branches in 3.0 in any YAML spelling;$ref, which share every union again;truecases after rendering and reparsing.TestSchema_Build_TypeListWithAliasindatamodel/low/base.TestRequiredConservativeSchemaContexts:oneOfwith a single branch now expects "not breaking".go test ./...passes, and 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 for03-oneof, with the other two cases unchanged.Performance
test_specs/(including Stripe, DocuSign and Asana) are identical to main.BenchmarkPipeline_Compare_Stripeshows no measurable difference (about 280 ms on both).Commits
The second commit fixes the
$refand 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:
nullableas the model does,typeas the model does,typenext to a$refin 3.1,$reftotrue.The last two commits fix places where the check or the walk relied on things the schema model does not read:
typenext to its own$ref, written in place,oneOfshared when the walk follows keywords the model drops.Not covered
Branches of the same type that are told apart by
constorenum, as in many discriminated unions, stay shared. Proving those branches exclusive would need a different analysis.In OpenAPI 3.0 a
typenext to a$refnever narrows a branch. WithTransformSiblingRefson, the model does read it where the schema is written, so the check is more conservative than the model there. OpenAPI 3.0 says those siblings are ignored, so a tool that follows the spec sees{$ref: Untyped, type: string}as accepting anything.A
discriminatoranywhere 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
schemaFieldandwalkReached, and adds fields tousageWalkerand functions in the same place, so whichever of the two is merged second has conflicts inschema_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. #729 reads the keywords next to a$refwheneverTransformSiblingRefsis on, while this PR only narrows by them in 3.1 or later, so the two need to agree on that before both are merged.🤖 Generated with Claude Code