From 099b0752f593b74eecfd43f92be0a203d8b92281 Mon Sep 17 00:00:00 2001 From: release-it Date: Wed, 24 Nov 2021 19:37:48 +0000 Subject: [PATCH 1/2] add support for deepObject --- lib/validators.js | 15 +++++++++++++++ test/fixtures/openapi3/defs/pets.yaml | 11 +++++++++++ test/test-hapi-openapi3.js | 9 ++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/validators.js b/lib/validators.js index 8a44f6b..c1775ac 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -108,6 +108,21 @@ 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) => { + let 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); From 5d63f4cd345a9b8402f09666241cc8a1c727a5c5 Mon Sep 17 00:00:00 2001 From: release-it Date: Wed, 24 Nov 2021 19:39:09 +0000 Subject: [PATCH 2/2] lint --- lib/validators.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/validators.js b/lib/validators.js index c1775ac..9b19a64 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -109,16 +109,17 @@ const create = function (options = {}) { routeExt: function (request, h) { const p = parameter.in === 'query' ? 'query' : 'params'; - if(parameter.style === "deepObject") { + if (parameter.style === 'deepObject') { request[p] = Object.keys(request[p]).reduce((data, key) => { - let match = key.match(new RegExp(`^${parameter.name}\\[([^\\]]*)]$`)); - if(match) { + const match = key.match(new RegExp(`^${parameter.name}\\[([^\\]]*)]$`)); + if (match) { data[parameter.name] = { - ...data[parameter.name] ?? {}, + ...data[parameter.name] || {}, [match[1]]:request[p][key] - } + }; delete request[p][key]; } + return data; },request[p]); }