Skip to content

Commit

Permalink
Don't skip parameter validation when parameter array is empty. (#68)
Browse files Browse the repository at this point in the history
* Don't skip parameter validation when parameter array is empty.

* Add test case for validation of parameters when parameters aren't specified.

* Improve test coverage.

Co-authored-by: Igor Savin <iselwin@gmail.com>
  • Loading branch information
wparad and kibertoad committed May 11, 2021
1 parent 6473579 commit da58523
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/index.js
Expand Up @@ -122,10 +122,8 @@ function buildRequestValidator(referenced, dereferenced, currentPath, currentMet
localParameters = oai2.buildPathParameters(parameters, pathParameters);
}

if (localParameters.length > 0 || options.contentTypeValidation) {
requestSchema.parameters = buildParametersValidation(localParameters,
dereferenced.paths[currentPath][currentMethod].consumes || dereferenced.paths[currentPath].consumes || dereferenced.consumes, options);
}
requestSchema.parameters = buildParametersValidation(localParameters,
dereferenced.paths[currentPath][currentMethod].consumes || dereferenced.paths[currentPath].consumes || dereferenced.consumes, options);

return requestSchema;
}
Expand Down
8 changes: 3 additions & 5 deletions src/utils/ajv-utils.js
Expand Up @@ -3,11 +3,9 @@ const filesKeyword = require('../customKeywords/files'),
contentKeyword = require('../customKeywords/contentTypeValidation');

function addCustomKeyword(ajv, formats, keywords) {
if (formats) {
formats.forEach(function (format) {
ajv.addFormat(format.name, format.pattern);
});
}
formats.forEach(function (format) {
ajv.addFormat(format.name, format.pattern);
});

if (keywords) {
keywords.forEach((keyword) => {
Expand Down
2 changes: 1 addition & 1 deletion test/openapi3/general/general-oai3-test.js
Expand Up @@ -270,7 +270,7 @@ describe('oai3 - general tests', () => {
describe('init with json schema', () => {
function loadYamlAsJson(filename) {
const swaggerPath = path.join(__dirname, filename);
const jsonSchema = yaml.load(fs.readFileSync(swaggerPath), 'utf8');
const jsonSchema = yaml.load(fs.readFileSync(swaggerPath, 'utf8'));
return jsonSchema;
}
describe('loading yaml with discriminator with allOf', () => {
Expand Down
15 changes: 15 additions & 0 deletions test/openapi3/request/pets-request.yaml
Expand Up @@ -109,6 +109,21 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/error_model'
/pets-empty-query:
get:
summary: get all pets with no query allowed
security:
- public-key: []
description: >-
tags:
- pets
responses:
'200':
description: list of pets
content:
application/json:
schema:
$ref: '#/components/schemas/pets'
/pets/{pet_id}:
get:
summary: get pet by id
Expand Down
33 changes: 32 additions & 1 deletion test/openapi3/request/request-oai3-test.js
Expand Up @@ -4,13 +4,16 @@ const chai = require('chai');
const schemaValidatorGenerator = require('../../../src/index');
const path = require('path');
const uuid = require('uuid').v4;
const yaml = require('js-yaml');
const fs = require('fs').promises;

const expect = chai.expect;

const swaggerPath = path.join(__dirname, 'pets-request.yaml');

describe('oai3 - request tests', function () {
let schema;
before(function () {
const swaggerPath = path.join(__dirname, 'pets-request.yaml');
schema = schemaValidatorGenerator.buildSchemaSync(swaggerPath, {});
});
describe('check headers', function () {
Expand Down Expand Up @@ -169,6 +172,34 @@ describe('oai3 - request tests', function () {
]);
expect(isParametersMatch).to.be.false;
});

it('invalid query due to invalid parameters specified', async function () {
const path = '/pets-empty-query';
const method = 'get';
const endpointSchema = schema[path][method];
const jsonSchema = yaml.load(await fs.readFile(swaggerPath, 'utf8'));

// Validate that the test case does not contain parameters set
expect(jsonSchema.paths[path][method].parameters || []).to.be.empty;

let isParametersMatch = endpointSchema.parameters.validate({
query: { page: '1' },
headers: {},
path: {},
files: undefined });
expect(endpointSchema.parameters.errors).to.be.eql([
{
dataPath: '.query',
keyword: 'additionalProperties',
message: 'should NOT have additional properties',
params: {
additionalProperty: 'page'
},
schemaPath: '#/properties/query/additionalProperties'
}
]);
expect(isParametersMatch).to.be.false;
});
});

describe('check path', function () {
Expand Down

0 comments on commit da58523

Please sign in to comment.