openapi-backend icon indicating copy to clipboard operation
openapi-backend copied to clipboard

Fix query coercion issue

Open sabeslamidze opened this issue 1 year ago • 4 comments

Fixed #554

Note that query parameters are coerced just fine by Ajv validators. But parameters.query is never assigned back to original request that is passed down to handlers.

However, this might be not desirable behaviour by default. Maybe it is better to add additional option on OpenAPIBackend initialization to allow modifying data during validation like this:

  const api = new OpenAPIBackend({
    definition: "./open-api-spec.json",
    validate: true,
    **modifyReqData: true**,
    ajvOpts: {
      coerceTypes: true
    }
    ...
  })

Pls let me know what you think.

sabeslamidze avatar May 12 '23 14:05 sabeslamidze

@sabeslamidze I agree it'd be better to put this behind an option flag. 👍 Generally the less processing we make to the passed request object the better IMO.

anttiviljami avatar Jul 09 '23 18:07 anttiviljami

@anttiviljami don't you think if your schema says it must be a number, your code should receive it as a number? Why would I need to parse numbers (and to other types) manually in every handler?

ziflex avatar Dec 01 '23 18:12 ziflex

@sabeslamidze @anttiviljami @ziflex I understand that you want to keep original request as is. However, having a field of type number in code and then in run-time it is a string is not ok. AJV has an option called 'coerceTypes'. You can support that option and that will solve the issue, no? When can I expect this to be merged?

ydayagi avatar Dec 05 '23 07:12 ydayagi

@ydayagi no, I meant that I want this library to parse numbers for me. I do not want to parse params in every handler.

ziflex avatar Dec 07 '23 14:12 ziflex