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

Enhancement: Simple swagger validation HTTP middleware #53

Open
igorsechyn opened this issue Jul 3, 2017 · 0 comments
Open

Enhancement: Simple swagger validation HTTP middleware #53

igorsechyn opened this issue Jul 3, 2017 · 0 comments

Comments

@igorsechyn
Copy link

As mentioned in #44 I would like to submit a PR to make it easy to create a simple validation middleware for HTTP requests, which can be used with any mux library.

Before I start with the implementation, I wanted to share some ideas and run them past you, to make sure I am moving into the right direction.

Current State (simplified)

  • Context requires a RoutableAPI object to find an appropriate handler for each request.
  • At the moment it uses an internal routableUntypedAPI, which wraps around untyped.API and calls runtime.OperationHandler for each request after validation.
  • It also uses other functions of untyped.API for content negotiation, etc.
  • OperationHandler accepts body parameter, read from request, and returns a response object, which is then written into http response

With this implementation there is currently no easy way to bypass OperationHandlers, i.e. consumers of the library are forced to use untyped.API to specify handlers for each path and method.

Implementation suggestion

  • create a new method in Context NewRoutableProxyContext.
  • reuse untyped.API to allow to specify consumers, producers, authenticators
    • question, why can't consumers and producers be read from swagger spec?
  • reuse internal routableUntypedAPI struct, but add a new factory function newRoutableProxyAPI, which will create http handlers for each operation/method, but instead of calling OperationHandler, they will call a provided proxy http.Handler, if validation succeeds

Caveats (at least the ones I thought about)

  • in order to validate request body, it has to be read, which makes it unavailable in subsequent http handlers. One possible way to solve it, is to copy it and reset on the request when passing it to the proxy handler. Depending on the size of the body, this could consume too much memory.
  • response validation is no longer possible
  • any others ????

As an alternative I could add a completely new implementation of RoutableAPI without any references to untyped.API. I am just not quite sure, if it is still required in other modules.

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

No branches or pull requests

2 participants