Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion oapi_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ func ValidateRequestFromContext(ctx echo.Context, router routers.Router, options
clone := req.Clone(req.Context())
clone.URL.Path = strings.TrimPrefix(clone.URL.Path, options.Prefix)
req = clone
defer func() {
ctx.Request().Body = clone.Body
ctx.Request().GetBody = clone.GetBody
}()
}

route, pathParams, err := router.FindRoute(req)

// We failed to find a matching route for the request.
if err != nil {
if errors.Is(err, routers.ErrMethodNotAllowed) {
Expand Down
26 changes: 24 additions & 2 deletions oapi_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ func TestOapiRequestValidator(t *testing.T) {
e.GET("/protected_resource", func(c echo.Context) error {
called = true
return c.NoContent(http.StatusNoContent)

})

// Call a protected function to which we have access
Expand Down Expand Up @@ -453,13 +452,27 @@ func TestOapiRequestValidatorWithPrefix(t *testing.T) {

called := false

type testBody struct {
Msg string
}

// Register handler under the prefixed path (as echo sees it)
e.GET("/api/resource", func(c echo.Context) error {
called = true
// The original request path should be preserved for the handler
assert.Equal(t, "/api/resource", c.Request().URL.Path)
return c.NoContent(http.StatusOK)
})
e.POST("/api/resource", func(c echo.Context) error {
called = true
// The original request path should be preserved for the handler
assert.Equal(t, "/api/resource", c.Request().URL.Path)
req := c.Request()
data, err := io.ReadAll(req.Body)
require.NoError(t, err, "Reading POST body")
assert.Equal(t, "{\"Msg\":\"testbody\"}", string(data))
return c.NoContent(http.StatusOK)
})

// A request to /api/resource should validate against /resource in the spec
{
Expand All @@ -469,6 +482,16 @@ func TestOapiRequestValidatorWithPrefix(t *testing.T) {
called = false
}

// A POST request to /api/resource should validate against /resource in the spec and have a specific body
{
rec := doPost(t, e, "http://test.hostname/api/resource", testBody{
Msg: "testbody",
})
assert.Equal(t, http.StatusOK, rec.Code)
assert.True(t, called, "Handler should have been called")
called = false
}

// A request with query params should also validate correctly
{
rec := doGet(t, e, "http://test.hostname/api/resource?id=50")
Expand Down Expand Up @@ -566,7 +589,6 @@ func TestEncodedPathParams(t *testing.T) {
}

func TestGetSkipperFromOptions(t *testing.T) {

options := new(Options)
assert.NotNil(t, getSkipperFromOptions(options))

Expand Down