From 6216906c504723740802fe60b8ea673ec8c214bc Mon Sep 17 00:00:00 2001 From: release-it Date: Tue, 2 Nov 2021 11:27:54 +0100 Subject: [PATCH 1/5] x-yaml --- lib/validators.js | 9 ++++++++- test/fixtures/defs/pets.json | 3 ++- test/test-hapi-openapi.js | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/validators.js b/lib/validators.js index 8a44f6b..df1ebde 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -2,6 +2,7 @@ const Enjoi = require('enjoi'); const Joi = require('joi'); +const yaml = require('js-yaml'); const extensions = [ { type: 'int64', base: Joi.string().regex(/^\d+$/) }, @@ -191,7 +192,13 @@ const create = function (options = {}) { if (requestBody.content[mediaType].schema) { const parameter = { in: 'body', schema: requestBody.content[mediaType].schema, name: 'body' }; const validator = makeValidator(parameter, consumes, openapi, allowUnknownProperties); - validate.payload = validator.validate; + validate.payload = (body, ctx) => { + let contentType = ctx.context.headers["content-type"]; + if(consumes.includes(contentType) && contentType === "text/x-yaml") { + body = yaml.load(a[0]); + } + return validator.validate(body, context); + } break; } } diff --git a/test/fixtures/defs/pets.json b/test/fixtures/defs/pets.json index 6e70d70..40593eb 100644 --- a/test/fixtures/defs/pets.json +++ b/test/fixtures/defs/pets.json @@ -22,7 +22,8 @@ "http" ], "consumes": [ - "application/json" + "application/json", + "text/x-yaml" ], "produces": [ "application/json" diff --git a/test/test-hapi-openapi.js b/test/test-hapi-openapi.js index 4a606af..9c474b1 100644 --- a/test/test-hapi-openapi.js +++ b/test/test-hapi-openapi.js @@ -497,6 +497,43 @@ Test('test plugin', function (t) { }); + t.test('validate yaml', async function (t) { + t.plan(2); + + const server = new Hapi.Server(); + + try { + await server.register({ + plugin: OpenAPI, + options: { + api: Path.join(__dirname, './fixtures/defs/pets.json'), + handlers: Path.join(__dirname, './fixtures/handlers') + } + }); + response = await server.inject({ + method: 'POST', + headers: {"content-type": "text/x-yaml"}, + url: '/v1/petstore/pets', + payload: "id: '0'\nname: 'Cat'" + }); + + t.strictEqual(response.statusCode, 200, `${response.request.path} OK.`); + + response = await server.inject({ + method: 'POST', + headers: {"content-type": "text/x-yaml"}, + url: '/v1/petstore/pets', + payload: `name: 123` + }); + + t.strictEqual(response.statusCode, 400, `${response.request.path} payload bad.`); + } + catch (error) { + t.fail(error.message); + } + + }); + t.test('routes', async function (t) { t.plan(5); From 17d36712376bef2fcc6f2f41012f6cbfa47bd6ca Mon Sep 17 00:00:00 2001 From: release-it Date: Tue, 2 Nov 2021 11:35:45 +0100 Subject: [PATCH 2/5] lint --- lib/validators.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/validators.js b/lib/validators.js index df1ebde..9423f23 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -2,7 +2,7 @@ const Enjoi = require('enjoi'); const Joi = require('joi'); -const yaml = require('js-yaml'); +const Yaml = require('js-yaml'); const extensions = [ { type: 'int64', base: Joi.string().regex(/^\d+$/) }, @@ -193,13 +193,21 @@ const create = function (options = {}) { const parameter = { in: 'body', schema: requestBody.content[mediaType].schema, name: 'body' }; const validator = makeValidator(parameter, consumes, openapi, allowUnknownProperties); validate.payload = (body, ctx) => { - let contentType = ctx.context.headers["content-type"]; - if(consumes.includes(contentType) && contentType === "text/x-yaml") { - body = yaml.load(a[0]); + const contentType = ctx.context.headers['content-type']; + if (consumes.includes(contentType) && contentType === 'text/x-yaml') { + try { + body = Yaml.load(body); + } + catch (err) { + // ignore parsing error + } } - return validator.validate(body, context); - } + + return validator.validate(body, ctx); + }; + break; + } } } From 261d197e02a679ae3784afc44e6ea5d5b415ef10 Mon Sep 17 00:00:00 2001 From: release-it Date: Tue, 2 Nov 2021 14:09:28 +0100 Subject: [PATCH 3/5] fix tests --- lib/validators.js | 4 +-- test/fixtures/defs/pets.json | 3 +-- test/fixtures/openapi3/defs/pets.yaml | 3 +++ test/test-hapi-openapi.js | 38 -------------------------- test/test-hapi-openapi3.js | 39 +++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 42 deletions(-) diff --git a/lib/validators.js b/lib/validators.js index 9423f23..3c3c886 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -201,8 +201,8 @@ const create = function (options = {}) { catch (err) { // ignore parsing error } - } - + } + return validator.validate(body, ctx); }; diff --git a/test/fixtures/defs/pets.json b/test/fixtures/defs/pets.json index 40593eb..6e70d70 100644 --- a/test/fixtures/defs/pets.json +++ b/test/fixtures/defs/pets.json @@ -22,8 +22,7 @@ "http" ], "consumes": [ - "application/json", - "text/x-yaml" + "application/json" ], "produces": [ "application/json" diff --git a/test/fixtures/openapi3/defs/pets.yaml b/test/fixtures/openapi3/defs/pets.yaml index 1e88c02..f74b5b2 100644 --- a/test/fixtures/openapi3/defs/pets.yaml +++ b/test/fixtures/openapi3/defs/pets.yaml @@ -83,6 +83,9 @@ paths: application/json: schema: $ref: '#/components/schemas/newPet' + text/x-yaml: + schema: + $ref: '#/components/schemas/newPet' required: true responses: 200: diff --git a/test/test-hapi-openapi.js b/test/test-hapi-openapi.js index 9c474b1..515c809 100644 --- a/test/test-hapi-openapi.js +++ b/test/test-hapi-openapi.js @@ -496,44 +496,6 @@ Test('test plugin', function (t) { } }); - - t.test('validate yaml', async function (t) { - t.plan(2); - - const server = new Hapi.Server(); - - try { - await server.register({ - plugin: OpenAPI, - options: { - api: Path.join(__dirname, './fixtures/defs/pets.json'), - handlers: Path.join(__dirname, './fixtures/handlers') - } - }); - response = await server.inject({ - method: 'POST', - headers: {"content-type": "text/x-yaml"}, - url: '/v1/petstore/pets', - payload: "id: '0'\nname: 'Cat'" - }); - - t.strictEqual(response.statusCode, 200, `${response.request.path} OK.`); - - response = await server.inject({ - method: 'POST', - headers: {"content-type": "text/x-yaml"}, - url: '/v1/petstore/pets', - payload: `name: 123` - }); - - t.strictEqual(response.statusCode, 400, `${response.request.path} payload bad.`); - } - catch (error) { - t.fail(error.message); - } - - }); - t.test('routes', async function (t) { t.plan(5); diff --git a/test/test-hapi-openapi3.js b/test/test-hapi-openapi3.js index afa90f2..e95b36c 100644 --- a/test/test-hapi-openapi3.js +++ b/test/test-hapi-openapi3.js @@ -74,6 +74,45 @@ Test('test plugin', function (t) { } }); + + t.test('validate yaml', async function (t) { + t.plan(2); + + const server = new Hapi.Server(); + + try { + await server.register({ + plugin: OpenAPI, + options: { + api: Path.join(__dirname, './fixtures/openapi3/defs/pets.yaml'), + handlers: Path.join(__dirname, './fixtures/handlers') + } + }); + let response = await server.inject({ + method: 'POST', + headers: {"content-type": "text/x-yaml"}, + url: '/v1/petstore/pets', + payload: "id: '0'\nname: 'Cat'" + }); + + t.strictEqual(response.statusCode, 200, `${response.request.path} OK.`); + + response = await server.inject({ + method: 'POST', + headers: {"content-type": "text/x-yaml"}, + url: '/v1/petstore/pets', + payload: `id: '0'\nname_does_not_exists: 'Cat'` + }); + + t.strictEqual(response.statusCode, 400, `${response.request.path} payload bad.`); + } + catch (error) { + t.fail(error.message); + } + + }); + + t.test('routes with output validation', async function (t) { t.plan(5); From 04aee1f5317acc88e0cf974bed661c72b57c8dc0 Mon Sep 17 00:00:00 2001 From: release-it Date: Tue, 2 Nov 2021 13:11:00 +0000 Subject: [PATCH 4/5] lint --- test/test-hapi-openapi.js | 1 + test/test-hapi-openapi3.js | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/test/test-hapi-openapi.js b/test/test-hapi-openapi.js index 515c809..4a606af 100644 --- a/test/test-hapi-openapi.js +++ b/test/test-hapi-openapi.js @@ -496,6 +496,7 @@ Test('test plugin', function (t) { } }); + t.test('routes', async function (t) { t.plan(5); diff --git a/test/test-hapi-openapi3.js b/test/test-hapi-openapi3.js index e95b36c..a82bc14 100644 --- a/test/test-hapi-openapi3.js +++ b/test/test-hapi-openapi3.js @@ -74,7 +74,6 @@ Test('test plugin', function (t) { } }); - t.test('validate yaml', async function (t) { t.plan(2); @@ -112,7 +111,6 @@ Test('test plugin', function (t) { }); - t.test('routes with output validation', async function (t) { t.plan(5); @@ -377,4 +375,4 @@ Test('test plugin', function (t) { t.fail(error.message); } }); -}); \ No newline at end of file +}); From 36a38b89ef44524e8ad34074b439f88e478527cd Mon Sep 17 00:00:00 2001 From: release-it Date: Tue, 2 Nov 2021 13:12:00 +0000 Subject: [PATCH 5/5] lint --- lib/validators.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/validators.js b/lib/validators.js index 3c3c886..71b1746 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -205,7 +205,6 @@ const create = function (options = {}) { return validator.validate(body, ctx); }; - break; }