diff --git a/lib/validators.js b/lib/validators.js index 8a44f6b..9b19a64 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -108,6 +108,22 @@ const create = function (options = {}) { schema, routeExt: function (request, h) { const p = parameter.in === 'query' ? 'query' : 'params'; + + if (parameter.style === 'deepObject') { + request[p] = Object.keys(request[p]).reduce((data, key) => { + const match = key.match(new RegExp(`^${parameter.name}\\[([^\\]]*)]$`)); + if (match) { + data[parameter.name] = { + ...data[parameter.name] || {}, + [match[1]]:request[p][key] + }; + delete request[p][key]; + } + + return data; + },request[p]); + } + if (request[p][parameter.name] !== undefined) { request[p][parameter.name] = coerce && request[p][parameter.name] && coerce(request[p][parameter.name]); } diff --git a/test/fixtures/openapi3/defs/pets.yaml b/test/fixtures/openapi3/defs/pets.yaml index 1e88c02..977111d 100644 --- a/test/fixtures/openapi3/defs/pets.yaml +++ b/test/fixtures/openapi3/defs/pets.yaml @@ -35,6 +35,17 @@ paths: schema: type: integer format: int32 + - name: filter + in: query + description: maximum number of results to return + schema: + type: object + properties: + breeds: + type: array + items: + type: string + style: deepObject responses: 200: description: pet response diff --git a/test/test-hapi-openapi3.js b/test/test-hapi-openapi3.js index afa90f2..adaea02 100644 --- a/test/test-hapi-openapi3.js +++ b/test/test-hapi-openapi3.js @@ -8,7 +8,7 @@ const StubAuthTokenScheme = require('./fixtures/lib/stub-auth-token-scheme'); Test('test plugin', function (t) { t.test('basic API', async function (t) { - t.plan(8); + t.plan(9); const server = new Hapi.Server(); @@ -68,6 +68,13 @@ Test('test plugin', function (t) { }); t.strictEqual(response.statusCode, 200, `${response.request.path} OK.`); + + response = await server.inject({ + method: 'GET', + url: '/v1/petstore/pets?filter%5Bbreeds%5D=bully&filter%5Bbreeds%5D=chartreux' + }); + + t.strictEqual(response.statusCode, 200, `${response.request.path} OK.`); } catch (error) { t.fail(error.message);