Repository navigation
fix(what-changed): keep the direction of schemas in exclusive unions - #727
frederikprijck wants to merge 7 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 40339 +59
=========================================
+ Hits 40280 40339 +59
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 <[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]>
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 <[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]>
|
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
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. Otherwise only the referenced schema counts.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.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 atypenext to a$refthat 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
requiredentry removed from a schema in an exclusive union that only requests use is not reported as breaking either.Tests
schema_usage_oneof_test.go:'true'), a nullable object ornullin OpenAPI 3.1, a$refnext to a narrowertypein OpenAPI 3.1, a reference without atypenext to atype, atypenext to a reference along the chain, and arequiredentry removed from a request-only union.type, an unquotedtype: null, a boolean branch, a type name that is not a JSON type, a$refnext to atypeit has nothing in common with, a$refnarrowed to the type of another branch, a$refnext to a type that is not a JSON type, a reference without atypenext to atypein OpenAPI 3.0, a narrowertypenext to a$refwithTransformSiblingRefsoff, nullable branches in OpenAPI 3.0 (also spelledTrueandTRUE), an emptytypelist withnullable, a union that a request also uses, arequiredentry removed in a response, and arequiredentry removed from a request union of two objects.oneOfthat is not a list, atypethat is not a name or a list, an emptytypelist (also withnullable), reference cycles of one and two schemas, a$refnext to atypewhen the version is unknown, and references that cannot be resolved.TestRequiredConservativeSchemaContextschanges:oneOfwith a single branch now expects "not breaking" (renamed toexclusive union of one branch).go test ./what-changed/... ./tests/... .passes.03-oneofwith this branch, and01-direct-openand02-array-openare unchanged.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: skipping the check for a shared direction, readingnullableas the model does, readingtypeas the model does, stopping at a repeated reference instead of after eight, and narrowing by thetypenext to a$refin 3.1.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.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
schemaField, 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.🤖 Generated with Claude Code