Repository navigation
fix(what-changed): follow discriminator mappings instead of dropping all usage - #729
frederikprijck wants to merge 11 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 40363 +83
=========================================
+ Hits 40280 40363 +83
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 <noreply@anthropic.com>
…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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
…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 <noreply@anthropic.com>
…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 <noreply@anthropic.com>
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.
… the model does
Looking for the children of a parent discriminator, the walker followed both a
$ref and the allOf next to it. Without TransformSiblingRefs the schema model
reads a schema with a $ref as the referenced schema alone, so with
Alias: {$ref: Widget, allOf: [{$ref: Pet}]} the model does not make Alias build
on Pet. The walk still did, so a discriminator on a response-only Pet gave
Widget the response direction, and requiring a property of Widget was not
breaking.
An allOf next to a $ref now only counts with TransformSiblingRefs, as in the
model. The transform does not depend on the OpenAPI version.
A test covers that fixture with the transform on, where it stays not breaking,
and off, where it is now breaking.
Co-Authored-By: Claude <noreply@anthropic.com>
…r children To find the children of a parent discriminator, the walker stored every component schema under each schema it builds on, directly or through others. A chain of n schemas that each build on the one before therefore stored about n² entries: 65,536 for 256 schemas. It now stores each direct link once, from a schema to the schemas that build on it with allOf or a $ref, and follows those links from the parent when a discriminator needs its children. A set of the schemas already reached keeps cycles safe. Only the component schemas reached this way are collected and sorted by where they are declared, so a discriminator costs what its children cost, not a pass over every component schema. The children found and their order are the same as before, except that a schema listed twice under components is returned once. A test builds a chain of 256 schemas, checks that all of them are children of the first one in the order they are declared, and that 510 links are stored. Co-Authored-By: Claude <noreply@anthropic.com>
|
Besides the fixes for your four comments (a34cd89, a1d23c3, 013e418 and 62e6cb7), I found that the walk could rely on things the schema model does not read. On main that was hidden, because any discriminator made the usage unknown. I fixed that in two commits:
|
… only in place as unknown
With TransformSiblingRefs, the schema model reads an allOf next to a $ref where
the schema is written, but not when it reaches the schema through a $ref: it
then follows the $ref of that schema and drops the rest. The sibling transform
also skips an alias in a list, so an aliased schema is read the same way. While
looking for the children of a parent discriminator, the walk followed that
allOf in both cases. With Child: {allOf: [{$ref: Mid}, {$ref: Widget}]} and
Mid: {$ref: Other, allOf: [{$ref: Pet}]}, the model does not make Child build
on Pet, but the walk did, so a discriminator on a response-only Pet gave Widget
the response direction, and requiring a property of Widget was not breaking.
Before this PR, a discriminator anywhere made the usage unknown, so it was
breaking.
With the transform, a reference to a schema with an allOf next to its own $ref,
or an alias of one, now makes the usage unknown, because whether the model
reads that allOf depends on how the schema is reached. Without the transform,
the model never reads it, which the walk already followed.
A test covers an allOf next to a $ref reached through a $ref from a child and
in an alias, with the transform on and off, and checks whether the usage is
known.
Co-Authored-By: Claude <noreply@anthropic.com>
…he walk follows what the model 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 in
a document with a discriminator, because the discriminator made the usage
unknown.
With this PR, following a discriminator could make it matter, for example:
- a discriminator next to the $ref of Holder: {$ref: Other, discriminator: ...}
reached through a $ref, or in the keywords next to it,
- keywords the model drops in a schema that a discriminator selects,
- Holder: {$ref: Other, properties: {w: {$ref: Widget}}} in one response and a
discriminator in another,
- content next to the $ref of a response in a document with a discriminator.
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, including a $ref whose value is an alias while looking for the
children of a parent discriminator, and whether it followed a discriminator.
When both happen, the usage is unknown, as on main, whatever the order.
Documents without such keywords keep the usage of their discriminators.
The schemas a discriminator selects are walked as written, as the model reads a
component schema, which matches the children found for an Alias with an allOf
next to its $ref.
Tests cover a discriminator next to a $ref written in place, reached through a
$ref, in the keywords next to it and in an alias, a parent discriminator next to
a $ref, and keywords next to the $ref of a mapping target and of a child, each
with the transform on and off and whether the usage is known. Others cover a
discriminator elsewhere in the document, content next to the $ref of a response
and of a path item, and a $ref whose value is an alias, each with a document
without such keywords that keeps the usage.
Co-Authored-By: Claude <noreply@anthropic.com>
…enAPI 3.1 A mapping or default mapping value that resolves to a boolean was accepted as a boolean schema in every version. Before OpenAPI 3.1 a schema cannot be a boolean, so in a 3.0 document a mapping to '#/paths/~1pets/get/deprecated' kept the usage, and a required entry added to a schema that only a response uses was not breaking. On main, the discriminator makes the usage unknown, so it is breaking. A boolean target is now only a schema when ResolveDocumentVersion confirms OpenAPI 3.1 or later. Otherwise it makes the usage unknown, like any other target that is not a schema. Tests cover a mapping in 3.0 that keeps the usage, and a mapping in 3.0 to a boolean component and to a boolean field, next to the 3.1 and 3.2 cases. Co-Authored-By: Claude <noreply@anthropic.com>
…ends elsewhere
A discriminator on a parent can select a component schema that builds on the
parent with allOf by its name. When the mapping has that name as a key, the
value selects the schema it maps to instead. The walk still gave the child
the direction of the discriminator. With Cat and Widget building on a
response-only Pet and mapping: {Widget: Cat}, nothing can select Widget, yet
a required entry added to it was not breaking. On main, the discriminator
makes the usage unknown, so it is breaking.
A child is now only selected by name when one of its component names is not a
key of the mapping. A mapping value that names the child, such as legacy:
Widget, still selects it. The parent is walked as before.
Tests cover a name sent to another schema, with and without another value
naming the child, a name sent to the child itself, another name sent to
another schema, a child listed under two names, and a mapping that sends the
name of the parent elsewhere.
Co-Authored-By: Claude <noreply@anthropic.com>
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, and gives the schemas it can select the direction of the schema that holds it:
mappinganddefaultMapping, by reference or by name;oneOfor ananyOf, only the listed schemas, which the walk follows already;allOf, directly or through other schemas, as the model reads them. A name that the mapping sends to another schema does not select its schema.Other schemas are not selected, and keep the configured rule when nothing uses them. A
requiredentry added to a schema that only a response uses, or that only a response-side discriminator can select, is no longer breaking.The usage stays unknown, as on main, when a mapping target is not a schema of the document (a boolean only counts from OpenAPI 3.1), when a
$refmet while looking for children cannot be followed or is read differently depending on where the model reaches it, when the walk also follows keywords that the model drops next to a$ref, and for$dynamicRef,$recursiveRefand$id.Tests
schema_usage_discriminator_test.go, covering the cases that are now not breaking, the cases that stay breaking as on main, and the cases where the usage stays unknown, withTransformSiblingRefson and off where it matters.TestRequiredConservativeSchemaContextsnow expects "not breaking": a response-onlyallOfwrapper with a discriminator.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 reports 0 breaking, andgo test ./what-changed/...passes.Not covered
notorif, in aoneOfwhose branches can overlap, in a callback or a webhook), gives 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. So does a reference to a schema with anallOfnext to its own$ref, withTransformSiblingRefs.$ref, the usage of the whole document is unknown, not only the usage of the schemas those keywords reach.$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