axa-group / bauta.js

Bauta.js is an add-on for your Node.js applications such as Express.js or Fastify.
Other
42 stars 3 forks source link

bautajs-fastify: validation query param does not work with csv fields #135

Closed Xavier-Redondo closed 8 months ago

Xavier-Redondo commented 10 months ago

Context

We define an endpoint that has a query param that is an array and instead of using multi as the format, we use csv (comma separated value).

This is exemplified in this PR: https://github.com/axa-group/bauta.js/pull/133

For this issues we are referring to the following endpoint defined in the api:

 "/array-query-param-csv" :{
      "get": {
        "operationId": "queryParamAsArrayCsv",
        "summary": "Generates an array with all the input query params",
        "parameters": [
          {
            "name": "chickenIds",
            "in": "query",
            "required": true,
            "schema": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "minItems": 1,
              "maxItems": 1000
            },
            "style": "form",
            "explode": false
          }
        ],
        "responses": {
             // Removed to reduce size
        }
      }
    },

According to this endpoint, these url's should be valid:

http://localhost:8080/api/array-query-param-csv?chickenIds=2,3 http://localhost:8080/api/array-query-param-csv?chickenIds=2

Current behaviour

None of the two url's work. Instead of having an array with the query param elements we get the following validation error:

{"statusCode":400,"error":"Bad Request","message":"The request was not valid"}

with the following error log in the server:

js {"level":30,"time":1705508619246,"pid":50352,"hostname":"xxx","reqId":"req-2","res":{"statusCode":400},"err":{"type":"ValidationError","message":"The request was not valid","stack":"Validation Error: The request was not valid \n {\n \"name\": \"Validation Error\",\n \"errors\": [\n {\n \"path\": \"#/properties/chickenIds/type\",\n \"location\": \"querystring\",\n \"message\": \"must be array\",\n \"errorCode\": \"type\"\n }\n ],\n \"statusCode\": 400,\n \"message\": \"The request was not valid\"\n}","aggregateErrors":[{"type":"Object","message":"must be array","stack":"","path":"#/properties/chickenIds/type","location":"querystring","errorCode":"type"}],"name":"Validation Error","errors":[{"path":"#/properties/chickenIds/type","location":"querystring","message":"must be array","errorCode":"type"}],"statusCode":400},"msg":"The request was not valid"}

Expected behaviour

Ideally this should work for both cases as in the case of multi.

Note: i will not treat this as a bug but as a feature, it seems that we simply do not support this because the libraries we are using do not do it.