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

Conversation

sgermain06
Copy link

Added a way to allow handlers to call specific methods, defined by operatrionId. Also added a unified way to define a base path for handlers, allowing the x-hapi-handler record to simply call the path to a file without needing to change directories back to the previous one or forcing the handler files to be in the same directory as the OpenAPI definition file.

const init = async () => {
    await server.register({
        plugin: require('hapi-openapi'),
        options: {
            api: './api/swagger/swagger.yaml',
            handlersPath: './api/controllers', // <-- This is where the plugin should look to resolve controllers defined in x-hapi-handler
        }
    });
    await server.start();
    console.log(server.info.uri);
};
{
    ...
    "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"
                        }
                    }
                }
            }
        },
        ...
    }
}

This should call the getFirstPet function in the file that's located in <__dirname>/api/controllers/pets.js

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

@@ -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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants