-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(#1726): add literal colon support for gin, echo, fiber #1879
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
22c8c07
add literal colon support for gin and echo
cosban b045ccb
backslashes should themselves be escaped too
cosban a58b695
and of course, the tests need to be updated to account for the backsl…
cosban 94a0f72
Merge branch 'main' into literal-colon-support
cosban 91c4729
Merge remote-tracking branch 'upstream/main' into literal-colon-support
mromaszewicz 3053bdb
Complete literal-colon path support for gin, echo, and fiber
mromaszewicz 9d8b5b9
Update greptile directives
mromaszewicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package paths | ||
|
|
||
| import ( | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| fiberv2 "github.com/gofiber/fiber/v2" | ||
| fiberv3 "github.com/gofiber/fiber/v3" | ||
| "github.com/labstack/echo/v4" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| colonecho "github.com/oapi-codegen/oapi-codegen/v2/internal/test/paths/echo" | ||
| colonfiber "github.com/oapi-codegen/oapi-codegen/v2/internal/test/paths/fiber" | ||
| colonfiberv3 "github.com/oapi-codegen/oapi-codegen/v2/internal/test/paths/fiberv3" | ||
| colongin "github.com/oapi-codegen/oapi-codegen/v2/internal/test/paths/gin" | ||
| ) | ||
|
|
||
| // Each handler writes its own operation name so the test can prove that | ||
| // /pets:validate and /pets:generate dispatch to distinct handlers rather than | ||
| // colliding on a single ":"-parameter route (issue #1726). | ||
|
|
||
| type echoServer struct{} | ||
|
|
||
| func (echoServer) ValidatePets(ctx echo.Context) error { return ctx.String(http.StatusOK, "validate") } | ||
| func (echoServer) GeneratePets(ctx echo.Context) error { return ctx.String(http.StatusOK, "generate") } | ||
|
|
||
| type ginServer struct{} | ||
|
|
||
| func (ginServer) ValidatePets(c *gin.Context) { c.String(http.StatusOK, "validate") } | ||
| func (ginServer) GeneratePets(c *gin.Context) { c.String(http.StatusOK, "generate") } | ||
|
|
||
| type fiberServer struct{} | ||
|
|
||
| func (fiberServer) ValidatePets(c *fiberv2.Ctx) error { return c.SendString("validate") } | ||
| func (fiberServer) GeneratePets(c *fiberv2.Ctx) error { return c.SendString("generate") } | ||
|
|
||
| type fiberv3Server struct{} | ||
|
|
||
| func (fiberv3Server) ValidatePets(c fiberv3.Ctx) error { return c.SendString("validate") } | ||
| func (fiberv3Server) GeneratePets(c fiberv3.Ctx) error { return c.SendString("generate") } | ||
|
|
||
| // wantBody maps each colon path to the body its dedicated handler returns. | ||
| var wantBody = map[string]string{ | ||
| "/pets:validate": "validate", | ||
| "/pets:generate": "generate", | ||
| } | ||
|
|
||
| // assertServeHTTP drives an http.Handler-compatible router (echo, gin). | ||
| func assertServeHTTP(t *testing.T, name string, h http.Handler) { | ||
| t.Helper() | ||
| for path, want := range wantBody { | ||
| rec := httptest.NewRecorder() | ||
| req := httptest.NewRequest(http.MethodPost, path, nil) | ||
| h.ServeHTTP(rec, req) | ||
| assert.Equalf(t, http.StatusOK, rec.Code, "%s POST %s status", name, path) | ||
| assert.Equalf(t, want, rec.Body.String(), "%s POST %s dispatched to wrong handler", name, path) | ||
| } | ||
| } | ||
|
|
||
| func TestEchoColonPathsRouteIndependently(t *testing.T) { | ||
| e := echo.New() | ||
| colonecho.RegisterHandlers(e, echoServer{}) | ||
| assertServeHTTP(t, "echo", e) | ||
| } | ||
|
|
||
| func TestGinColonPathsRouteIndependently(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| g := gin.New() | ||
| colongin.RegisterHandlers(g, ginServer{}) | ||
| assertServeHTTP(t, "gin", g) | ||
| } | ||
|
|
||
| func TestFiberColonPathsRouteIndependently(t *testing.T) { | ||
| app := fiberv2.New() | ||
| colonfiber.RegisterHandlers(app, fiberServer{}) | ||
| for path, want := range wantBody { | ||
| req := httptest.NewRequest(http.MethodPost, path, nil) | ||
| resp, err := app.Test(req) | ||
| require.NoErrorf(t, err, "fiber POST %s", path) | ||
| assert.Equalf(t, http.StatusOK, resp.StatusCode, "fiber POST %s status", path) | ||
| body, _ := io.ReadAll(resp.Body) | ||
| assert.Equalf(t, want, string(body), "fiber POST %s dispatched to wrong handler", path) | ||
| } | ||
| } | ||
|
|
||
| func TestFiberV3ColonPathsRouteIndependently(t *testing.T) { | ||
| app := fiberv3.New() | ||
| colonfiberv3.RegisterHandlers(app, fiberv3Server{}) | ||
| for path, want := range wantBody { | ||
| req := httptest.NewRequest(http.MethodPost, path, nil) | ||
| resp, err := app.Test(req) | ||
| require.NoErrorf(t, err, "fiberv3 POST %s", path) | ||
| assert.Equalf(t, http.StatusOK, resp.StatusCode, "fiberv3 POST %s status", path) | ||
| body, _ := io.ReadAll(resp.Body) | ||
| assert.Equalf(t, want, string(body), "fiberv3 POST %s dispatched to wrong handler", path) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Package paths verifies path-handling edge cases in generated servers. It | ||
| // currently checks that literal-colon path segments route independently on the | ||
| // ':'-parameter routers (Echo, Gin, Fiber v2/v3) rather than colliding as a | ||
| // single path parameter. See issue #1726. | ||
| package paths | ||
|
|
||
| //go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=echo/config.yaml spec.yaml | ||
| //go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=gin/config.yaml spec.yaml | ||
| //go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=fiber/config.yaml spec.yaml | ||
| //go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=fiberv3/config.yaml spec.yaml | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # yaml-language-server: $schema=../../../../../configuration-schema.json | ||
| package: colonpathsecho | ||
| generate: | ||
| echo-server: true | ||
| models: true | ||
| output: echo/colon.gen.go |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # yaml-language-server: $schema=../../../../../configuration-schema.json | ||
| package: colonpathsfiber | ||
| generate: | ||
| fiber-server: true | ||
| models: true | ||
| output: fiber/colon.gen.go |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.