OpenAPITools / openapi-diff

Utility for comparing two OpenAPI specifications.
Apache License 2.0
803 stars 153 forks source link

[bug] support of breaking changes for "anyOf" #212

Open TenOs opened 3 years ago

TenOs commented 3 years ago

So I've been tested openapi-diff to see if we can use it in our CI/CD pipeline to prevent any breaking changes from happening. After running a few tests I've noticed that it doesn't seem to support anyOf correctly.

See the two openapi files attached. Basically the endpoint returns a response which either ResponseA or ResponseB. New file removes a required property from ResponseA and openapi-diff doesn't catch the breaking change. old.yaml

openapi: 3.0.2
info:
  title: test API
  version: 0.1.0
paths:
  /test:
    get:
      summary: Test
      operationId: test_test_get
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                title: Response Test Test Get
                anyOf:
                  - $ref: '#/components/schemas/ResponseA'
                  - $ref: '#/components/schemas/ResponseB'
components:
  schemas:
    ResponseA:
      title: ResponseA
      required:
        - attr_a_1
        - attr_a_2
      type: object
      properties:
        attr_a_1:
          title: Attr A 1
          type: string
        attr_a_2:
          title: Attr A 2
          type: string
    ResponseB:
      title: ResponseB
      required:
        - attr_b
      type: object
      properties:
        attr_b:
          title: Attr B
          type: string

new.yaml

openapi: 3.0.2
info:
  title: test API
  version: 0.1.0
paths:
  /test:
    get:
      summary: Test
      operationId: test_test_get
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                title: Response Test Test Get
                anyOf:
                  - $ref: '#/components/schemas/ResponseA'
                  - $ref: '#/components/schemas/ResponseB'
components:
  schemas:
    ResponseA:
      title: ResponseA
      required:
        - attr_a_1
      type: object
      properties:
        attr_a_1:
          title: Attr A 1
          type: string
    ResponseB:
      title: ResponseB
      required:
        - attr_b
      type: object
      properties:
        attr_b:
          title: Attr B
          type: string

Result

No differences. Specifications are equivalents

This is a very simple test scenario, but I've tested the same one with a change like this in:

for all of them it wasn't detecting any breaking changes....

I've tested these using docker openapitools/openapi-diff:latest.

joschi commented 3 years ago

Diff:

--- core/src/test/resources/issue-212-1.yaml    2021-02-28 17:52:14.000000000 +0100
+++ core/src/test/resources/issue-212-2.yaml    2021-02-28 17:52:34.000000000 +0100
@@ -23,15 +23,11 @@
       title: ResponseA
       required:
         - attr_a_1
-        - attr_a_2
       type: object
       properties:
         attr_a_1:
           title: Attr A 1
           type: string
-        attr_a_2:
-          title: Attr A 2
-          type: string
     ResponseB:
       title: ResponseB
       required:
nc-nsre commented 2 years ago

Found that the error is within the SchemaDiff class in the method Schema<?> resolveComposedSchema(Components, Schema<?>) which only considers ((ComposedSchema) schema).getAllOf() but not ((ComposedSchema) schema).getAnyOf()

willosborne commented 1 month ago

Can this be closed now after that that PR was merged?