Skip to content

Commit

Permalink
Use deepObject serialization for object in query (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Oct 26, 2020
1 parent d3c23d7 commit 1571c5f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
14 changes: 10 additions & 4 deletions openapi3/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,19 @@ func (r *Reflector) parseParametersIn(
refl.ReadStringTag(field.Tag, "collectionFormat", &swg2CollectionFormat)
switch swg2CollectionFormat {
case "csv":
p.WithStyle("form").WithExplode(false)
p.WithStyle(string(QueryParameterStyleForm)).WithExplode(false)
case "ssv":
p.WithStyle("spaceDelimited").WithExplode(false)
p.WithStyle(string(QueryParameterStyleSpaceDelimited)).WithExplode(false)
case "pipes":
p.WithStyle("pipeDelimited").WithExplode(false)
p.WithStyle(string(QueryParameterStylePipeDelimited)).WithExplode(false)
case "multi":
p.WithStyle("form").WithExplode(true)
p.WithStyle(string(QueryParameterStyleForm)).WithExplode(true)
}

if in == ParameterInQuery {
if propertySchema.HasType(jsonschema.Object) {
p.WithStyle(string(QueryParameterStyleDeepObject)).WithExplode(true)
}
}

err := refl.PopulateFieldsFromTags(&p, field.Tag)
Expand Down
40 changes: 40 additions & 0 deletions openapi3/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,43 @@ func TestReflector_SetupRequest(t *testing.T) {

assertjson.Equal(t, expected, b, string(b))
}

func TestReflector_SetupRequest_queryObject(t *testing.T) {
reflector := openapi3.Reflector{}
op := openapi3.Operation{}

s := reflector.SpecEns()
s.Info.Title = apiName
s.Info.Version = apiVersion

require.NoError(t, reflector.SetupRequest(openapi3.OperationContext{
Operation: &op,
HTTPMethod: http.MethodGet,
Input: new(struct {
InQuery map[int]float64 `query:"in_query"`
}),
}))
require.NoError(t, s.AddOperation(http.MethodPost, "/somewhere", op))

b, err := assertjson.MarshalIndentCompact(s, "", " ", 120)
assert.NoError(t, err)

expected := []byte(`{
"openapi":"3.0.2","info":{"title":"SampleAPI","version":"1.2.3"},
"paths":{
"/somewhere":{
"post":{
"parameters":[
{
"name":"in_query","in":"query","style":"deepObject","explode":true,
"schema":{"type":"object","additionalProperties":{"type":"number"}}
}
],
"responses":{"204":{"description":"No Content"}}
}
}
}
}`)

assertjson.Equal(t, expected, b, string(b))
}

0 comments on commit 1571c5f

Please sign in to comment.