oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Path parameters are not URL decoded

Open rtfpessoa opened this issue 4 years ago • 5 comments

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.

rtfpessoa avatar Dec 13 '20 00:12 rtfpessoa

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.

reinkrul avatar Mar 25 '21 08:03 reinkrul

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.

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 avatar Mar 25 '21 09:03 reinkrul

@reinkrul thanks for the middleware.

rtfpessoa avatar Jul 13 '21 08:07 rtfpessoa

Some things have changed since the last two years. If you still use the above middleware, you'll double decode. Should be closed.

woutslakhorst avatar Dec 19 '23 12:12 woutslakhorst

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)
	}
}

Zen3515 avatar Mar 04 '24 14:03 Zen3515