Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,28 @@ cases where round-tripping is not possible in principle.

##### Configuring query encoding

By default, spaces in query strings are encoded as `+`, following Go's
`net/url` (the form-urlencoded convention used by browsers). This is accepted
by most servers, but it is *not* RFC 3986 compliant — some servers (for
example, OData endpoints expecting `?filter=name%20eq%20'x'`) reject `+` for a
space with `400 Bad Request`.
By default, spaces in query strings are encoded as `%20`, per RFC 3986. Before
v1.10.0 the default was `+`, following Go's `net/url` (the form-urlencoded
convention used by browsers). That is accepted by most servers, but it is *not*
RFC 3986 compliant — some servers (for example, OData endpoints expecting
`?filter=name%20eq%20'x'`) reject `+` for a space with `400 Bad Request`,
whereas servers that accept `+` also accept `%20`.

Generated clients route all query escaping through the package-level
`DefaultQueryEncoder`, so you can control this behavior. Two encoders ship out
of the box:

- `NetURLQueryEncoder` — the default; encodes a space as `+`.
- `RFC3986QueryEncoder` — encodes a space as `%20` for strict RFC 3986
compliance.
- `RFC3986QueryEncoder` — the default; encodes a space as `%20` for strict
RFC 3986 compliance.
- `NetURLQueryEncoder` — encodes a space as `+`, the default before v1.10.0.

To opt into RFC 3986 encoding, set the default once during program
To restore the previous `+` encoding, set the default once during program
initialization (it is not safe to mutate concurrently with in-flight requests,
the same contract as `http.DefaultClient`):

```go
func init() {
runtime.DefaultQueryEncoder = runtime.RFC3986QueryEncoder{}
runtime.DefaultQueryEncoder = runtime.NetURLQueryEncoder{}
}
```

