kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node
MIT License
894 stars 237 forks source link

x-express-openapi-additional-middleware when not using JS APIDoc? #126

Open Frizzled opened 6 years ago

Frizzled commented 6 years ago

Is it possible to inject middleware that fires on all routes without using a JavaScript APIDoc?

I want to validate & load data based on the route provided (after req.params are initialized but before any services) while keeping the APIDoc in JSON or YAML for documentation generation.

As a work-around I'm keeping the API Doc separate then adding x-express-openapi-additional-middleware:

const fs = require('fs');
const path = require('path');
const validate = require('../middleware/validate');

let apiDoc = JSON.parse(fs.readFileSync(path.resolve(__dirname, './api-doc.json'), 'utf8'));
apiDoc['x-express-openapi-additional-middleware'] = [validate];

module.exports = apiDoc;

Is there an initialize setting I'm missing?

e.g.

    openapi.initialize({
        apiDoc: fs.readFileSync(path.resolve(__dirname, './api-doc.json'), 'utf8'),
        app: app,
        paths: path.resolve(__dirname, './routes'),
        middleware: {validate: validate}
    });
jsdevel commented 6 years ago

right now it's only supported in .js files. I suppose we could add args.middleware that you could provide key/vals to where the key is a string used in the x-express-openapi-additional-middleware array and the val is the middleware function. Would that work?

Frizzled commented 6 years ago

That sounds great -

Having to merge logic into JSON or YAML isn't a deal-breaker. I'm very happy with how fully Swagger 2.0 is supported; thanks for all the hard work on this project!

jsdevel commented 6 years ago

You're welcome! I should be able to add support for this soon

jsdevel commented 6 years ago

In the mean time, feel free to open a PR.

brianwestphal commented 5 years ago

Also, the documentation on https://www.npmjs.com/package/express-openapi shows setting 'x-express-openapi-additional-middleware' during initialize, which doesn't seem to work. I.e.

initialize({
    app: app,
    paths: path.resolve(__dirname, 'api-paths'),
    'x-express-openapi-additional-middleware': [validateAllResponses],
    'x-express-openapi-validation-strict': true,
    apiDoc: apiDoc
});

one has to set it on apiDoc, as in the above issue

jsdevel commented 5 years ago

ahh, good catch @brianwestphal . Those properties should be on the apiDoc, not the args. Can you open a PR to correct that in the README?

jlcanovasgrupobme commented 5 years ago

My take on it is to parse the Yaml to JSON first and then merge:

const jsYaml = require('yaml-js');
let apiDoc = jsYaml.load(fs.readFileSync(path.resolve(__dirname, 'openapi.yaml'), 'utf8'));
openapi.initialize({
  apiDoc: {
    ...apiDoc,
    'x-express-openapi-additional-middleware': [validateAllResponses],
    'x-express-openapi-validation-strict': true
  },
[...]
}

Of course, this is just a workaround and I would prefer a new args property.

SkyaTura commented 4 years ago

right now it's only supported in .js files. I suppose we could add args.middleware that you could provide key/vals to where the key is a string used in the x-express-openapi-additional-middleware array and the val is the middleware function. Would that work?

Is this still on roadmap? Is highly needed on my use case.

jsdevel commented 4 years ago

@SkyaTura feel free to submit a PR.

ei2kpi-ptc commented 3 years ago

Hey! Any idea if the args.middleware was added to allow x-express-openapi-additional-middleware in YAML files to trigger a middleware function?