Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Remove input schemas check from jsonBodyHandler
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
postatum committed Jul 14, 2020
1 parent a877fea commit 44fb3af
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 49 deletions.
16 changes: 1 addition & 15 deletions osprey-method-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -371,6 +356,7 @@ function jsonBodyHandler (body, path, methodName, options) {
return next()
})

const isNodeShape = body.schema.properties !== undefined
if (!isNodeShape) {
return compose(middleware)
}
Expand Down
94 changes: 60 additions & 34 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 44fb3af

Please sign in to comment.