Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Altreus opened this issue May 19, 2021 · 2 comments
Open

Comments

@Altreus
Copy link

Altreus commented May 19, 2021

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
Copy link
Author

Altreus commented May 19, 2021

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.

giang-nghg added a commit to Mykrobe-tools/mykrobe-atlas-tracking-api that referenced this issue Aug 20, 2021
Zhicheng-Liu pushed a commit to Mykrobe-tools/mykrobe-atlas-tracking-api that referenced this issue Aug 21, 2021
* Update OpenAPI generator

* Delete sample endpoint

* Fixed requirement to avoid currently open issue: python-openapi/openapi-spec-validator#126

* Change return status

* Update specs
@JulienPalard
Copy link
Contributor

JulienPalard commented Sep 2, 2021

Is this fixed by #129?

It can be tried easily using:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants