Skip to content

Commit af28bea

Browse files
authored
Add JSON collectionFormat (#193)
1 parent 0a163b6 commit af28bea

8 files changed

Lines changed: 21 additions & 12 deletions

File tree

_examples/advanced-generic-openapi31/_testdata/openapi.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,11 @@
592592
"style":"deepObject","explode":true
593593
},
594594
{
595-
"name":"json_filter","in":"query","description":"JSON object value in query.",
595+
"name":"json_map","in":"query","description":"JSON object (map) value in query.",
596+
"content":{"application/json":{"schema":{"additionalProperties":{"type":"number"},"type":["null","object"]}}}
597+
},
598+
{
599+
"name":"json_filter","in":"query","description":"JSON object (struct) value in query.",
596600
"content":{"application/json":{"schema":{"$ref":"#/components/schemas/AdvancedJsonFilter"}}}
597601
},
598602
{
@@ -864,7 +868,8 @@
864868
"properties":{
865869
"deepObjectFilter":{"$ref":"#/components/schemas/AdvancedDeepObjectFilter"},
866870
"inQuery":{"additionalProperties":{"type":"number"},"type":["object","null"]},
867-
"jsonFilter":{"$ref":"#/components/schemas/AdvancedJsonFilter"}
871+
"jsonFilter":{"$ref":"#/components/schemas/AdvancedJsonFilter"},
872+
"jsonMap":{"additionalProperties":{"type":"number"},"type":["object","null"]}
868873
},
869874
"type":"object"
870875
},