Expand All @@ -146,8 +147,7 @@ correct, media-type-defined behavior.
- **Query and path values are percent-encoded.** Reserved characters
(`&`, `=`, `#`, `?`, etc.) and non-ASCII bytes are escaped via
`url.QueryEscape` / `url.PathEscape`. Spaces in query values are encoded
as `+` (form-urlencoded convention), matching `url.Values.Encode()`,
unless `DefaultQueryEncoder` is changed (see above).
as `%20` (RFC 3986), unless `DefaultQueryEncoder` is changed (see above).
- **Header values are passed through raw.** Per RFC 7230 §3.2.6, header
field values may contain visible ASCII plus space/tab; bytes ≥ `0x80` are
`obs-text` and explicitly marked obsolete in RFC 9110. There is no
Expand Down
2 changes: 1 addition & 1 deletion deepobject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestDeepObject(t *testing.T) {
require.NoError(t, err)
// Spaces in values are encoded as '+' per url.QueryEscape (form-encoded
// convention, matching the pre-v2.7.0 behavior of url.Values.Encode()).
require.EqualValues(t, "p[ab][0]=true&p[ao][0][Foo]=bar&p[ao][0][Is]=true&p[ao][1][Foo]=baz&p[ao][1][Is]=false&p[aop][0][Foo]=a&p[aop][1][Foo]=b&p[aop][1][count]=2&p[as][0]=hello&p[as][1]=world&p[b]=true&p[d]=2020-02-01&p[f]=4.2&p[i]=12&p[m][additional]=1&p[o][ID]=456&p[o][Name]=Joe+Schmoe&p[oas][0]=foo&p[oas][1]=bar&p[ob]=true&p[od]=2020-02-01&p[of]=3.7&p[oi]=5&p[om][additional]=1&p[onas][names][0]=Bill&p[onas][names][1]=Frank&p[oo][ID]=123&p[oo][Name]=Marcin+Romaszewicz", marshaled)
require.EqualValues(t, "p[ab][0]=true&p[ao][0][Foo]=bar&p[ao][0][Is]=true&p[ao][1][Foo]=baz&p[ao][1][Is]=false&p[aop][0][Foo]=a&p[aop][1][Foo]=b&p[aop][1][count]=2&p[as][0]=hello&p[as][1]=world&p[b]=true&p[d]=2020-02-01&p[f]=4.2&p[i]=12&p[m][additional]=1&p[o][ID]=456&p[o][Name]=Joe%20Schmoe&p[oas][0]=foo&p[oas][1]=bar&p[ob]=true&p[od]=2020-02-01&p[of]=3.7&p[oi]=5&p[om][additional]=1&p[onas][names][0]=Bill&p[onas][names][1]=Frank&p[oo][ID]=123&p[oo][Name]=Marcin%20Romaszewicz", marshaled)

// Use url.ParseQuery for the round-trip — it decodes percent-escapes and
// '+' back to literal characters, matching what a real HTTP server does.
Expand Down
20 changes: 11 additions & 9 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ type QueryEncoder interface {

// NetURLQueryEncoder preserves Go's net/url behavior, encoding a space as '+'.
// This matches the application/x-www-form-urlencoded convention that browsers
// use for HTML forms. It is the default encoder, so existing generated clients
// are unaffected. Note that many, but not all, servers accept '+' as a space.
// use for HTML forms. It was the default encoder before v1.10.0. Note that
// many, but not all, servers accept '+' as a space; set
// runtime.DefaultQueryEncoder to a NetURLQueryEncoder to restore the old
// behavior.
type NetURLQueryEncoder struct{}

func (NetURLQueryEncoder) EscapeQueryValue(value string, allowReserved bool) string {
Expand All @@ -55,8 +57,8 @@ func (NetURLQueryEncoder) EscapeQueryValue(value string, allowReserved bool) str
// RFC3986QueryEncoder encodes a space as %20 rather than '+', for strict RFC
// 3986 compliance. Some servers (for example, OData endpoints that expect
// filters such as ?filter=name%20eq%20'x') reject '+'-encoded spaces with 400
// Bad Request. Set runtime.DefaultQueryEncoder to an RFC3986QueryEncoder to opt
// in.
// Bad Request, while servers that accept '+' as a space also accept %20. It is
// the default encoder.
//
// The +->%20 rewrite is lossless: url.QueryEscape encodes a literal '+' in the
// input as %2B, so any '+' remaining in its output can only be a space.
Expand All @@ -70,8 +72,8 @@ func (RFC3986QueryEncoder) EscapeQueryValue(value string, allowReserved bool) st
}

// DefaultQueryEncoder is the QueryEncoder used by all generated clients to
// escape query parameters. It defaults to NetURLQueryEncoder for backwards
// compatibility. Set it once during program initialization; like
// http.DefaultClient, it is not safe to mutate concurrently with in-flight
// requests.
var DefaultQueryEncoder QueryEncoder = NetURLQueryEncoder{}
// escape query parameters. It defaults to RFC3986QueryEncoder; before v1.10.0
// the default was NetURLQueryEncoder. Set it once during program
// initialization; like http.DefaultClient, it is not safe to mutate
// concurrently with in-flight requests.
var DefaultQueryEncoder QueryEncoder = RFC3986QueryEncoder{}
8 changes: 4 additions & 4 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (

func TestDefaultQueryEncoder(t *testing.T) {
// The default encoder must not have been changed by any other test.
assert.IsType(t, NetURLQueryEncoder{}, DefaultQueryEncoder,
"DefaultQueryEncoder should default to NetURLQueryEncoder")
assert.IsType(t, RFC3986QueryEncoder{}, DefaultQueryEncoder,
"DefaultQueryEncoder should default to RFC3986QueryEncoder")

// withQueryEncoder temporarily installs enc, restoring the previous
// encoder when the test finishes.
Expand All @@ -34,15 +34,15 @@ func TestDefaultQueryEncoder(t *testing.T) {
t.Cleanup(func() { DefaultQueryEncoder = prev })
}

t.Run("net/url encoder keeps + for spaces (default)", func(t *testing.T) {
t.Run("net/url encoder keeps + for spaces", func(t *testing.T) {
withQueryEncoder(t, NetURLQueryEncoder{})

result, err := StyleParamWithLocation("form", false, "filter", ParamLocationQuery, "name eq 'x'")
assert.NoError(t, err)
assert.EqualValues(t, "filter=name+eq+%27x%27", result)
})

t.Run("RFC 3986 encoder uses %20 for spaces", func(t *testing.T) {
t.Run("RFC 3986 encoder uses %20 for spaces (default)", func(t *testing.T) {
withQueryEncoder(t, RFC3986QueryEncoder{})

result, err := StyleParamWithLocation("form", false, "filter", ParamLocationQuery, "name eq 'x'")
Expand Down