swagger-parser icon indicating copy to clipboard operation
swagger-parser copied to clipboard

Invalid references do not throw parser error

Open aishvaryav opened this issue 4 years ago • 7 comments

In the attached open API document, the response schema is referencing a parameter rather than a schema. This is an invalid use case and should throw an error during parsing. I noticed when $refs are invalid, they usually are not validated correctly.

How should the implementation handle this scenario? Should it throw an error? Should it ignore that response or schema?

There needs to be a consistent approach in handling this, and hence I'm asking this question.

openapi: 3.0.1 info: title: Swagger Petstore description: 'This is a sample server Petstore server' version: 1.0.0 servers:

  • url: http://mytestServer/{v1} variables: v2: default: 'd' paths: '/pet/{petId}': get: parameters: - $ref: '#/components/parameters/P1' responses: '200': $ref: '#/components/responses/R1' components: responses: R1: description: The specified resource was not found content: application/json: schema: $ref: '#/components/parameters/P1' parameters: P1: in: path name: petId schema: type: string required: true

aishvaryav avatar Jul 04 '20 09:07 aishvaryav

I think I got the same issue, any news regading this?

rtfpessoa avatar Jul 31 '20 11:07 rtfpessoa

Not sure if my problem belongs in this thread, but I do not get any error at all when my $ref points to an object that does not exist.

ZQ-PSK avatar Aug 25 '20 09:08 ZQ-PSK

@gracekarina any pointer you can give us? I would be ok helping to fix this.

rtfpessoa avatar Sep 17 '20 18:09 rtfpessoa

hi, the error message Is available through SwaggerParseResult.getMessages(). Please let us know if this fixes your issue.

gracekarina avatar Sep 21 '20 21:09 gracekarina

@gracekarina thanks for the answer. I still wonder if there any special reason for the parser to continue instead of failing?

rtfpessoa avatar Sep 22 '20 08:09 rtfpessoa

When the swagger-parser is called from swagger-cli's SwaggerCodegen, the messages are logged but the code generator fails silently. The only way to get the messages is to fork the code and throw from the parser instead of just logging and adding to the messages. The assumption by swagger* teams seems to be that a human is running the code generator interactively and looking at log output (with debug enabled), but we run the code generator programmatically, in every build, and there's nobody looking at output. Instead, we expect to fail the build if something is wrong with the schema, but that doesn't happen. It just fails silently. If swagger-parser had an option to throw an exception that contains the messages instead of logging and returning, it would be super helpful.

jimshowalter avatar Jan 03 '21 19:01 jimshowalter

hi, the error message Is available through SwaggerParseResult.getMessages(). Please let us know if this fixes your issue.

@gracekarina Unfortunately, this issue is still present. The validation for invalid references only works for schema references, but not for the other referencable objects (e.g. responses, parameters, request body, header, ...).

Here is a simple example:

openapi: 3.0.3
info:
  title: Test
  version: 0.0.1
paths:
  /albums:
    get:
      responses:
        200:
          $ref: "#/components/responses/TestFoo"
components:
  responses:
    Test:
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/TestNumberBar"
      description: "Test response"
  schemas:
    TestNumber:
      type: number

When parsing this with OpenAPIV3Parser the SwaggerParseResult.getMessages() only contains one error:

attribute components.responses.Test.content.'application/json'.schema.TestNumberBar is not of type `schema`

So the invalid reference of #/components/schemas/TestNumberBar is correctly reported, but the invalid reference of #/components/responses/TestFoo is not reported at all.

sonallux avatar Oct 31 '22 10:10 sonallux