Skip to content

Commit

Permalink
Merge 94e39bc into f974df2
Browse files Browse the repository at this point in the history
  • Loading branch information
clarakosi committed Apr 17, 2019
2 parents f974df2 + 94e39bc commit ed51b6b
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 51 deletions.
12 changes: 6 additions & 6 deletions lib/filters/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ const supportedValidators = ['maximum',
* @constructor
*/
class Validator {
constructor(parameters, definitions) {
constructor(parameters, schemas) {
this._ajv = constructAjv({ verbose: true });
if (definitions) {
Object.keys(definitions).forEach((schemaName) => {
if (schemas) {
Object.keys(schemas).forEach((schemaName) => {
this._ajv.addSchema(
definitions[schemaName],
`#/definitions/${schemaName}`
schemas[schemaName],
`#/components/schemas/${schemaName}`
);
});
}
Expand Down Expand Up @@ -256,7 +256,7 @@ module.exports = (hyper, req, next, options, specInfo) => {
} else {
const validator = new Validator(
specInfo.spec.parameters,
specInfo.specRoot && specInfo.specRoot.definitions
specInfo.specRoot.components && specInfo.specRoot.components.schemas
);
CACHE.set(specInfo.spec, validator);
validator.validate(req);
Expand Down
5 changes: 1 addition & 4 deletions lib/hyperswitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,7 @@ HyperSwitch.prototype.defaultListingHandler = function (match, hyper, req) {
match.value.specRoot && !match.value.specRoot['x-listing']) {
return P.resolve({
status: 200,
body: Object.assign({}, match.value.specRoot, {
// Set the base path dynamically
basePath: getDocBasePath(req, match.value.specRoot)
})
body: Object.assign({}, match.value.specRoot)
});
} else if (rq.path ||
(match.value.specRoot &&
Expand Down
19 changes: 9 additions & 10 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fs = P.promisifyAll(require('fs'));
const handlerTemplate = require('./handlerTemplate');
const swaggerRouter = require('swagger-router');
const path = require('path');
const utils = require('./utils');

const Node = swaggerRouter.Node;
const Template = swaggerRouter.Template;
Expand Down Expand Up @@ -440,7 +441,7 @@ class Router {
subtree = new Node();
// Set up our specific value object
subtree.value = value;
value.path = childScope.specRoot.basePath + childScope.prefixPath;
value.path = childScope.specRoot.servers[0].url + childScope.prefixPath;
value.methods = {};
// XXX: Set ACLs and other value properties for path
// subtree.value.acls = ...;
Expand Down Expand Up @@ -474,7 +475,7 @@ class Router {
subtree.value = value;
// Copy over the remaining value properties.
Object.assign(subtree.value, origSubtree.value);
subtree.value.path = childScope.specRoot.basePath + childScope.prefixPath;
subtree.value.path = childScope.specRoot.servers[0].url + childScope.prefixPath;
specPromise = P.resolve();
}
branchNode.setChild(path[path.length - 1], subtree);
Expand Down Expand Up @@ -505,11 +506,10 @@ class Router {
scope.rootScope = scope;
}

// Merge in definitions & securityDefinitions from the spec.
// Merge in components from the spec.
// TODO: Do we need a clone here? Is it okay if those definitions are
// added to the higher level spec?
Object.assign(scope.specRoot.definitions, spec.definitions);
Object.assign(scope.specRoot.securityDefinitions, spec.securityDefinitions);
utils.mergeDeep(scope.specRoot.components, spec.components);
scope.specRoot.tags = scope.specRoot.tags.concat(spec.tags || [])
.filter((tag, index, self) => {
return index === self.findIndex((t) => {
Expand Down Expand Up @@ -637,22 +637,21 @@ class Router {
Router.prototype._createNewApiRoot = function (node, spec, scope) {
const specRoot = Object.assign({}, spec);
// Make sure the spec has the standard properties set up.
specRoot.swagger = spec.swagger || '2.0';
specRoot.definitions = spec.definitions || {};
specRoot.securityDefinitions = spec.securityDefinitions || {};
specRoot.openapi = spec.openapi || '3.0.1';
specRoot.components = spec.components || {};
specRoot['x-default-params'] = spec['x-default-params'] || {};
specRoot.tags = spec.tags || [];

delete specRoot['x-route-filters'];

// Reset paths. These are going to be built up during path setup.
specRoot.paths = {};
specRoot.basePath = scope.prefixPath;
specRoot.servers = [{ url: scope.prefixPath }];

node.setChild({ type: 'meta', name: 'apiRoot' }, new Node({
specRoot,
methods: {},
path: `${specRoot.basePath}/`,
path: `${specRoot.servers[0].url}/`,
globals: node.value && node.value.globals || scope.globals
}));

Expand Down
32 changes: 32 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,36 @@ class NullLogger {
}
utils.nullLogger = new NullLogger();

/**
* Deep merge two objects.
* @param {Object} target
* @param {Object} ...sources
* @return {Object}
*/
utils.mergeDeep = (target, ...sources) => {
const isObject = (item) => {
return (item && typeof item === 'object' && !Array.isArray(item));
};

if (!sources.length || !sources || !target || !isObject(target)) {
return target;
}

sources.forEach((source) => {
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach((key) => {
if (isObject(source[key])) {
if (!target[key]) {
target[key] = {};
}
utils.mergeDeep(target[key], source[key]);
} else {
target[key] = source[key];
}
});
}
});
return target;
};

module.exports = utils;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hyperswitch",
"version": "0.11.1",
"version": "0.11.2",
"description": "REST API creation framework",
"main": "index.js",
"scripts": {
Expand Down
30 changes: 16 additions & 14 deletions test/filters/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var assert = require('./../utils/assert.js');
var validator = require('../../lib/filters/validator');

var testValidator = (req, parameters, example, definitions) => {
var testValidator = (req, parameters, example, components) => {
return validator(null, req, function (hyper, req) {
if (example) {
assert.deepEqual(req, example);
Expand All @@ -13,7 +13,7 @@ var testValidator = (req, parameters, example, definitions) => {
parameters: parameters
},
specRoot: {
definitions: definitions
components: components
}
});
};
Expand Down Expand Up @@ -287,7 +287,7 @@ describe('Validator filter', function () {
}
});

it('Should support refs to definitions for parameters', () => {
it('Should support refs to components/schemas for parameters', () => {
testValidator({
body: {
test_value: 'four',
Expand All @@ -299,22 +299,24 @@ describe('Validator filter', function () {
in: 'body',
type: 'object',
schema: {
$ref: '#/definitions/test_schema'
$ref: '#/components/schemas/test_schema'
},
required: 'true'
}
], undefined, {
test_schema: {
type: 'object',
parameters: {
test_value: {
type: 'string'
},
int_test_value: {
type: 'integer'
schemas: {
test_schema: {
type: 'object',
parameters: {
test_value: {
type: 'string'
},
int_test_value: {
type: 'integer'
}
}
}
}
});
})
});
});
});
2 changes: 1 addition & 1 deletion test/hyperswitch/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Documentation handling',() => {
.then((res) => {
assert.deepEqual(res.status, 200);
assert.contentType(res, 'application/json');
assert.deepEqual(res.body.swagger, '2.0');
assert.deepEqual(res.body.openapi, '3.0.1');
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/hyperswitch/hyperswitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ describe('HyperSwitch context',() => {
.then((res) => {
assert.deepEqual(res.status, 200);
assert.contentType(res, 'application/json');
assert.deepEqual(res.body.swagger, '2.0');
assert.deepEqual(res.body.openapi, '3.0.1');
});
});

Expand Down
8 changes: 4 additions & 4 deletions test/router/api_module_1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ paths:
/{api:api}:
x-modules:
- spec:
definitions:
first_parameter:
description: First parameter definition
components:
schemas:
first_parameter:
description: First parameter definition
paths:
/one:
get:
x-request-handler:
- return_nothing:
return:
status: 200

9 changes: 5 additions & 4 deletions test/router/api_module_2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ paths:
/{api:api}:
x-modules:
- spec:
definitions:
second_parameter:
description: Second parameter definition
components:
schemas:
second_parameter:
description: Second parameter definition
paths:
/two:
get:
x-request-handler:
- return_nothing:
return:
status: 200
status: 200
4 changes: 2 additions & 2 deletions test/router/buildTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('Router',() => {
assert.deepEqual(!!node.value, true);
assert.deepEqual(!!node.value.specRoot, true);
var spec = node.value.specRoot;
assert.deepEqual(spec.definitions, {
assert.deepEqual(spec.components.schemas, {
first_parameter: {description: 'First parameter definition'},
second_parameter: {description: 'Second parameter definition'}
});
Expand All @@ -182,7 +182,7 @@ describe('Router',() => {
assert.deepEqual(!!node.value, true);
assert.deepEqual(!!node.value.specRoot, true);
var spec = node.value.specRoot;
assert.deepEqual(spec.definitions, {
assert.deepEqual(spec.components.schemas, {
some_object: {description: 'bla bla bla'}
});
assert.deepEqual(Object.keys(spec.paths), ['/test']);
Expand Down
9 changes: 5 additions & 4 deletions test/router/root_api_spec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
definitions:
some_object:
description: 'bla bla bla'
components:
schemas:
some_object:
description: 'bla bla bla'
paths:
/test:
get:
x-request-hander:
- return_result:
return:
status: 200
status: 200

0 comments on commit ed51b6b

Please sign in to comment.