Gi60s / openapi-enforcer

Apache License 2.0
94 stars 22 forks source link

Expected an integer/array. Received: undefined #66

Closed jy95 closed 4 years ago

jy95 commented 4 years ago

Hello,

First thanks @Gi60s for the regular improvements. I found a little bug that prevents to update openapi-enforcer 1.8.1 to the latest version (1.8.7)

In my OAS definition, I included some examples for requests :

# Examples for /search
examples:
  searchExample1:
    description: "Search the first 10 exercises that have 'Java' in their title and have some specific tags ( 1 AND (2 OR 3 OR 4) )"
    value:
      data:
        title: "Java"
        tags: [1, [2, 3, 4]]
  searchExample2:
    description: "Search the exercises on page 2 that have 'Java' in their title but with specific tags ( (NOT 1) AND (2 OR 3)  )"
    value:
      metadata:
        page: 2
        size: 10
      data:
        title: "Java"
        tags: [-1, [2, 3]]

but It seems openapi-enforcer is unable to validate these examples :

  console.error node_modules/jest-jasmine2/build/jasmine/Env.js:249
    Error: One or more errors exist in the OpenApi definition
      at: paths
        at: /api/search > post > requestBody > content > application/json > examples
          at: searchExample1
            Invalid value
              Did not validate against all schemas
                at: 0 > data > tags > 1
                  Did not validate against exactly one schema
                    at: 0
                      Invalid value
                        Expected an integer. Received: undefined
                    at: 1
                      Invalid value
                        Expected an array. Received: undefined
          at: searchExample2
            Invalid value
              Did not validate against all schemas
                at: 0 > data > tags > 1
                  Did not validate against exactly one schema
                    at: 0
                      Invalid value
                        Expected an integer. Received: undefined
                    at: 1
                      Invalid value
                        Expected an array. Received: undefined
        at /__w/sourcecode_api/sourcecode_api/node_modules/openapi-enforcer-middleware/index.js:95:28

Thanks for the help

Gi60s commented 4 years ago

I'm glad to help where I can. I'm also glad that you're enjoying this library.

Will you send me a link for the full OpenAPI document or include it in a comment here. That will help with debugging. Thanks.

jy95 commented 4 years ago

As I split my OAS into several files in my repository, the problem is localizated here :

components:
  schemas:
    SearchDataCriterias:
      type: object
      description: "Search criterias"
      properties:
        title:
          type: string
          description: "Something we want to find inside the title of exercises"
          maxLength: 100
          example: "SINF2MS"
        tags:
          type: array
          items:
            oneOf:
              - type: integer
                format: int32
                example: 1
              - type: array
                items:
                  type: integer
                  format: int32
                minItems: 1 # Not allowing empty array if we use it
          description: "Tags search encoded in Conjunctive Normal Form. (for NOT predicat, use a negative integer)"
          example: [-1, [2, 3]]
        user_ids:
          type: array
          description: "Filter the exercises by their creators."
          minItems: 1
          items:
            type: integer
            minimum: 0
            description: "An user ID"
          uniqueItems: true
        exercise_ids:
          type: array
          description: "Filter the exercises by their ids."
          minItems: 1
          items:
            type: integer
            minimum: 0
            description: "An exercise ID"
          uniqueItems: true  
        vote:
          description: "Only take exercises that meet a given threshold"
          type: object
          properties:
            operator:
              description: "The operation we want to apply"
              type: string
              enum: ['<=', '<', '>=', '>']
            value:
              description: "The threshold value"
              type: number
              minimum: 0.0
              maximum: 5.0
              example: 5.0
          required:
            - operator
            - value
Gi60s commented 4 years ago

Wow, I had to dig to find that one. :)

This is fixed and published to NPM as version 1.8.8.

jy95 commented 4 years ago

Some days I feel like a bug detector unintentionally ^^ Thanks