Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upMiddleware Use behavior undocumented and unintuitive #2010
Comments
|
@sguillia I just confirmed this behavior with |
|
Same for group.Use(...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please take a look at the following code
And now please take a look at the documentation for Use() (emphasis mine):
Who in the world would have guessed that the middlewares are only called with the routes that where declared after them, not before?
Steps to reproduce:
go run main.gowith main.go being the code abovecurl localhost:8080/pingxand watch as the logger logs requests on stdoutcurl localhost:8080/pingand watch as nothing is logged anywhere.To me this is a bug but I have a feeling that this is intended behavior...
If it is intended, I would suggest to update the documentation, but this is not enough.
I would also change the README because this behavior is documented nowhere.
Also I suggest renaming
.Useto.UseFromNowOnor to have both calls available.This bug prevents from using middlewares with OpenAPI
go-gin-servergenerator . OpenAPI generator is a tool that writes API routes (among others). By nature, a lot of users will add the CORS middleware, and will:Also, this thread shows users struggling with the bug. It has been around for years
gin-contrib/cors#29
Thank you for your understanding
Note. For some people, it might look intuitive in the code above that
UsemeansUseFromNowOn. But I happened to work on this, whereNewRouteris generated code. It is way less explicit.