Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for operationId and handlers path #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const optionsSchema = Joi.object({
cors: Joi.alternatives().try(Joi.object(), Joi.boolean()).default(true),
vhost: Joi.string().allow(null),
handlers: Joi.alternatives().try(Joi.string().default(Path.join(CALLER_DIR, 'routes')), Joi.object()).allow(null),
handlersPath: Joi.string().allow(null),
extensions: Joi.array().items(Joi.string()).default(['js']),
outputvalidation: Joi.boolean().default(false)
}).required();
Expand Down Expand Up @@ -78,7 +79,7 @@ const register = async function (server, options, next) {

Hoek.assert(!validation.error, validation.error);

const { api, cors, vhost, handlers, extensions, outputvalidation } = validation.value;
const { api, cors, vhost, handlers, handlersPath, extensions, outputvalidation } = validation.value;
let { docs, docspath } = validation.value;
const spec = await Parser.validate(api);

Expand All @@ -104,7 +105,7 @@ const register = async function (server, options, next) {

if (typeof api === 'string') {
apiDocument = requireApi(api);
basedir = Path.dirname(Path.resolve(api));
basedir = handlersPath ? Path.resolve(handlersPath) : Path.dirname(Path.resolve(api));
}
else {
apiDocument = api;
Expand Down
15 changes: 13 additions & 2 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,21 @@ const create = async function (server, { api, basedir, cors, vhost, handlers, ex

for (const [path, operations] of Object.entries(api.paths)) {
const pathnames = Utils.unsuffix(path, '/').split('/').slice(1).join(SEPARATOR);
const hapiHandler = operations['x-hapi-handler'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the handler in case of x-hapi-handler is already set few lines before, this is probably a bug as we probably wanted precedence of x-hapi-handler over a path but now we do it twice.


for (const [method, operation] of Object.entries(operations)) {
const pathsearch = `${pathnames}${SEPARATOR}${method}`;
const handler = Hoek.reach(handlers, pathsearch, { separator: SEPARATOR });
const operationId = operation.operationId;
const hapiHandlerMethod = `${hapiHandler}${SEPARATOR}${method}`;
const hapiHandlerOperationId = `${hapiHandler}${SEPARATOR}${operationId}`;
const pathsearchMethod = `${pathnames}${SEPARATOR}${method}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pathsearchMethod => pathSearchMethod
maybe pathSearchWithMethod

const pathSearchOperationId = `${pathnames}${SEPARATOR}${operationId}`;
// Give precedence to operationId over method and give precedence to hapiHandler over path.
const handler =
Hoek.reach(handlers, hapiHandlerOperationId, { separator: SEPARATOR }) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we are searching for `xxx/Undefined; most of the time and do a disk access

Hoek.reach(handlers, pathSearchOperationId, { separator: SEPARATOR }) ||
Hoek.reach(handlers, hapiHandlerMethod, { separator: SEPARATOR }) ||
Hoek.reach(handlers, pathsearchMethod, { separator: SEPARATOR });

const xoptions = operation['x-hapi-options'] || {};

if (!handler) {
Expand Down
258 changes: 258 additions & 0 deletions test/fixtures/defs/pets_xhandlers_handlersPath.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore (Simple)",
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
"termsOfService": "http://helloreverb.com/terms/",
"contact": {
"name": "Swagger API team",
"email": "[email protected]",
"url": "http://swagger.io"
},
"license": {
"name": "MIT",
"url": "http://opensource.org/licenses/MIT"
}
},
"host": "petstore.swagger.io",
"basePath": "/v1/petstore",
"schemes": [
"http"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/firstPet": {
"x-hapi-handler": "pets",
"get": {
"description": "Returns all pets from the system that the user has access to",
"operationId": "getFirstPet",
"produces": [
"application/json",
"application/xml",
"text/xml",
"text/html"
],
"responses": {
"200": {
"description": "pet response",
"schema": {
"$ref": "#/definitions/pet"
}
},
"default": {
"description": "unexpected error",
"schema": {
"$ref": "#/definitions/errorModel"
}
}
}
}
},
"/pets": {
"x-hapi-handler": "pets",
"get": {
"description": "Returns all pets from the system that the user has access to",
"operationId": "findPets",
"produces": [
"application/json",
"application/xml",
"text/xml",
"text/html"
],
"parameters": [
{
"name": "tags",
"in": "query",
"description": "tags to filter by",
"required": false,
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi"
},
{
"name": "limit",
"in": "query",
"description": "maximum number of results to return",
"required": false,
"type": "integer",
"format": "int32"
}
],
"responses": {
"200": {
"description": "pet response",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/pet"
}
}
},
"default": {
"description": "unexpected error",
"schema": {
"$ref": "#/definitions/errorModel"
}
}
}
},
"post": {
"description": "Creates a new pet in the store. Duplicates are allowed",
"operationId": "addPet",
"produces": [
"application/json"
],
"parameters": [
{
"name": "pet",
"in": "body",
"description": "Pet to add to the store",
"required": true,
"schema": {
"$ref": "#/definitions/newPet"
}
}
],
"responses": {
"200": {
"description": "pet response",
"schema": {
"$ref": "#/definitions/pet"
}
},
"default": {
"description": "unexpected error",
"schema": {
"$ref": "#/definitions/errorModel"
}
}
}
}
},
"/pets/{id}": {
"x-hapi-handler": "pets",
"get": {
"description": "Returns a user based on a single ID, if the user does not have access to the pet",
"operationId": "findPetById",
"produces": [
"application/json",
"application/xml",
"text/xml",
"text/html"
],
"parameters": [
{
"name": "id",
"in": "path",
"description": "ID of pet to fetch",
"required": true,
"type": "integer",
"format": "int64"
}
],
"responses": {
"200": {
"description": "pet response",
"schema": {
"$ref": "#/definitions/pet"
}
},
"default": {
"description": "unexpected error",
"schema": {
"$ref": "#/definitions/errorModel"
}
}
}
},
"delete": {
"description": "deletes a single pet based on the ID supplied",
"operationId": "deletePet",
"parameters": [
{
"name": "id",
"in": "path",
"description": "ID of pet to delete",
"required": true,
"type": "integer",
"format": "int64"
}
],
"responses": {
"204": {
"description": "pet deleted"
},
"default": {
"description": "unexpected error",
"schema": {
"$ref": "#/definitions/errorModel"
}
}
}
}
}
},
"definitions": {
"pet": {
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"newPet": {
"type": "object",
"required": [
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"errorModel": {
"type": "object",
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}
15 changes: 15 additions & 0 deletions test/fixtures/handlers/pets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,20 @@ module.exports = {
},
post: function (req, h) {
return Store.get(Store.put(req.payload));
},
getFirstPet: function (req, h) {
return Store.first();
},
findPetById: [
function (req, h) {
return Store.get(req.params.id);
},
function handler(req, h) {
return req.pre.p1;
}
],
delete: function (req, h) {
Store.delete(req.params.id);
return Store.all();
}
};
5 changes: 4 additions & 1 deletion test/fixtures/lib/store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var store = [];
const store = [];

module.exports = {
put: function (data) {
Expand All @@ -15,5 +15,8 @@ module.exports = {
},
all: function () {
return store;
},
first: function () {
return {};
}
};
Loading