Skip to content

Commit 1571c5f

Browse files
authored
Use deepObject serialization for object in query (#16)
1 parent d3c23d7 commit 1571c5f

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

openapi3/reflect.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,19 @@ func (r *Reflector) parseParametersIn(
232232
refl.ReadStringTag(field.Tag, "collectionFormat", &swg2CollectionFormat)
233233
switch swg2CollectionFormat {
234234
case "csv":
235-
p.WithStyle("form").WithExplode(false)
235+
p.WithStyle(string(QueryParameterStyleForm)).WithExplode(false)
236236
case "ssv":
237-
p.WithStyle("spaceDelimited").WithExplode(false)
237+
p.WithStyle(string(QueryParameterStyleSpaceDelimited)).WithExplode(false)
238238
case "pipes":
239-
p.WithStyle("pipeDelimited").WithExplode(false)
239+
p.WithStyle(string(QueryParameterStylePipeDelimited)).WithExplode(false)
240240
case "multi":
241-
p.WithStyle("form").WithExplode(true)
241+
p.WithStyle(string(QueryParameterStyleForm)).WithExplode(true)
242+
}
243+
244+
if in == ParameterInQuery {
245+
if propertySchema.HasType(jsonschema.Object) {
246+
p.WithStyle(string(QueryParameterStyleDeepObject)).WithExplode(true)
247+
}
242248
}
243249

244250
err := refl.PopulateFieldsFromTags(&p, field.Tag)

openapi3/reflect_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,43 @@ func TestReflector_SetupRequest(t *testing.T) {
450450

451451
assertjson.Equal(t, expected, b, string(b))
452452
}
453+
454+
func TestReflector_SetupRequest_queryObject(t *testing.T) {
455+
reflector := openapi3.Reflector{}
456+
op := openapi3.Operation{}
457+
458+
s := reflector.SpecEns()
459+
s.Info.Title = apiName
460+
s.Info.Version = apiVersion
461+
462+
require.NoError(t, reflector.SetupRequest(openapi3.OperationContext{
463+
Operation: &op,
464+
HTTPMethod: http.MethodGet,
465+
Input: new(struct {
466+
InQuery map[int]float64 `query:"in_query"`
467+
}),
468+
}))
469+
require.NoError(t, s.AddOperation(http.MethodPost, "/somewhere", op))
470+
471+
b, err := assertjson.MarshalIndentCompact(s, "", " ", 120)
472+
assert.NoError(t, err)
473+
474+
expected := []byte(`{
475+
"openapi":"3.0.2","info":{"title":"SampleAPI","version":"1.2.3"},
476+
"paths":{
477+
"/somewhere":{
478+
"post":{
479+
"parameters":[
480+
{
481+
"name":"in_query","in":"query","style":"deepObject","explode":true,
482+
"schema":{"type":"object","additionalProperties":{"type":"number"}}
483+
}
484+
],
485+
"responses":{"204":{"description":"No Content"}}
486+
}
487+
}
488+
}
489+
}`)
490+
491+
assertjson.Equal(t, expected, b, string(b))
492+
}

0 commit comments

Comments
 (0)