Skip to content

what-changed: required property added to a response-only schema is reported as breaking when used inside oneOf #726

Open

Description

@frederikprijck

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 when the schema is used inside a oneOf. 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 in how the response refers to Widget:

  • 01-direct-open: the response is $ref: '#/components/schemas/Widget'.

  • 03-oneof: the response is a oneOf of a list of widgets, or an object that wraps a list of widgets:

    schema:
      oneOf:
        - type: array
          items: {$ref: '#/components/schemas/Widget'}
        - type: object
          properties:
            widgets:
              type: array
              items: {$ref: '#/components/schemas/Widget'}

In both, 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 this 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
03-oneof                           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 05 (a discriminator) is reported in #728, case 04 (a closed schema) is a question in #730, and case 06 combines all three.

Cause

usageWalker.schemaField normally keeps the direction of the enclosing schema for allOf and anyOf, but marks every oneOf branch as used by both requests and responses (schema_usage.go#L254-L267):

case v3.AllOfLabel, v3.AnyOfLabel, v3.DependentSchemasLabel:
    w.each(value, schemaObject, sameInstance)
...
case v3.OneOfLabel:
    w.each(value, schemaObject, sharedSchema)

Widget is therefore never "only used by responses", and its required entry keeps the default rule.

Being careful with oneOf makes sense in general. It accepts an instance only when exactly one branch matches. If a response in one branch gains a field, it could start matching a second branch, and oneOf then rejects it. That cannot happen when no instance can match two branches, for example when the branches accept different JSON types, or when there is only one branch. Then oneOf behaves like anyOf.

If there is another reason oneOf is shared that I missed, I am happy to adjust.

Proposed fix

#727 keeps the direction of a oneOf when no instance can match two of its branches:

  • there is a single branch, or
  • the branches accept different types. The check follows a $ref before reading type, because OpenAPI 3.0 ignores the siblings of $ref. It reads type lists and nullable, only accepts the JSON Schema type names, and treats integer and number as overlapping.

Everything else is treated as today: branches with overlapping types or without a type stay shared, and a reference that cannot be resolved still makes the usage unknown. Because the direction is kept, a required entry removed from a request-only union of exclusive branches is not reported as breaking either. The PR adds tests for the relaxed cases and for the ones that must stay breaking. It changes one existing test row: oneOf with a single branch now expects "not breaking" instead of "breaking".

go test ./what-changed/... passes locally.

Not covered: branches of the same type that are told apart by const or enum, as in many discriminated unions, stay shared. Proving those branches exclusive would need a different analysis, so I left them out.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions