Allow customizing default query encoder#145
Merged
Merged
Conversation
Addresses oapi-codegen/oapi-codegen#2051 Go's net/url encodes a space in query strings as '+' (the form-urlencoded convention), which is not RFC 3986 compliant. Some servers (e.g. OData endpoints expecting ?filter=name%20eq%20'x') reject '+'-encoded spaces with 400 Bad Request, while others require '+'. Since neither encoding is universally correct, this makes the behavior configurable rather than changing the default. Introduce a QueryEncoder interface (a single EscapeQueryValue method) with two built-in implementations in a new encoder.go: NetURLQueryEncoder (the default, '+' for space, preserving current behavior) and RFC3986QueryEncoder ('%20' for space). A package-level DefaultQueryEncoder (mirroring http.DefaultClient) lets callers opt in during program initialization. Names and map keys are escaped as values with allowReserved=false, which allowReserved never applies to. All query escaping in styleparam.go and deepobject.go now routes through DefaultQueryEncoder, so both styled and deepObject query parameters honor the choice. Path escaping and x-www-form-urlencoded request bodies are left unchanged, where '+' for a space is the correct, media-type-defined behavior. Documented under the README "Encoding" section; tests in encoder_test.go. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
37c4761 to
334a9ea
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses oapi-codegen/oapi-codegen#2051
Go's net/url encodes a space in query strings as '+' (the form-urlencoded convention), which is not RFC 3986 compliant. Some servers (e.g. OData endpoints expecting ?filter=name%20eq%20'x') reject '+'-encoded spaces with 400 Bad Request, while others require '+'. Since neither encoding is universally correct, this makes the behavior configurable rather than changing the default.
Introduce a QueryEncoder interface with two built-in implementations in a new encoder.go: NetURLQueryEncoder (the default, '+' for space, preserving current behavior) and RFC3986QueryEncoder ('%20' for space). A package-level DefaultQueryEncoder (mirroring http.DefaultClient) lets callers opt in during program initialization.
All query escaping in styleparam.go and deepobject.go now routes through DefaultQueryEncoder, so both styled and deepObject query parameters honor the choice. Path escaping and x-www-form-urlencoded request bodies are left unchanged, where '+' for a space is the correct, media-type-defined behavior.
Documented under the README "Encoding" section; tests in encoder_test.go.