Skip to content

Commit de2a3e6

Browse files
authored
Merge pull request #1727 from shuvamk/fix/param-explode-default
fix(parser): default explode to false for non-form parameter styles
2 parents 9cba433 + a1b8031 commit de2a3e6

4 files changed

Lines changed: 208 additions & 10 deletions

File tree

‎openapi/parser/parse_header.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,16 @@ func (p *parser) parseHeader(name string, header *ogen.Header, ctx *jsonpointer.
8686
return nil, p.wrapField("content", p.file(ctx), locator, err)
8787
}
8888

89+
style := inferParamStyle(locatedIn, header.Style)
8990
op := &openapi.Header{
9091
Name: name,
9192
Description: header.Description,
9293
Deprecated: header.Deprecated,
9394
Schema: schema,
9495
Content: content,
9596
In: locatedIn,
96-
Style: inferParamStyle(locatedIn, header.Style),
97-
Explode: inferParamExplode(locatedIn, header.Explode),
97+
Style: style,
98+
Explode: inferParamExplode(style, header.Explode),
9899
Required: header.Required,
99100
Pointer: locator.Pointer(p.file(ctx)),
100101
}

‎openapi/parser/parse_mediatype.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,12 @@ func (p *parser) parseMediaType(ct string, m ogen.Media, ctx *jsonpointer.Resolv
117117
rerr = p.wrapLocation(p.file(ctx), locator, rerr)
118118
}()
119119

120+
style := inferParamStyle(openapi.LocationQuery, e.Style)
120121
encoding := &openapi.Encoding{
121122
ContentType: e.ContentType,
122123
Headers: map[string]*openapi.Header{},
123-
Style: inferParamStyle(openapi.LocationQuery, e.Style),
124-
Explode: inferParamExplode(openapi.LocationQuery, e.Explode),
124+
Style: style,
125+
Explode: inferParamExplode(style, e.Explode),
125126
AllowReserved: e.AllowReserved,
126127
Pointer: locator.Pointer(p.file(ctx)),
127128
}

‎openapi/parser/parse_parameter.go‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,16 @@ func (p *parser) parseParameter(param *ogen.Parameter, ctx *jsonpointer.ResolveC
180180
return nil, p.wrapField("content", p.file(ctx), locator, err)
181181
}
182182

183+
style := inferParamStyle(locatedIn, param.Style)
183184
op := &openapi.Parameter{
184185
Name: param.Name,
185186
Description: param.Description,
186187
Deprecated: param.Deprecated,
187188
Schema: schema,
188189
Content: content,
189190
In: locatedIn,
190-
Style: inferParamStyle(locatedIn, param.Style),
191-
Explode: inferParamExplode(locatedIn, param.Explode),
191+
Style: style,
192+
Explode: inferParamExplode(style, param.Explode),
192193
Required: param.Required,
193194
AllowReserved: param.AllowReserved,
194195
Pointer: locator.Pointer(p.file(ctx)),
@@ -238,18 +239,22 @@ func inferParamStyle(locatedIn openapi.ParameterLocation, style string) openapi.
238239
return openapi.ParameterStyle(style)
239240
}
240241

