swagger-tools
swagger-tools copied to clipboard
Throw error on request to path that's not defined in Swagger doc?
Hey swagger-tools team, thanks for this project!
So I'm using Swagger to develop docs for an existing API, and I'm testing against a Node server that's running swagger-tools. I need my tests to fail if the endpoint isn't defined in my swagger.yaml (so I can make sure I've documented every endpoint) but it seems that swagger-metadata silently ignores undefined endpoints/operations:
(source: swagger-metadata.js:406)
debug(' Is a Swagger path: %s', !_.isUndefined(cacheEntry));
// Request does not match an API defined in the Swagger document(s)
if (!cacheEntry) {
return next();
}
I'm currently solving this problem by simply editing line 410 (return next();) to instead
throw new Error('Request does not match an API defined in the Swagger document(s)');
But is there a canonical way to get the behavior I'm looking for ― some kind of "strict mode"? Or plans to support one?
Thanks!
The idea behind this was that if the requested path is not one defined in your Swagger document, it's a no-op as far as this middleware is concerned. I like the idea of a strict mode and it's been suggested for parameters but not for paths. I think it's a cool idea.
@whitlockjc do we have any update on this issue on how can I throw an error message defined instead of silently ignoring it?
Throw in some middleware after the swagger-tools middleware that will treat anything it gets as an error.