oapi-codegen
oapi-codegen copied to clipboard
Path parameters are not URL decoded
I am defining a path parameter that is a string with format date-time. This means that if I invoke the endpoint properly my request library will encode path parameters and in the case of the date it will encode the :.
This means that date 2020-12-12T23:51:17Z will become 2020-12-12T23%3A51%3A17Z.
Currently this is being passed to the code as the encoded version which seems weird since http servers usually deal with this things for you.
Also if I use github.com/getkin/kin-openapi/openapi3filter it fails to validate the value since it containing the encoded parts makes it an invalid date-time.
Any ideas if this is a bug? Or am I doing something wrong in my side? Also, if this needs a fix I can help if you give me some pointers.
We have the same problem, although we can work around it by unescaping ourselves (since we don't use openapi3filter).
Edit: I just looked into this and although you could fix it in deepmap/oapi-codegen it is Echo that interprets and passes the path parameters, so it should be fixed there in my opinion.
I am defining a path parameter that is a string with format date-time. This means that if I invoke the endpoint properly my request library will encode path parameters and in the case of the date it will encode the
:.This means that date
2020-12-12T23:51:17Zwill become2020-12-12T23%3A51%3A17Z.Currently this is being passed to the code as the encoded version which seems weird since http servers usually deal with this things for you. Also if I use
github.com/getkin/kin-openapi/openapi3filterit fails to validate the value since it containing the encoded parts makes it an invalid date-time.Any ideas if this is a bug? Or am I doing something wrong in my side? Also, if this needs a fix I can help if you give me some pointers.
You can use this middleware to decode URL path parameters (taken from https://github.com/nuts-foundation/nuts-node/blob/master/core/engine.go);
// DecodeURIPath is a echo middleware that decodes path parameters
func DecodeURIPath(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
newValues := make([]string, len(c.ParamValues()))
for i, value := range c.ParamValues() {
path, err := url.PathUnescape(value)
if err != nil {
path = value
}
newValues[i] = path
}
c.SetParamNames(c.ParamNames()...)
c.SetParamValues(newValues...)
return next(c)
}
}
@reinkrul thanks for the middleware.
Some things have changed since the last two years. If you still use the above middleware, you'll double decode. Should be closed.
In my case, query parameters are not decoded when type is string, so I add this workaround.
But it could be fixed here
// middleware that decodes query parameters
func DecodeQueryParams(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
queryParams := c.QueryParams()
for queryKey, queryValue := range queryParams {
for _, value := range queryValue {
decodedValue, err := url.QueryUnescape(value)
if err != nil {
decodedValue = value
}
c.QueryParams().Set(queryKey, decodedValue)
}
}
return next(c)
}
}