Skip to content

fix(what-changed): follow discriminator mappings instead of dropping all usage - #729

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

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

Conversation

@frederikprijck

@frederikprijck frederikprijck commented Nov 23, 2026 •

Copy link
Copy Markdown

Fixes #728.

Problem

Since #725, a required entry 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 a discriminator: when the usage walk meets one, it marks the whole analysis as unknown (schema_usage.go#L272-L275) and buildSchemaUsage returns 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 $ref points at it, for example a schema that extends the discriminated one with allOf. 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:

  • It follows the schemas named by mapping and defaultMapping (a reference or a schema name), in the direction of the schema that holds the discriminator.
  • Next to a oneOf or an anyOf, all possible schemas must be listed, so the discriminator can only select one of them. The walk follows those already.
  • Otherwise the discriminator is on a parent schema, and can select by name any component schema that builds on the parent with allOf: directly, through other schemas, or in a nested allOf. A $ref counts as building on its target as well, since the schema model turns a $ref with siblings into allOf (TransformSiblingRefs), and an alias like Alias: {$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.
  • A component schema that no union lists and that does not build on a parent with a discriminator is not selected. When nothing else uses it, it keeps the configured rule.
  • As a result, a required entry 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.
  • The usage is still unknown when:
    • a mapping value is not a schema of the document: a URL, a file, a name that matches no schema, a value that is neither a local reference (#/...) nor a component name (such as Dog/properties/kind), or a pointer to a value that cannot be a schema (such as #/info/title). A boolean schema is a valid target.
    • a $ref cannot be followed while looking for the children of a parent, because the schema behind it could build on the parent.
    • the walk meets $dynamicRef, $recursiveRef or $id, as before.
  • Once the usage is unknown, the walk stops resolving mapping targets and looking for children, since the result is dropped.

Tests

  • New schema_usage_discriminator_test.go:
    • Now not breaking (breaking on main, where any discriminator the walk meets drops the usage):
      • an unrelated response-only discriminator, with and without a mapping;
      • a request-side discriminator next to a oneOf or an anyOf that does not list the schema, or on a parent the schema does not build on;
      • a schema that builds on a request-side parent next to a oneOf or an anyOf;
      • a child that nothing refers to of a parent that only a response uses;
      • a schema reachable only through mapping or defaultMapping, by reference or by name;
      • a mapping or default mapping to a boolean schema.
    • Still breaking (same as main):
      • a schema a request also uses;
      • an unused schema that the union does not list;
      • a schema that builds on a request-side parent, directly, through another child, through a reference or in a nested allOf;
      • a request-side discriminator whose mapping points at an inline response schema;
      • a schema used by a response inside a child of a request-side parent that nothing refers to;
      • a discriminator under not or if, in a callback, in a webhook, next to unevaluatedProperties: false, or found while walking a child of another parent;
      • a required entry removed from a request-only schema that builds on a response-side parent;
      • mapping values or a defaultMapping that are a URL, a file, a relative path, a missing schema or a string.
    • Usage unknown: a child lookup that meets a reference to another file or to a missing schema.
    • Nothing is resolved after the usage becomes unknown, from a mapping value or while looking for children.
  • One existing row in TestRequiredConservativeSchemaContexts changes: a response-only allOf wrapper with discriminator: {propertyName: kind} now expects "not breaking".
  • Two of the cases that are now not breaking go further than undoing the regression 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. On main, a schema that nothing refers to keeps the default rule even in a document without a discriminator. Now a schema reached only through mapping or defaultMapping of 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.
  • The 05-discriminator-elsewhere case 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 of TestRequiredDiscriminator, which reports 0 breaking.

Review follow-ups

  • d8120eb: a discriminator only selects the listed schemas of a union, or the allOf children of its parent, instead of every component schema.
  • afe5806: a mapping or default mapping to a value that cannot be a schema makes the usage unknown.
  • c230230: the walk stops resolving discriminator targets once the usage is unknown.

Not covered

  • A discriminator on a parent that a request uses, or that the walk already treats as shared (under not or if, in a oneOf whose branches can overlap, in a callback or a webhook), gives all of the parent's allOf children the request direction, so a required entry added to such a child stays breaking even if only responses use it otherwise. That includes the common pattern of a response oneOf: [Cat, Dog] where Cat and Dog extend Pet, which holds the discriminator, with allOf: Cat, Dog and any other child of Pet stay breaking. Schemas unrelated to Pet are not affected.
  • Once a parent discriminator is reached, every component schema is checked for children. A $ref that cannot be followed in any of them, at the top level or in an allOf, makes the usage unknown for the whole document, even if that schema is unrelated to the discriminator.
  • $dynamicRef, $recursiveRef and $id still 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 schemaField and 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

@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 (c230230).
⚠️ Report is 1 commits behind head on main.

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     
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 and others added 2 commits November 23, 2026 15:18
…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]>
@frederikprijck
frederikprijck force-pushed the fix/what-changed-discriminator-response-usage branch from c3fdd2d to 92482b8 Compare November 23, 2026 14:18

@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 #728. I verified that the original example now reports zero breaking changes.

Comment thread what-changed/model/schema_usage.go Outdated
for walked := schemaDirection(0); w.implicit != walked; {
walked = w.implicit
for _, schema := range usageFields(schemas) {
w.walk(schema, schemaObject, walked)

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 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

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 d8120eb, following the discriminator rules you shared.

Additionally:

  • A $ref along the way counts as building on the target as well. The schema model turns a $ref with siblings into allOf (TransformSiblingRefs), and an alias like Alias: {$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.

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 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

Comment thread what-changed/model/schema_usage.go Outdated
ref = "#/components/schemas/" + ref
}
schema := w.localTarget(ref)
if schema == nil {

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.

#/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.

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 afe5806.

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.

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)

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.

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?

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.

I address this in in c230230

frederikprijck and others added 3 commits November 26, 2026 10:51
… 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 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 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:

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.

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)

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants