open-api
open-api copied to clipboard
x-express-openapi-additional-middleware when not using JS APIDoc?
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}
});
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?
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!
You're welcome! I should be able to add support for this soon
In the mean time, feel free to open a PR.
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
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?
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.
right now it's only supported in
.jsfiles. I suppose we could addargs.middlewarethat you could provide key/vals to where the key is a string used in thex-express-openapi-additional-middlewarearray and the val is the middleware function. Would that work?
Is this still on roadmap? Is highly needed on my use case.
@SkyaTura feel free to submit a PR.
Hey! Any idea if the args.middleware was added to allow x-express-openapi-additional-middleware in YAML files to trigger a middleware function?