openapi-spec-validator icon indicating copy to clipboard operation
openapi-spec-validator copied to clipboard

Failures in oneOf, anyOf, etc, are actually in the referenced schema

Open Altreus opened this issue 4 years ago • 2 comments

If a schema is invalid, it will cause a oneOf to fail instead of failing on its own. I discovered this while thinking optional was a valid thing.

The below is valid except I've added optional to Dog. The failure is now On instance['paths']['/pets']['patch']['requestBody'] instead of the expected ['components']['schemas']['Dog'].

paths:
  /pets:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Cat'
                - $ref: '#/components/schemas/Dog'
      responses:
        '200':
          description: Updated
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/Cat'
                  - $ref: '#/components/schemas/Dog'
components:
  schemas:
    Dog:
      type: object
      optional: [ "bark" ]
      properties:
        bark:
          type: boolean
        breed:
          type: string
          enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:
      type: object
      properties:
        hunts:
          type: boolean
        age:
          type: integer
$ docker run -v $PWD/simple.yaml:/openapi.yaml --rm p1c2u/openapi-spec-validator /openapi.yaml
{'content': {'application/json': {'schema': {'oneOf': [{'$ref': '#/components/schemas/Cat', 'x-scope': ['file:///openapi.yaml']}, {'$ref':'#/components/schemas/Dog', 'x-scope': ['file:///openapi.yaml']}]}}}} is not valid under any of the given schemas

Failed validating 'oneOf' in schema['properties']['paths']['patternProperties']['^\\/']['patternProperties']['^(get|put|post|delete|options|head|patch|trace)$']['properties']['requestBody']:                             
    {'oneOf': [{'$ref': '#/definitions/RequestBody'},
               {'$ref': '#/definitions/Reference'}]}

On instance['paths']['/pets']['patch']['requestBody']:
    {'content': {'application/json': {'schema': {'oneOf': [{'$ref': '#/components/schemas/Cat',
                                                            'x-scope': ['file:///openapi.yaml']},
                                                           {'$ref': '#/components/schemas/Dog',
                                                            'x-scope': ['file:///openapi.yaml']}]}}}}

Altreus avatar May 19 '21 13:05 Altreus

A workaround is to replace all refs with a simple object; then it will validate all of the schemata without getting tripped up by a ref.

Altreus avatar May 19 '21 14:05 Altreus

Is this fixed by https://github.com/p1c2u/openapi-spec-validator/pull/129?

It can be tried easily using:

pip install --force-reinstall git+https://github.com/JulienPalard/openapi-spec-validator@show-context

JulienPalard avatar Sep 02 '21 10:09 JulienPalard