From 44fb3afc0f7cbf7c04d3d3eb93683a165a9fd8af Mon Sep 17 00:00:00 2001 From: Artem Kostiuk Date: Tue, 14 Jul 2020 09:44:11 +0300 Subject: [PATCH] Remove input schemas check from jsonBodyHandler This behaviour was ported from the old version of osprey-method-handler. The check limited body schema types which were validated in jsonBodyHandler to Scalar, Node, Array only. The issue was discovered when trying to validate a body of type Union. The validation just did not happen because the body schema was ignored as it didn't pass the check. Now the check is completely removed as it doesn't seem to make sense. As jsonBodyHandler is only called for application/json requests, all the request bodies defined in RAML for application/json should work well with validation. --- osprey-method-handler.js | 16 +------ test/index.js | 94 +++++++++++++++++++++++++--------------- 2 files changed, 61 insertions(+), 49 deletions(-) diff --git a/osprey-method-handler.js b/osprey-method-handler.js index 3e9941e..2d93229 100644 --- a/osprey-method-handler.js +++ b/osprey-method-handler.js @@ -346,21 +346,6 @@ function jsonBodyHandler (body, path, methodName, options) { }) const middleware = [jsonBodyParser] - /* - Check whether body.schema has type specified in RAML. - - In case it's not specified, its type would be AnyShape. - Type checks are performed by checking type-specific properties - instead of using 'instanceof' because of this issue: - https://github.com/aml-org/amf/issues/569 - */ - const isScalarShape = body.schema.dataType !== undefined - const isNodeShape = body.schema.properties !== undefined - const isArrayShape = body.schema.items !== undefined - if (!isScalarShape && !isNodeShape && !isArrayShape) { - return compose(middleware) - } - middleware.push(async function ospreyJsonBodyValidator (req, res, next) { const report = validateWithExtras( body.schema, req.body, options.ajv) @@ -371,6 +356,7 @@ function jsonBodyHandler (body, path, methodName, options) { return next() }) + const isNodeShape = body.schema.properties !== undefined if (!isNodeShape) { return compose(middleware) } diff --git a/test/index.js b/test/index.js index 9549c60..e9f1019 100644 --- a/test/index.js +++ b/test/index.js @@ -571,46 +571,74 @@ describe('osprey method handler', function () { describe('body', function () { describe('addJsonSchema', function () { - const JSON_SCHEMA = JSON.stringify({ - $schema: 'http://json-schema.org/draft-04/schema#', - title: 'Product set', - type: 'array', - items: { - title: 'Product', - type: 'object', - properties: { - id: { - description: 'The unique identifier for a product', - type: 'number' + const testRamlStr = `#%RAML 1.0 +types: + ProductSet: + type: | + { + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Product set", + "type": "array", + "items": { + "title": "Product", + "type": "object", + "properties": { + "id": { + "description": "The unique identifier for a product", + "type": "number" }, - name: { type: 'string' }, - price: { - type: 'number', - minimum: 0, - exclusiveMinimum: true + "name": { + "type": "string" }, - tags: { - type: 'array', - items: { type: 'string' }, - minItems: 1, - uniqueItems: true + "price": { + "type": "number", + "minimum": 0, + "exclusiveMinimum": true }, - dimensions: { - type: 'object', - properties: { - length: { type: 'number' }, - width: { type: 'number' }, - height: { type: 'number' } + "tags": { + "type": "array", + "items": { + "type": "string" }, - required: ['length', 'width', 'height'] + "minItems": 1, + "uniqueItems": true }, - warehouseLocation: { - description: 'Coordinates of the warehouse with the product', - $ref: 'http://json-schema.org/geo' + "dimensions": { + "type": "object", + "properties": { + "length": { + "type": "number" + }, + "width": { + "type": "number" + }, + "height": { + "type": "number" + } + }, + "required": [ + "length", + "width", + "height" + ] + }, + "warehouseLocation": { + "description": "Coordinates of the warehouse with the product", + "$ref": "http://json-schema.org/geo" } }, - required: ['id', 'name', 'price'] + "required": [ + "id", + "name", + "price" + ] } + } + ` + let schema + before(async function () { + const model = await wp.WebApiParser.raml10.parse(testRamlStr) + schema = model.declares[0] }) it('should support external $ref when added', async function () { @@ -622,7 +650,6 @@ describe('osprey method handler', function () { 'http://json-schema.org/geo' ) - const schema = new wp.model.domain.SchemaShape().withRaw(JSON_SCHEMA) const method = makeRequestMethod('application/json', schema) app.post('/', ospreyMethodHandler(method), function (req, res) { @@ -662,7 +689,6 @@ describe('osprey method handler', function () { const addSchema = sinon.spy(ajv, 'addSchema') - const schema = new wp.model.domain.SchemaShape().withRaw(JSON_SCHEMA) const method = makeRequestMethod('application/json', schema) const app = ospreyRouter()