Skip to content

Query encoding isn't RFC3986 compliant #2051

Closed

Description

@alienvspredator

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.

References:
golang/go#4013
RFC3986
application/x-www-form-urlencoded MIME

Activity

  1. mromaszewicz commented on Jul 15, 2026

    @mromaszewicz
    Member

    Hmm, interesting.

    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.

  2. mromaszewicz commented on Jul 15, 2026

    @mromaszewicz
    Member

    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.

  3. self-assigned this
    on Jul 15, 2026
  4. added this to the v2.8.0 milestone on Jul 15, 2026
  5. alienvspredator commented on Aug 5, 2026

    @alienvspredator
    Author

    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?

  6. mromaszewicz commented on Aug 5, 2026

    @mromaszewicz
    Member

    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.

  7. mromaszewicz commented on Nov 20, 2026

    @mromaszewicz
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions