Endava / cats

CATS is a REST API Fuzzer and negative testing tool for OpenAPI endpoints. CATS automatically generates, runs and reports tests with minimum configuration and no coding effort. Tests are self-healing and do not require maintenance.
Apache License 2.0
1.17k stars 74 forks source link

better parsing errors #17

Closed rahulahoop closed 2 years ago

rahulahoop commented 2 years ago

When trying to run cats on our project specification, i get a plain old

[**********][*****] ‼ fatal Error parsing OPEN API contract <filename>

anyway could get a reasoning with whats wrong in the error message ?

we are using relative file paths to reference and reuse schema definitions.

ive added cats.jar to my path so i can run it directly on the project level

en-milie commented 2 years ago

@rahulahoop this is fixed in the last commit and will be released this week.

rahulahoop commented 2 years ago

Ok I've built the jar from source and now Im getting the error stack trace.

i softlinked the jar so i can run it anywhere but the first stack trace i got was file not found becuase im giving the file relative to the dir im in, so like in the example

dir

$dir cats.jar --contract=spec

if i provide full PWD then he finds the file.

Then he cant interpret references to other yaml files and throws a npe:

[**********][*****]                            Exception while resolving:
java.lang.RuntimeException: Unable to load RELATIVE ref: ./schemas/shared.yaml path: .
    at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:239)

is this not supported?

@en-milie

en-milie commented 2 years ago

@rahulahoop Is it possible to post an example? It seems that the openapi parser has issues with this. I didn't test this scenario yet, so it will be helpful to see what's missing. It might be some additional parser configuration that's needed.

rahulahoop commented 2 years ago

example.zip

attached is a sample of referencing the path

produces the same error

@en-milie

rahulahoop commented 2 years ago

https://github.com/swagger-api/swagger-parser/blob/2de0106e4502d6db72848f5a8daad13a7e572b98/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ResolverCache.java#L126

its correct in reading from an external file, perhaps its the path

am i incorrect in providing the full qualified path for the contract?

en-milie commented 2 years ago

I'll run the example and get back

en-milie commented 2 years ago

@rahulahoop you should put $ref: './shared/extra.yaml/#/components/schemas/NotAccessible'. Please notice the ./ at the start.

rahulahoop commented 2 years ago

@en-milie i still get runtime exception with the same error

en-milie commented 2 years ago

I run CATS as: ./cats.jar --contract=contract.yaml --server=http://localhost:8200

The contract.yaml looks as follows:

openapi: "3.0.2"
info:
  title: API Title
  version: "1.0"
servers:
  - url: https://api.server.test/v1
paths:
  /test:
    get:
      responses:
        '200':
          description: OK

components:
  schemas:
    InFileObject:
      type: object
      properties:
        Acessable:
          type: string
    OutFileObject:
      $ref: './shared/extra.yaml/#/components/schemas/NotAccessible'

And I get a full run: ☑ complete CATS finished in 2151 ms. Total (excluding skipped) requests 66. ✔ Passed 3, ⚠ warnings: 22, ‼ errors: 41,

@rahulahoop am I missing something?

en-milie commented 2 years ago

@rahulahoop can I close this? or still having issues?

rahulahoop commented 2 years ago

I am still having an issue but Im not sure how I can recreate it.

I moved the jar to folder of my specification and he starts to fuzz with happyfuzzer but gets a null pointer on endpoint that looks like this

[**********][*****] ▶ start    Start fuzzing path /statistics/bk/{subtype}
[**********][*****] ✖ error    Something went wrong while running CATS!
java.lang.NullPointerException: null
        at com.endava.cats.generator.simple.PayloadGenerator.parseFromInnerSchema(PayloadGenerator.java:338)
  /statistics/bk/{subtype}:
    get:
      summary: Return statistics for BK case
      description: Returns the yearly statistics for BK cases. If no search parameter is given the
        current year will be used.
      operationId: get-statistics-bk
      parameters:
        - in: path
          name: subtype
          required: true
          schema:
            $ref: 'schemas/statistics-reports.yaml#/components/schemas/StatisticBKSubtype'
        - name: year
          in: query
          required: false
          schema:
            type: integer
      tags:
        - statistics
        - BK
      responses:
        200:
          $ref: '#/components/responses/StatisticDataResponse'
        404:
          description: No statistics for chosen year found

i tried to make a smaller replica case for this but its not getting npe so im a little bit confused

en-milie commented 2 years ago

@rahulahoop will you be able to post the schemas as well? both of them? (anonymised, of course)

rahulahoop commented 2 years ago

@en-milie sure i think its with the subtype tho

    StatisticBKSubtype:
      type: string
      enum:
        - UNKNOWN
        - GRANT
        - REJECT
        - INQUERY
  responses:
    StatisticDataResponse:
      description: Returns the statistics for the chosen year
      content:
        application/json:
          schema:
            $ref: 'schemas/statistics-reports.yaml#/components/schemas/StatisticYear'

# another file...

    StatisticYear:
      type: object
      properties:
        year:
          type: integer
        months:
          type: array
          items:
            $ref: '#/components/schemas/StatisticMonth'
          maxItems: 12
          default: []
      required:
        - year
        - months
en-milie commented 2 years ago

@rahulahoop I did a run with the following schemas and didn't reproduce the problem. From the exception you mentioned above it seems that you use anyOf or oneOf with a discriminator. Can you also let me know where is that used?

statistics.yaml

openapi: "3.0.2"
info:
  title: API Title
  version: "1.0"
servers:
  - url: https://api.server.test/v1
paths:
  /statistics/bk/{subtype}:
    get:
      summary: Return statistics for BK case
      description: Returns the yearly statistics for BK cases. If no search parameter is given the
        current year will be used.
      operationId: get-statistics-bk
      parameters:
        - in: path
          name: subtype
          required: true
          schema:
            $ref: 'schemas/statistics-reports.yaml#/components/schemas/StatisticBKSubtype'
        - name: year
          in: query
          required: false
          schema:
            type: integer
      tags:
        - statistics
        - BK
      responses:
        200:
          $ref: '#/components/responses/StatisticDataResponse'
        404:
          description: No statistics for chosen year found
components:
  responses:
    StatisticDataResponse:
      description: Returns the statistics for the chosen year
      content:
        application/json:
          schema:
            $ref: 'schemas/statistics-reports.yaml#/components/schemas/StatisticYear'

statistics-reports.yaml:

openapi: "3.0.2"
info:
  title: API Title
  version: "1.0"
servers:
  - url: https://api.server.test/v1
paths:
  /test:
    get:
      responses:
        '200':
          description: OK
components:
  schemas:
    StatisticMonth:
      type: object
      properties:
        month:
          type: integer
        day:
          type: integer
    StatisticYear:
      type: object
      properties:
        year:
          type: integer
        months:
          type: array
          items:
            $ref: '#/components/schemas/StatisticMonth'
          maxItems: 12
          default: [ ]
      required:
        - year
        - months
    StatisticBKSubtype:
      type: string
      enum:
        - UNKNOWN
        - GRANT
        - REJECT
        - INQUERY
en-milie commented 2 years ago

@rahulahoop can I close this one? and maybe open a new one for the issue(s) you are facing?

rahulahoop commented 2 years ago

@en-milie sure thats a good idea