_examples/advanced-generic-openapi31/query_object.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@ func queryObject() usecase.Interactor {
2020

2121
type inputQueryObject struct {
2222
Query map[int]float64 `query:"in_query" description:"Object value in query."`
23-
JSONFilter jsonFilter `query:"json_filter" description:"JSON object value in query."`
23+
JSONMap map[int]float64 `query:"json_map" collectionFormat:"json" description:"JSON object (map) value in query."`
24+
JSONFilter jsonFilter `query:"json_filter" description:"JSON object (struct) value in query."`
2425
DeepObjectFilter deepObjectFilter `query:"deep_object_filter" description:"Deep object value in query params."`
2526
}
2627

2728
type outputQueryObject struct {
2829
Query map[int]float64 `json:"inQuery"`
30+
JSONMap map[int]float64 `json:"jsonMap"`
2931
JSONFilter jsonFilter `json:"jsonFilter"`
3032
DeepObjectFilter deepObjectFilter `json:"deepObjectFilter"`
3133
}
3234

3335
u := usecase.NewInteractor(func(ctx context.Context, in inputQueryObject, out *outputQueryObject) (err error) {
3436
out.Query = in.Query
37+
out.JSONMap = in.JSONMap
3538
out.JSONFilter = in.JSONFilter
3639
out.DeepObjectFilter = in.DeepObjectFilter
3740

_examples/advanced-generic-openapi31/query_object_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ func Test_queryObject(t *testing.T) {
5353
},
5454
{
5555
name: "ok",
56-
url: `/query-object?in_query[1]=0&in_query[2]=0&in_query[3]=0&json_filter={"foo":"strin"}&deep_object_filter[bar]=asd`,
56+
url: `/query-object?in_query[1]=0&in_query[2]=0&in_query[3]=0&json_map={"123":123.45}&json_filter={"foo":"strin"}&deep_object_filter[bar]=asd`,
5757
code: http.StatusOK,
5858
resp: `{
59-
"inQuery":{"1":0,"2":0,"3":0},"jsonFilter":{"foo":"strin"},
59+
"inQuery":{"1":0,"2":0,"3":0},"jsonMap":{"123":123.45},"jsonFilter":{"foo":"strin"},
6060
"deepObjectFilter":{"bar":"asd"}
6161
}`,
6262
},

_examples/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/stretchr/testify v1.9.0
1919
github.com/swaggest/assertjson v1.9.0
2020
github.com/swaggest/jsonschema-go v0.3.66
21-
github.com/swaggest/openapi-go v0.2.46
21+
github.com/swaggest/openapi-go v0.2.47
2222
github.com/swaggest/rest v0.0.0-00010101000000-000000000000
2323
github.com/swaggest/swgui v1.8.0
2424
github.com/swaggest/usecase v1.3.1

_examples/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ github.com/swaggest/form/v5 v5.1.1 h1:ct6/rOQBGrqWUQ0FUv3vW5sHvTUb31AwTUWj947N6c
125125
github.com/swaggest/form/v5 v5.1.1/go.mod h1:X1hraaoONee20PMnGNLQpO32f9zbQ0Czfm7iZThuEKg=
126126
github.com/swaggest/jsonschema-go v0.3.66 h1:4c5d7NRRqPLTswsbaypKqcMe3Z+CYHE3/lGsPIByp8o=
127127
github.com/swaggest/jsonschema-go v0.3.66/go.mod h1:7N43/CwdaWgPUDfYV70K7Qm79tRqe/al7gLSt9YeGIE=
128-
github.com/swaggest/openapi-go v0.2.46 h1:T7gCeErrNLu93aACb2/IacAPAe/lOcGz141a1+LYEx8=
129-
github.com/swaggest/openapi-go v0.2.46/go.mod h1:MK5O26lG289kFgMOyXK1VXDoTZ89KJ8Vt0v0ic23zZw=
128+
github.com/swaggest/openapi-go v0.2.47 h1:qBh28FHz0M1QSJmGRCcY/Xt9WKRkECKXGUbw/U8IcJ4=
129+
github.com/swaggest/openapi-go v0.2.47/go.mod h1:MK5O26lG289kFgMOyXK1VXDoTZ89KJ8Vt0v0ic23zZw=
130130
github.com/swaggest/refl v1.3.0 h1:PEUWIku+ZznYfsoyheF97ypSduvMApYyGkYF3nabS0I=
131131
github.com/swaggest/refl v1.3.0/go.mod h1:3Ujvbmh1pfSbDYjC6JGG7nMgPvpG0ehQL4iNonnLNbg=
132132
github.com/swaggest/swgui v1.8.0 h1:dPu8TsYIOraaObAkyNdoiLI8mu7nOqQ6SU7HOv254rM=

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/swaggest/assertjson v1.9.0
1515
github.com/swaggest/form/v5 v5.1.1
1616
github.com/swaggest/jsonschema-go v0.3.66
17-
github.com/swaggest/openapi-go v0.2.46
17+
github.com/swaggest/openapi-go v0.2.47
1818
github.com/swaggest/refl v1.3.0
1919
github.com/swaggest/usecase v1.3.1
2020
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ github.com/swaggest/form/v5 v5.1.1 h1:ct6/rOQBGrqWUQ0FUv3vW5sHvTUb31AwTUWj947N6c
8080
github.com/swaggest/form/v5 v5.1.1/go.mod h1:X1hraaoONee20PMnGNLQpO32f9zbQ0Czfm7iZThuEKg=
8181
github.com/swaggest/jsonschema-go v0.3.66 h1:4c5d7NRRqPLTswsbaypKqcMe3Z+CYHE3/lGsPIByp8o=
8282
github.com/swaggest/jsonschema-go v0.3.66/go.mod h1:7N43/CwdaWgPUDfYV70K7Qm79tRqe/al7gLSt9YeGIE=
83-
github.com/swaggest/openapi-go v0.2.46 h1:T7gCeErrNLu93aACb2/IacAPAe/lOcGz141a1+LYEx8=
84-
github.com/swaggest/openapi-go v0.2.46/go.mod h1:MK5O26lG289kFgMOyXK1VXDoTZ89KJ8Vt0v0ic23zZw=
83+
github.com/swaggest/openapi-go v0.2.47 h1:qBh28FHz0M1QSJmGRCcY/Xt9WKRkECKXGUbw/U8IcJ4=
84+
github.com/swaggest/openapi-go v0.2.47/go.mod h1:MK5O26lG289kFgMOyXK1VXDoTZ89KJ8Vt0v0ic23zZw=
8585
github.com/swaggest/refl v1.3.0 h1:PEUWIku+ZznYfsoyheF97ypSduvMApYyGkYF3nabS0I=
8686
github.com/swaggest/refl v1.3.0/go.mod h1:3Ujvbmh1pfSbDYjC6JGG7nMgPvpG0ehQL4iNonnLNbg=
8787
github.com/swaggest/usecase v1.3.1 h1:JdKV30MTSsDxAXxkldLNcEn8O2uf565khyo6gr5sS+w=

request/factory.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ func (df *DecoderFactory) jsonParams(formDecoder *form.Decoder, in rest.ParamIn,
229229

230230
fieldVal := v.Interface()
231231

232-
if refl.HasTaggedFields(fieldVal, jsonTag) && !refl.HasTaggedFields(fieldVal, string(in)) {
232+
if sf.Tag.Get("collectionFormat") == "json" ||
233+
(refl.HasTaggedFields(fieldVal, jsonTag) && !refl.HasTaggedFields(fieldVal, string(in))) {
233234
// If value is a struct with `json` tags, custom decoder unmarshals json
234235
// from a string value into a struct.
235236
formDecoder.RegisterFunc(func(s string) (interface{}, error) {

0 commit comments

Comments
 (0)