241-
func inferParamExplode(locatedIn openapi.ParameterLocation, explode *bool) bool {
242+
func inferParamExplode(style openapi.ParameterStyle, explode *bool) bool {
242243
if explode != nil {
243244
return *explode
244245
}
245246

246247
// When style is form, the default value is true.
247248
// For all other styles, the default value is false.
248-
if locatedIn.Query() || locatedIn.Cookie() {
249+
switch style {
250+
case openapi.QueryStyleForm:
249251
return true
252+
case openapi.QueryStyleDeepObject:
253+
// ogen supports deepObject only in exploded form.
254+
return true
255+
default:
256+
return false
250257
}
251-
252-
return false
253258
}
254259

255260
func (p *parser) validateParamStyle(param *openapi.Parameter, file location.File) error {

‎openapi/parser/parse_parameter_test.go‎

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,194 @@ func TestNullOnlyParameterRejected(t *testing.T) {
131131
a.Error(err)
132132
a.Contains(err.Error(), "combination")
133133
}
134+
135+
func explodePtr(v bool) *bool { return &v }
136+
137+
func stringArraySchema() *ogen.Schema {
138+
return &ogen.Schema{
139+
Type: "array",
140+
Items: &ogen.Items{Item: &ogen.Schema{Type: "string"}},
141+
}
142+
}
143+
144+
func TestParameterExplodeDefault(t *testing.T) {
145+
object := &ogen.Schema{
146+
Type: "object",
147+
Properties: []ogen.Property{{Name: "id", Schema: &ogen.Schema{Type: "string"}}},
148+
}
149+
150+
tests := []struct {
151+
name string
152+
style string
153+
explode *bool
154+
schema *ogen.Schema
155+
want bool
156+
}{
157+
{"Unset", "", nil, stringArraySchema(), true},
158+
{"Form", "form", nil, stringArraySchema(), true},
159+
{"SpaceDelimited", "spaceDelimited", nil, stringArraySchema(), false},
160+
{"PipeDelimited", "pipeDelimited", nil, stringArraySchema(), false},
161+
{"DeepObject", "deepObject", nil, object, true},
162+
{"SpaceDelimitedExplodeTrue", "spaceDelimited", explodePtr(true), stringArraySchema(), true},
163+
{"PipeDelimitedExplodeTrue", "pipeDelimited", explodePtr(true), stringArraySchema(), true},
164+
{"FormExplodeFalse", "form", explodePtr(false), stringArraySchema(), false},
165+
}
166+
for _, tt := range tests {
167+
t.Run(tt.name, func(t *testing.T) {
168+
root := &ogen.Spec{
169+
OpenAPI: "3.0.3",
170+
Paths: map[string]*ogen.PathItem{
171+
"/items": {
172+
Get: &ogen.Operation{
173+
OperationID: "listItems",
174+
Parameters: []*ogen.Parameter{
175+
{
176+
Name: "ids",
177+
In: "query",
178+
Style: tt.style,
179+
Explode: tt.explode,
180+
Schema: tt.schema,
181+
},
182+
},
183+
Responses: map[string]*ogen.Response{
184+
"200": {Description: "OK"},
185+
},
186+
},
187+
},
188+
},
189+
}
190+
191+
a := require.New(t)
192+
193+
var raw yaml.Node
194+
a.NoError(raw.Encode(root))
195+
root.Raw = &raw
196+
197+
spec, err := Parse(root, Settings{
198+
RootURL: testRootURL,
199+
})
200+
a.NoError(err)
201+
a.Len(spec.Operations, 1)
202+
203+
params := spec.Operations[0].Parameters
204+
a.Len(params, 1)
205+
a.Equal(tt.want, params[0].Explode)
206+
})
207+
}
208+
}
209+
210+
func TestEncodingExplodeDefault(t *testing.T) {
211+
tests := []struct {
212+
name string
213+
style string
214+
explode *bool
215+
want bool
216+
}{
217+
{"Unset", "", nil, true},
218+
{"Form", "form", nil, true},
219+
{"SpaceDelimited", "spaceDelimited", nil, false},
220+
{"PipeDelimited", "pipeDelimited", nil, false},
221+
{"PipeDelimitedExplodeTrue", "pipeDelimited", explodePtr(true), true},
222+
}
223+
for _, tt := range tests {
224+
t.Run(tt.name, func(t *testing.T) {
225+
const ct = "application/x-www-form-urlencoded"
226+
227+
root := &ogen.Spec{
228+
OpenAPI: "3.0.3",
229+
Paths: map[string]*ogen.PathItem{
230+
"/submit": {
231+
Post: &ogen.Operation{
232+
OperationID: "submit",
233+
RequestBody: &ogen.RequestBody{
234+
Required: true,
235+
Content: map[string]ogen.Media{
236+
ct: {
237+
Schema: &ogen.Schema{
238+
Type: "object",
239+
Properties: []ogen.Property{{Name: "tags", Schema: stringArraySchema()}},
240+
},
241+
Encoding: map[string]ogen.Encoding{
242+
"tags": {Style: tt.style, Explode: tt.explode},
243+
},
244+
},
245+
},
246+
},
247+
Responses: map[string]*ogen.Response{
248+
"200": {Description: "OK"},
249+
},
250+
},
251+
},
252+
},
253+
}
254+
255+
a := require.New(t)
256+
257+
var raw yaml.Node
258+
a.NoError(raw.Encode(root))
259+
root.Raw = &raw
260+
261+
spec, err := Parse(root, Settings{
262+
RootURL: testRootURL,
263+
})
264+
a.NoError(err)
265+
a.Len(spec.Operations, 1)
266+
267+
encoding := spec.Operations[0].RequestBody.Content[ct].Encoding
268+
a.Contains(encoding, "tags")
269+
a.Equal(tt.want, encoding["tags"].Explode)
270+
})
271+
}
272+
}
273+
274+
func TestHeaderExplodeDefault(t *testing.T) {
275+
tests := []struct {
276+
name string
277+
explode *bool
278+
want bool
279+
}{
280+
{"Unset", nil, false},
281+
{"ExplodeTrue", explodePtr(true), true},
282+
}
283+
for _, tt := range tests {
284+
t.Run(tt.name, func(t *testing.T) {
285+
root := &ogen.Spec{
286+
OpenAPI: "3.0.3",
287+
Paths: map[string]*ogen.PathItem{
288+
"/items": {
289+
Get: &ogen.Operation{
290+
OperationID: "listItems",
291+
Responses: map[string]*ogen.Response{
292+
"200": {
293+
Description: "OK",
294+
Headers: map[string]*ogen.Header{
295+
"X-Tags": {
296+
Explode: tt.explode,
297+
Schema: stringArraySchema(),
298+
},
299+
},
300+
},
301+
},
302+
},
303+
},
304+
},
305+
}
306+
307+
a := require.New(t)
308+
309+
var raw yaml.Node
310+
a.NoError(raw.Encode(root))
311+
root.Raw = &raw
312+
313+
spec, err := Parse(root, Settings{
314+
RootURL: testRootURL,
315+
})
316+
a.NoError(err)
317+
a.Len(spec.Operations, 1)
318+
319+
headers := spec.Operations[0].Responses.StatusCode[200].Headers
320+
a.Contains(headers, "X-Tags")
321+
a.Equal(tt.want, headers["X-Tags"].Explode)
322+
})
323+
}
324+
}

0 commit comments

Comments
 (0)