Repository navigation
Description
Summary
Since v0.44.0 (#719, #725), adding a property to an open schema that only responses use, and listing it in required, is not reported as breaking. The server now sends one more field, which does not break a client that tolerates additional properties.
That stops working for every schema in a document as soon as a schema used by an operation or a webhook has a discriminator, even when the discriminator has nothing to do with the schema that changed. The same change is reported as breaking again. Both cases below should report 0 breaking changes.
Reproduce
frederikprijck/libopenapi-whatchanged-breaking-changes-reproduction has one directory per case. go run . compares each case's base.yaml with head.yaml using libopenapi v0.44.0. Two cases differ only by an extra, unrelated route:
-
01-direct-open:GET /widgetsreturns$ref: '#/components/schemas/Widget'. -
05-discriminator-elsewhere: the same, plus a second route whose response schema has a discriminator (excerpt of the case):paths: /pets: get: responses: "200": description: ok content: application/json: schema: {$ref: '#/components/schemas/Pet'} components: schemas: Pet: oneOf: - $ref: '#/components/schemas/Cat' - $ref: '#/components/schemas/Dog' discriminator: propertyName: kind mapping: cat: '#/components/schemas/Cat' dog: '#/components/schemas/Dog'
CatandDogare plain objects./petsand these schemas are in bothbase.yamlandhead.yaml, and nothing links them toWidget.
In both cases, the only change between base.yaml and head.yaml is that Widget gains an extra property and extra is added to required. Widget is open (no additionalProperties: false) and only the /widgets response uses it.
go run . prints one line per case. These are the lines for the two cases above:
case changes breaking
01-direct-open 2 0
05-discriminator-elsewhere 2 1 <- expected 0
changes is the total number of changes, and breaking how many of them are reported as breaking. The two changes are the new property and the new required entry. The required entry is the one reported as breaking.
Case 03 (oneOf) is reported in #726, case 04 (a closed schema) is a question in #730, and case 06 combines all three.
Cause
When usageWalker.schemaField meets a discriminator, it marks the whole usage analysis as unknown (schema_usage.go#L272-L275):
case v3.DynamicRefLabel, "$recursiveRef", "$id", v3.DiscriminatorLabel:
// Resource scopes and implicit discriminator targets need more than local pointer reachability.
w.unknown = truebuildSchemaUsage then returns no usage information at all (#L135-L137). Without it, no schema is known to be used only by responses, so every required entry keeps the default rule, including the one on Widget.
The concern in the comment is real. A discriminator can select a component schema by its name (an implicit mapping), even when no $ref points at it, for example a schema that extends the discriminated one with allOf. So a schema that looks response-only might also be selected from a request. But the effect is much wider than that concern: one discriminator turns off the analysis for the whole document.
If there is another reason the discriminator has to disable everything, I am happy to adjust.
Proposed fix
#729 keeps the analysis and handles the discriminator itself:
- It follows the schemas named by the discriminator's
mappinganddefaultMapping(a reference or a schema name), in the direction of the schema that holds the discriminator. - Because a discriminator can select any component schema by name, every component schema gets the discriminator's direction, including one that nothing refers to, and everything below it. Walking a schema can reach another discriminator and add a direction, so this repeats until the directions stop changing. If a request-side discriminator can select
Widget, which a response also uses,Widgetbecomes shared and itsrequiredentry stays breaking. - A component schema that nothing refers to is then used in the direction of the discriminators, because one of them can select it by name.
- A mapping value that is not a schema of the document, such as a file, a URL, a relative path or a name that matches no schema, still makes the usage unknown. So do
$dynamicRef,$recursiveRefand$id, as before.
The PR adds tests for the relaxed cases and for the ones that must stay breaking. It changes one existing test row: a response-only allOf wrapper with discriminator: {propertyName: kind} now expects "not breaking" instead of "breaking".
go test ./what-changed/... passes locally.
Not covered: a discriminator on a schema 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 every component schema the request direction, so a required entry added to a component schema that responses use stays breaking in that document. That includes the common pattern of a response oneOf: [Cat, Dog] where Cat and Dog extend Pet, which holds the discriminator, with allOf. $dynamicRef, $recursiveRef and $id also still disable the analysis. Both need a different argument, so I left them out.
Activity
[-]what-changed: required property added to a response-only schema is reported as breaking when a schema used by an operation has a discriminator[/-][+]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[/+]