APIDevTools / swagger-cli

Swagger 2.0 and OpenAPI 3.0 command-line tool
https://apitools.dev/swagger-cli
MIT License
515 stars 69 forks source link

Circular reference seems to break validation #62

Open jkoscialkowski opened 3 years ago

jkoscialkowski commented 3 years ago

Hi! It seems I might have been using the OpenAPI spec the wrong way - putting definitions field in schemas which is not supported by the spec. However, I haven't got any errors using swagger-cli validate due to the issue I describe below.

Assume the main spec is:

openapi: "3.0.3"
info:
  version: 0.0.1
  title: API
  description: Some description
paths:
  /route:
    get:
      operationId: route
      requestBody:
        content:
          application/json:
            schema:
              $ref: ./schema.yaml
      responses:
        '200':
          description: Successful
          content:
            application/json:
              schema:
                type: object

Then there are 3 scenarios depending on the contents of schema.yaml.

1. WORKS

type: object
properties:
  prop1:
    type: string

2. FAILS

type: object
properties:
  prop1:
    type: string
definitions:
  somedef:
    type: string

The error message is

Swagger schema validation failed. 
  Data does not match any schemas from 'oneOf' at #/paths//route/get/requestBody
    Data does not match any schemas from 'oneOf' at #/paths//route/get/requestBody/content/application/json/schema
      Additional properties not allowed: definitions at #/content/application/json/schema
      Missing required property: $ref at #/content/application/json/schema
    Missing required property: $ref at #/paths//route/get/requestBody

JSON_OBJECT_VALIDATION_FAILED

3. WORKS

type: object
properties:
  prop1:
    type: string
definitions:
  somedef:
    $ref: '#/definitions/somedef'

While I have found some info that OpenAPI doesn't forbid circular references, this seems to be an error, because the definitions field should not be accepted at all.