Skip to content

Commit b173098

Browse files
mromaszewiczclaude
andauthored
feat: default query encoding to RFC 3986 (space as %20) (#190)
Closes: #189 Flip `DefaultQueryEncoder` from `NetURLQueryEncoder` to `RFC3986QueryEncoder`, so generated clients encode a space in query parameters as `%20` instead of `+`. OpenAPI's default `style: form` for query parameters is defined by RFC 6570, which encodes a space as `%20`; `+` belongs to `application/x-www-form-urlencoded`, which applies to request bodies. Servers that accept `+` as a space also accept `%20`, but strict RFC 3986 servers reject `+`. This changes default client behavior. To restore the previous encoding, set `runtime.DefaultQueryEncoder = runtime.NetURLQueryEncoder{}` during program initialization. Co-authored-by: Claude <noreply@anthropic.com>
1 parent c67d5d3 commit b173098

4 files changed

Lines changed: 28 additions & 26 deletions

File tree

‎README.md‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,27 +113,28 @@ cases where round-tripping is not possible in principle.
113113

114114
##### Configuring query encoding
115115

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

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

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

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

134135
```go
135136
func init() {
136-
runtime.DefaultQueryEncoder = runtime.RFC3986QueryEncoder{}
137+
runtime.DefaultQueryEncoder = runtime.NetURLQueryEncoder{}
137138
}
138139
```
139140

@@ -146,8 +147,7 @@ correct, media-type-defined behavior.
146147
- **Query and path values are percent-encoded.** Reserved characters
147148
(`&`, `=`, `#`, `?`, etc.) and non-ASCII bytes are escaped via
148149
`url.QueryEscape` / `url.PathEscape`. Spaces in query values are encoded
149-
as `+` (form-urlencoded convention), matching `url.Values.Encode()`,
150-
unless `DefaultQueryEncoder` is changed (see above).
150+
as `%20` (RFC 3986), unless `DefaultQueryEncoder` is changed (see above).
151151
- **Header values are passed through raw.** Per RFC 7230 §3.2.6, header
152152
field values may contain visible ASCII plus space/tab; bytes ≥ `0x80` are
153153
`obs-text` and explicitly marked obsolete in RFC 9110. There is no

‎deepobject_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestDeepObject(t *testing.T) {
107107
require.NoError(t, err)
108108
// Spaces in values are encoded as '+' per url.QueryEscape (form-encoded
109109
// convention, matching the pre-v2.7.0 behavior of url.Values.Encode()).
110-
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)
110+
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)
111111

112112
// Use url.ParseQuery for the round-trip — it decodes percent-escapes and
113113
// '+' back to literal characters, matching what a real HTTP server does.

‎encoder.go‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ type QueryEncoder interface {
3939

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

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

7274
// DefaultQueryEncoder is the QueryEncoder used by all generated clients to
73-
// escape query parameters. It defaults to NetURLQueryEncoder for backwards
74-
// compatibility. Set it once during program initialization; like
75-
// http.DefaultClient, it is not safe to mutate concurrently with in-flight
76-
// requests.
77-
var DefaultQueryEncoder QueryEncoder = NetURLQueryEncoder{}
75+
// escape query parameters. It defaults to RFC3986QueryEncoder; before v1.10.0
76+
// the default was NetURLQueryEncoder. Set it once during program
77+
// initialization; like http.DefaultClient, it is not safe to mutate
78+
// concurrently with in-flight requests.
79+
var DefaultQueryEncoder QueryEncoder = RFC3986QueryEncoder{}

‎encoder_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222

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

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

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

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

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

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

0 commit comments

Comments
 (0)