open-api icon indicating copy to clipboard operation
open-api copied to clipboard

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

Open Frizzled opened this issue 7 years ago • 10 comments

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}
    });

Frizzled avatar Jun 26 '18 19:06 Frizzled

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?

jsdevel avatar Jun 26 '18 19:06 jsdevel

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!

Frizzled avatar Jun 26 '18 20:06 Frizzled

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

jsdevel avatar Jun 26 '18 20:06 jsdevel

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

jsdevel avatar Jun 26 '18 20:06 jsdevel

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

brianwestphal avatar Jan 30 '19 22:01 brianwestphal

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?

jsdevel avatar Jan 30 '19 23:01 jsdevel

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.

jlcanovasgrupobme avatar Apr 26 '19 09:04 jlcanovasgrupobme

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.

SkyaTura avatar Jan 25 '20 20:01 SkyaTura

@SkyaTura feel free to submit a PR.

jsdevel avatar Feb 07 '20 00:02 jsdevel

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

ei2kpi-ptc avatar Nov 24 '20 19:11 ei2kpi-ptc