Gi60s / openapi-enforcer

Apache License 2.0
94 stars 22 forks source link

Cannot validate arrays in the query string when using openapi.request #156

Closed drewpureescapes closed 11 months ago

drewpureescapes commented 1 year ago

When using the openapi.request function (https://openapi-enforcer.com/api/components/openapi.html#request) having query string parameters that are an array of enums like:

    arrayenums:
      name: arrayenums
      in: query
      description: 0-n from the enum list
      required: false
      schema:
        type: array
        items:
          type: string
          enum:
            - "Foo"
            - "Bar"
            - "Baz"

and calling the function like:

const input = {
    method: 'GET',
    path: '/foo',
    query: {
        arrayenums: ['Foo', 'Bar'],
    },
};
const [req, validationError] = openapi.request(input);

I get the error Expected an object with string keys and string values.

I've tried comma separating the item to be like 'Foo,Bar' but then it fails the enum check.

Is it not possible to validate an item from the query string that's an array?

Gi60s commented 1 year ago

Hey @drewpureescapes. I'll make some time to check this out today and I'll get back to you.

Thanks for the issue.

Gi60s commented 1 year ago

I just wanted to update you that I've confirmed that this is a bug. I'll work on getting a fix in.

There is also an alternative that you can use for the time being. Instead of putting the query parameters in the query object map, instead, add the query parameters to the path.

For example:

const input = {
    method: 'GET',
    path: '/foo?arrayenums=Foo&arrayenums=Bar'
};
const [req, validationError] = openapi.request(input);

Placing the query parameters in the path seems to be working for all parameter styles and explode combinations as documented here: https://swagger.io/docs/specification/serialization

drewpureescapes commented 1 year ago

Hi James,

Thank you. The workaround tides me over for now. As I'm working in an AWS Lambda I have an event and the query values are already split out for me into an object, so I have to rebuild the query string with qs to use the workaround, but it'll do until your fix is ready. Thank you.

Gi60s commented 1 year ago

You may be interested in this NPM package too if you're using AWS Lambda: https://www.npmjs.com/package/openapi-enforcer-lambda

I haven't worked on it or used it for a while, but I think it runs fine. It is also supposed to make it easy to develop and test your API locally before pushing it to AWS.

Green-Peter commented 2 weeks ago

I am receiving the same error when using just an array, even without an enum, just a simple array parameter with items: string.

Will there be a fix for this?

Gi60s commented 1 week ago

@Green-Peter I'll look into it again. If I remember correctly it was not an easy fix.

If you can provide me more details on what you're trying to do then I might be able to give you some ideas to solve the problem.