You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The generated code uses Go's standard net/url library for Query parameters encoding. The generated code contains the following line:
queryURL.RawQuery=queryValues.Encode()
Which calls to the func (v url.Values) Encode() string function. This function defined in the Go's standard net/url library isn't fully RFC3986-compliant. RFC3986 states that the space character ' ' (U+0020) has to be encoded as '%20' sequence, while net/url encodes spaces as '+' character. There's an encoding which uses '+' for encoding spaces, but it is a non-standard modification used by browsers to send HTML forms (See: Wikipedia).
While this encoding might be treated as valid by some service providers, there are service providers which expect full compliance with RFC3986 and refuse to serve such requests responding with status 400 Bad Request.
For instance, some OData servers use queries such as ?filter=name eq 'some-name' (encoded as ?filter=name%20eq%20'some-name') and mark ?filter=name+eq+'some-name' as an invalid query.
Our own backend will decode %20 and + just fine, so this is a client side encoding issue.
It seems there is a choice to be made here by the user, since if we assume one way vs another, we could be incorrect for someone.
I'm thinking that the runtime might need a configurable encoder, where we provide a default one, but you can override it with your own. I guess we could provide two out of the box, the net encoder and and RFC3986 encoder (which just replaces spaces with %20 in net/url encoded output.
I will need to release a new version, v1.6.0, since we're adding new API methods. Once that is released, you will be able to globally customize the encoder.
The change to make a per-request encoder is very invasive, so I'm hoping this will be sufficient.
Before runtime has a release, you can just pin to a SHA. I will wait for a release until I ship oapi-codegen v.2.8.0, since I might want to piggy-back more changes in there.
Hi! Thanks for turnaround on the encoder configuration, but I'd push back on keeping + as the default one. I think framing that neither encoding is universally correct isn't right.
OpenAPI query parameters default to style: form, which is defined by RFC6570 URI Template. This RFC expansion encodes a space character as %20, and has no '+'-encoding at all. The '+'-encoding is application/x-www-form-urlencoded, which is associated with request bodies by OpenAPI, not the url query component.
Regarding compatibility: the risk between two options isn't symetrical. %20 decodes to a space character under both RFC3986 parsers and form-urlencoded parsers. The WHATWG form-urlencoded algorithm converts both + and %20 to a space character, while + only decodes to a space under a form-urlencoded parser and is rejected or mis-decoded by RFC3986-strict parsers. So %20 is a strict superset. Every server that accepts '+'-encoding also accepts %20 as a space, but not the other way around.
Given that, would you consider flipping the default to RFC3986QueryEncoder and keeping NetURLEncoder as the opt-in for anyone who depends on the old behaviour?
I'm ok with flipping that behavior. I need to read more about it. You make a solid point. Since our backends will parse both encodings correctly, it doesn't make much difference to our own code, I was just being super conservative not to change default behavior of the client for people who are using it to talk to other backends.
The encoder lives in the runtime library, so this is being tracked there now. We'll flip the default to RFC3986QueryEncoder (space encoded as %20) in runtime v1.10.0: oapi-codegen/runtime#189.
Until then, you can opt in with runtime.DefaultQueryEncoder = runtime.RFC3986QueryEncoder{} at program initialization. After the flip, runtime.NetURLQueryEncoder{} will remain available for anyone who needs the old + behavior.
Activity
mromaszewicz commentedon Jul 15, 2026
Hmm, interesting.
Our own backend will decode
%20and+just fine, so this is a client side encoding issue.It seems there is a choice to be made here by the user, since if we assume one way vs another, we could be incorrect for someone.
I'm thinking that the runtime might need a configurable encoder, where we provide a default one, but you can override it with your own. I guess we could provide two out of the box, the
netencoder and and RFC3986 encoder (which just replaces spaces with%20innet/urlencoded output.Allow customizing default query encoder
mromaszewicz commentedon Jul 15, 2026
This is going to be a purely runtime fix.
I will need to release a new version, v1.6.0, since we're adding new API methods. Once that is released, you will be able to globally customize the encoder.
The change to make a per-request encoder is very invasive, so I'm hoping this will be sufficient.
Before runtime has a release, you can just pin to a SHA. I will wait for a release until I ship oapi-codegen v.2.8.0, since I might want to piggy-back more changes in there.
Allow customizing default query encoder (#145)
alienvspredator commentedon Aug 5, 2026
Hi! Thanks for turnaround on the encoder configuration, but I'd push back on keeping
+as the default one. I think framing that neither encoding is universally correct isn't right.OpenAPI query parameters default to
style: form, which is defined by RFC6570 URI Template. This RFC expansion encodes a space character as%20, and has no '+'-encoding at all. The '+'-encoding isapplication/x-www-form-urlencoded, which is associated with request bodies by OpenAPI, not the url query component.Regarding compatibility: the risk between two options isn't symetrical.
%20decodes to a space character under both RFC3986 parsers andform-urlencodedparsers. The WHATWGform-urlencodedalgorithm converts both+and%20to a space character, while+only decodes to a space under aform-urlencodedparser and is rejected or mis-decoded by RFC3986-strict parsers. So%20is a strict superset. Every server that accepts '+'-encoding also accepts%20as a space, but not the other way around.Given that, would you consider flipping the default to
RFC3986QueryEncoderand keepingNetURLEncoderas the opt-in for anyone who depends on the old behaviour?mromaszewicz commentedon Aug 5, 2026
I'm ok with flipping that behavior. I need to read more about it. You make a solid point. Since our backends will parse both encodings correctly, it doesn't make much difference to our own code, I was just being super conservative not to change default behavior of the client for people who are using it to talk to other backends.
mromaszewicz commentedon Nov 20, 2026
The encoder lives in the runtime library, so this is being tracked there now. We'll flip the default to
RFC3986QueryEncoder(space encoded as%20) in runtime v1.10.0: oapi-codegen/runtime#189.Until then, you can opt in with
runtime.DefaultQueryEncoder = runtime.RFC3986QueryEncoder{}at program initialization. After the flip,runtime.NetURLQueryEncoder{}will remain available for anyone who needs the old+behavior.