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

Can not demand an array to be one or more items long #125

Open
hermann-p opened this issue Jan 10, 2020 · 2 comments
Open

Can not demand an array to be one or more items long #125

hermann-p opened this issue Jan 10, 2020 · 2 comments

Comments

@hermann-p
Copy link

hermann-p commented Jan 10, 2020

I just ran into a strange issue, and I'm kind of surprised no one else seemed to do yet. I just wrote a request body description for a POST endpoint, requiring the request body to be of application/json and containing three fields, one of them an array of strings. When I try setting the array's minItems property to 1, I get an error saying

Error: schema is invalid: data/properties/value/properties/articleIds/minItems should be integer

However, when I set it to 2, parsing succeeds, and the endpoint returns validation errors when I pass an array with less than two items.

The relevant part of the YAML reads like this:

 requestBody:
      required: true
      content:
        application/json:
          schema:
            type: object
            properties:
              articleIds:
                type: array
                uniqueItems: true
                minItems: 1 # this fails until I set it to some other number, e.g. 2
                items:
                  type: string
              details:
                type: object
                nullable: true
              kind:
                type: string
            required:
              - articleIds
              - kind

Let me know if I can do anything to help debugging this.

Btw, this lib helps us tremendously developing our project in a future-proof way, thanks for that.

@jwalton
Copy link
Contributor

jwalton commented Jan 10, 2020

Well, this is an interesting problem.

I tried adding this test way down at the validator level, and it passes, but I expected it to.

I tried adding:

  /bug125:
    post:
      summary: minItems should work
      # x-exegesis-controller: echoController.ts
      # x-exegesis-operationId: echo
      requestBody:
        required: true
        content:
          'application/json':
            schema:
              type: object
              properties:
                articleIds:
                  type: array
                  uniqueItems: true
                  minItems: '1'
                  items:
                    type: string
                details:
                  type: object
                  nullable: true
                kind:
                  type: string
              required:
                - articleIds
                - kind
      responses:
        '200':
          description: Respond
          content:
            'application/json':
              schema: { type: 'object' }

to test/integration/openapi.yaml, and it passes too. In order to get your minItems should be integer, I had to change minItems: 1 to minItems: '1'. So, "it works for me", although that's not a very satisfying answer, obviously.

I just pushed 2.3.0, so I'd try upgrading to that. Maybe some package we depend on was updated by greenkeeper and magically fixed this. :)

If that doesn't help, then generateValidator() is the function that's taking in your schema and passing it to ajv to generate a body validator function. So I'd start with a breakpoint there and see if minItems is an integer or a string at that point (if it's failing, I'm willing to bet it's a string). So then you just need to figure out where it gets transformed from an integer into a string.

bundle() would be another place to look - this is where we take in an OpenAPI object or a filename, and run it though json-schema-ref-parser to read the file, resolve all the $refs, and pass that on to the oas3 module. If it's somehow turning it from an integer to a string here, this might be a bug in json-schema-ref-parser or in the YAML parser they are using.

I'm traveling for the next week (taking my daughter to Disney World for her birthday!) so I probably won't get back to you about this next week. But if you haven't figured it out by then feel free to bug me some more. :)

@hermann-p
Copy link
Author

Thanks for your answer. Just an update: everything seems to be fine in the exegesis main-module. I now have assume the error occurs somewhere in the path from the exegesis-express module to the main module. I'll investigate further and keep you up to date.
You and your family have a nice trip and enjoy yourselves - and don't you dare to work over there ;)

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