Repository navigation
fix(what-changed): follow discriminator mappings instead of dropping all usage - #729
frederikprijck wants to merge 5 commits into
Conversation
6502a43 to
1ee17b0
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #729 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 316 316
Lines 40280 40324 +44
=========================================
+ Hits 40280 40324 +44
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:
|
…all usage Any discriminator reachable from a path made the usage walk give up, so every schema in the document fell back to the conservative required-entry rule, even schemas unrelated to the discriminator. The walk now follows the schemas a discriminator mapping or default mapping names, in the direction of the schema that holds it. A discriminator can also select any component schema by name, so each component schema that is already in use gains that direction as well. Schemas that nothing refers to stay unused. A mapping value that is not a schema of the document, such as a file or a URL, still makes the usage unknown. Co-Authored-By: Claude <[email protected]>
…ction A discriminator can select any component schema by name, including one that nothing refers to. The second pass only walked component schemas that were already in use, so a schema below an unused component kept the direction of the place that referred to it. With a request-side discriminator, a schema used by a response inside such a component could be relaxed although a request can send it. The second pass now walks every component schema with the direction of the discriminators. A walk can reach another discriminator and add a direction, so it repeats until the directions stop changing. A schema that only a discriminator can select is now used in that direction instead of not at all. A mapping value that is neither a reference nor a component name is a relative URI of another document, so it now makes the usage unknown instead of being resolved as a pointer inside the components. Tests cover a schema below an unused component, a mapping to an inline schema, discriminators under not and if, in a callback, in a webhook and next to unevaluatedProperties, a second discriminator found in an unused component, the removal of a required entry from a request, and a mapping to a relative path. Co-Authored-By: Claude <[email protected]>
c3fdd2d to
92482b8
Compare
daveshanley
left a comment
There was a problem hiding this comment.
Thanks for fixing #728. I verified that the original example now reports zero breaking changes.
| for walked := schemaDirection(0); w.implicit != walked; { | ||
| walked = w.implicit | ||
| for _, schema := range usageFields(schemas) { | ||
| w.walk(schema, schemaObject, walked) |
There was a problem hiding this comment.
With a Pet response whose oneOf lists only Cat and Dog, this loop also marks an unused Widget as response-only. Adding a required field to Widget then reports zero breaking changes. Could we limit implicit targets to the listed oneOf or anyOf alternatives and, for a parent discriminator, its allOf descendants? That keeps the configured rule for Widget. OpenAPI discriminator rules
There was a problem hiding this comment.
Fixed in d8120eb, following the discriminator rules you shared.
Additionally:
- A
$refalong the way counts as building on the target as well. The schema model turns a$refwith siblings intoallOf(TransformSiblingRefs), and an alias likeAlias: {$ref: Pet}should not hide a child behind it. - While looking for children, a reference that can't be followed (to another file, or to a schema that doesn't exist) might lead back to the parent, which would make that schema a child we can't see. Rather than guess, the usage becomes unknown, as it already does for unresolved references elsewhere.
A discriminator with none of oneOf, anyOf or allOf around it is undefined in the spec. I treat it as a parent with no children, so it selects nothing by name.
The tests I added earlier relied on any schema being selectable by name. They now let Widget build on the parent with allOf, and new tests cover the cases above.
There was a problem hiding this comment.
The Cat/Dog union case now passes. One parent-discriminator case still hides a required-field change. Cat and Widget both extend a response-only Pet. With mapping: {Widget: Cat}, the value Widget selects Cat, and nothing else selects Widget. Requiring Widget.id still reports 0 breaking changes. Please skip a child’s implicit name when an explicit mapping overrides it. This case should report 1. Adding legacy: Widget gives it a real response use and should report 0. OpenAPI mapping rules
| ref = "#/components/schemas/" + ref | ||
| } | ||
| schema := w.localTarget(ref) | ||
| if schema == nil { |
There was a problem hiding this comment.
#/info/title resolves to a string, passes this check, and gets skipped by walk. Could we set w.unknown when the target cannot represent a schema? A test for this pointer in mapping and in OpenAPI 3.2 defaultMapping would cover both paths. Boolean schemas should still work in 3.1 and later.
There was a problem hiding this comment.
Confirmed that #/info/title now falls back for both mapping and defaultMapping. On OpenAPI 3.0.4, a mapping to #/paths/~1pets/get/deprecated still accepts false as a schema. The required-field change then reports 0 breaking changes instead of the configured 1. Please accept boolean schema targets only when ResolveDocumentVersion() confirms 3.1 or later. A 3.0 regression case alongside the passing 3.1 boolean case would cover this. OpenAPI 3.0 schema
| switch key { | ||
| case base.MappingLabel: | ||
| for _, target := range usageFields(value) { | ||
| w.discriminatorTarget(target, dir) |
There was a problem hiding this comment.
An external mapping sets w.unknown, so the result will be discarded. With that mapping first, I saw the walker still resolve and cache all 512 local targets that followed. Could we return as soon as a target sets w.unknown and skip walkImplicitTargets when that flag is set?
… by name Every component schema got the direction of each discriminator, because one can select a schema by its name. The OpenAPI specification is narrower: next to a oneOf or an anyOf, all possible schemas must be listed, and only a discriminator on a parent schema can select a schema that builds on it with allOf. So a schema that nothing uses, and that a oneOf does not list, was treated as used by the response, and a required entry added to it was no longer breaking. A discriminator next to a oneOf or an anyOf now only selects the listed schemas, which the walk follows already. A discriminator on a parent selects the component schemas that build on the parent with allOf, directly, through other schemas, through a reference or in a nested allOf. A reference that cannot be followed while looking for them could lead to the parent, so it makes the usage unknown. The tests that relied on any schema being selectable by name now let Widget build on the parent. New tests cover a schema the union does not list, a union next to a parent that has a child, the ways a child can build on a parent, a schema that builds on itself, and references that cannot be followed. Co-Authored-By: Claude <[email protected]>
…ot a schema as unknown A mapping or default mapping value such as '#/info/title' resolves to a string. The walk skips anything that is not a mapping, so the discriminator target was silently dropped while the rest of the usage was kept. A target that is neither a mapping nor a boolean schema now makes the usage unknown. A boolean schema has nothing to walk, so it stays a valid target. Tests cover a string target in mapping and, in OpenAPI 3.2, in defaultMapping, and a boolean schema in both. Co-Authored-By: Claude <[email protected]>
…ge is unknown A mapping value outside the document makes the usage unknown, so the result is dropped. The walker still resolved and cached every mapping value after it and the default mapping, built the index of children for a parent discriminator, and kept following the references of other component schemas after one of them could not be followed. Each of these now stops as soon as the usage is unknown. A test checks that no target is resolved after the usage becomes unknown, both from a mapping value and while looking for the children of a parent. Co-Authored-By: Claude <[email protected]>
daveshanley
left a comment
There was a problem hiding this comment.
Thanks for fixing the #728 example and the fallback cost in c230230e. I verified that the example now reports zero breaking changes. With an external mapping first, the walker resolved zero of the 512 local targets that followed.
I’m requesting changes before merge. An explicit mapping can still hide a required-field change. Please also honor the sibling-reference setting, check the OpenAPI version for boolean targets, and keep the child index proportional to the direct inheritance links. Mapping override · Boolean target in 3.0
| return | ||
| } | ||
| w.ancestors(target, seen) | ||
| case v3.AllOfLabel: |
There was a problem hiding this comment.
I used an Alias that references Widget and also lists Pet under allOf. With TransformSiblingRefs=false, the model reads Alias as Widget alone. This walk still makes it a child of the response-only Pet, so requiring Widget.id reports 0 breaking changes. Please follow the reference alone when the model ignores its siblings. This fixture should report 1 with the transform disabled and 0 with it enabled.
| ancestors := make(map[*yaml.Node]bool) | ||
| w.ancestors(schema, ancestors) | ||
| for ancestor := range ancestors { | ||
| w.children[ancestor] = append(w.children[ancestor], schema) |
There was a problem hiding this comment.
A chain of 128 schemas stored 16,384 entries in w.children. At 256 schemas, 255 direct allOf links produced 65,536 entries. Could we store each direct parent–child link once and walk descendants from the discriminator that needs them? A visited set keeps cycles safe and avoids storing every ancestor–descendant pair.
Fixes #728.
Problem
Since #725, a
requiredentry added to an open schema that only responses use is not reported as breaking. That does not apply anywhere in a document where an operation or a webhook reaches adiscriminator: when the usage walk meets one, it marks the whole analysis as unknown (schema_usage.go#L272-L275) andbuildSchemaUsagereturns no usage (#L135-L137). Every schema then falls back to the default rule, including schemas that have nothing to do with the discriminator.The concern behind it is real: a discriminator can select a component schema by its name, even when no
$refpoints at it, for example a schema that extends the discriminated one withallOf. But it does not require giving up on the whole document.Change
The walk now handles the discriminator itself, following the conditions for using the discriminator:
mappinganddefaultMapping(a reference or a schema name), in the direction of the schema that holds the discriminator.oneOfor ananyOf, all possible schemas must be listed, so the discriminator can only select one of them. The walk follows those already.allOf: directly, through other schemas, or in a nestedallOf. A$refcounts as building on its target as well, since the schema model turns a$refwith siblings intoallOf(TransformSiblingRefs), and an alias likeAlias: {$ref: Pet}should not hide a child behind it. These children get the direction of the discriminator, including a child that nothing refers to, and everything below it.requiredentry added to a schema that only responses use, or that only a response-side discriminator can select, is no longer breaking. A schema used by a response that a request-side discriminator can select becomes shared, and stays breaking.#/...) nor a component name (such asDog/properties/kind), or a pointer to a value that cannot be a schema (such as#/info/title). A boolean schema is a valid target.$refcannot be followed while looking for the children of a parent, because the schema behind it could build on the parent.$dynamicRef,$recursiveRefor$id, as before.Tests
schema_usage_discriminator_test.go:oneOfor ananyOfthat does not list the schema, or on a parent the schema does not build on;oneOfor ananyOf;mappingordefaultMapping, by reference or by name;allOf;notorif, in a callback, in a webhook, next tounevaluatedProperties: false, or found while walking a child of another parent;requiredentry removed from a request-only schema that builds on a response-side parent;defaultMappingthat are a URL, a file, a relative path, a missing schema or a string.TestRequiredConservativeSchemaContextschanges: a response-onlyallOfwrapper withdiscriminator: {propertyName: kind}now expects "not breaking".mappingordefaultMappingof a response-side discriminator, or a child that nothing refers to of a parent that only a response uses, is treated as used by that response.go test ./what-changed/...passes.05-discriminator-elsewherecase from what-changed: required property added to a response-only schema is reported as breaking when a schema used by an operation or a webhook has a discriminator #728 is the "unrelated response only discriminator" row ofTestRequiredDiscriminator, which reports 0 breaking.Review follow-ups
allOfchildren of its parent, instead of every component schema.Not covered
notorif, in aoneOfwhose branches can overlap, in a callback or a webhook), gives all of the parent'sallOfchildren the request direction, so arequiredentry added to such a child stays breaking even if only responses use it otherwise. That includes the common pattern of a responseoneOf: [Cat, Dog]whereCatandDogextendPet, which holds the discriminator, withallOf:Cat,Dogand any other child ofPetstay breaking. Schemas unrelated toPetare not affected.$refthat cannot be followed in any of them, at the top level or in anallOf, makes the usage unknown for the whole document, even if that schema is unrelated to the discriminator.$dynamicRef,$recursiveRefand$idstill disable the analysis for the whole document.These need a different argument, so I left them as they are.
Note
#727 changes the same part of
schemaFieldand adds functions in the same place, so whichever of the two is merged second has a small conflict. The resolution keeps both changes. When the first one is merged, I will resolve it in the second one with a merge commit.🤖 Generated with Claude Code