Skip to content

Commit

Permalink
Add JSON collectionFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Mar 5, 2024
1 parent 0a163b6 commit 3f5a2b9
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 12 deletions.
9 changes: 7 additions & 2 deletions _examples/advanced-generic-openapi31/_testdata/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,11 @@
"style":"deepObject","explode":true
},
{
"name":"json_filter","in":"query","description":"JSON object value in query.",
"name":"json_map","in":"query","description":"JSON object (map) value in query.",
"content":{"application/json":{"schema":{"additionalProperties":{"type":"number"},"type":["null","object"]}}}
},
{
"name":"json_filter","in":"query","description":"JSON object (struct) value in query.",
"content":{"application/json":{"schema":{"$ref":"#/components/schemas/AdvancedJsonFilter"}}}
},
{
Expand Down Expand Up @@ -864,7 +868,8 @@
"properties":{
"deepObjectFilter":{"$ref":"#/components/schemas/AdvancedDeepObjectFilter"},
"inQuery":{"additionalProperties":{"type":"number"},"type":["object","null"]},
"jsonFilter":{"$ref":"#/components/schemas/AdvancedJsonFilter"}
"jsonFilter":{"$ref":"#/components/schemas/AdvancedJsonFilter"},
"jsonMap":{"additionalProperties":{"type":"number"},"type":["object","null"]}
},
"type":"object"
},
Expand Down
5 changes: 4 additions & 1 deletion _examples/advanced-generic-openapi31/query_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ func queryObject() usecase.Interactor {

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

type outputQueryObject struct {
Query map[int]float64 `json:"inQuery"`
JSONMap map[int]float64 `json:"jsonMap"`
JSONFilter jsonFilter `json:"jsonFilter"`
DeepObjectFilter deepObjectFilter `json:"deepObjectFilter"`
}

u := usecase.NewInteractor(func(ctx context.Context, in inputQueryObject, out *outputQueryObject) (err error) {
out.Query = in.Query
out.JSONMap = in.JSONMap
out.JSONFilter = in.JSONFilter
out.DeepObjectFilter = in.DeepObjectFilter

Expand Down
4 changes: 2 additions & 2 deletions _examples/advanced-generic-openapi31/query_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func Test_queryObject(t *testing.T) {
},
{
name: "ok",
url: `/query-object?in_query[1]=0&in_query[2]=0&in_query[3]=0&json_filter={"foo":"strin"}&deep_object_filter[bar]=asd`,
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`,
code: http.StatusOK,
resp: `{
"inQuery":{"1":0,"2":0,"3":0},"jsonFilter":{"foo":"strin"},
"inQuery":{"1":0,"2":0,"3":0},"jsonMap":{"123":123.45},"jsonFilter":{"foo":"strin"},
"deepObjectFilter":{"bar":"asd"}
}`,
},
Expand Down
2 changes: 1 addition & 1 deletion _examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/swaggest/assertjson v1.9.0
github.com/swaggest/jsonschema-go v0.3.66
github.com/swaggest/openapi-go v0.2.46
github.com/swaggest/openapi-go v0.2.47
github.com/swaggest/rest v0.0.0-00010101000000-000000000000
github.com/swaggest/swgui v1.8.0
github.com/swaggest/usecase v1.3.1
Expand Down
4 changes: 2 additions & 2 deletions _examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ github.com/swaggest/form/v5 v5.1.1 h1:ct6/rOQBGrqWUQ0FUv3vW5sHvTUb31AwTUWj947N6c
github.com/swaggest/form/v5 v5.1.1/go.mod h1:X1hraaoONee20PMnGNLQpO32f9zbQ0Czfm7iZThuEKg=
github.com/swaggest/jsonschema-go v0.3.66 h1:4c5d7NRRqPLTswsbaypKqcMe3Z+CYHE3/lGsPIByp8o=
github.com/swaggest/jsonschema-go v0.3.66/go.mod h1:7N43/CwdaWgPUDfYV70K7Qm79tRqe/al7gLSt9YeGIE=
github.com/swaggest/openapi-go v0.2.46 h1:T7gCeErrNLu93aACb2/IacAPAe/lOcGz141a1+LYEx8=
github.com/swaggest/openapi-go v0.2.46/go.mod h1:MK5O26lG289kFgMOyXK1VXDoTZ89KJ8Vt0v0ic23zZw=
github.com/swaggest/openapi-go v0.2.47 h1:qBh28FHz0M1QSJmGRCcY/Xt9WKRkECKXGUbw/U8IcJ4=
github.com/swaggest/openapi-go v0.2.47/go.mod h1:MK5O26lG289kFgMOyXK1VXDoTZ89KJ8Vt0v0ic23zZw=
github.com/swaggest/refl v1.3.0 h1:PEUWIku+ZznYfsoyheF97ypSduvMApYyGkYF3nabS0I=
github.com/swaggest/refl v1.3.0/go.mod h1:3Ujvbmh1pfSbDYjC6JGG7nMgPvpG0ehQL4iNonnLNbg=
github.com/swaggest/swgui v1.8.0 h1:dPu8TsYIOraaObAkyNdoiLI8mu7nOqQ6SU7HOv254rM=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/swaggest/assertjson v1.9.0
github.com/swaggest/form/v5 v5.1.1
github.com/swaggest/jsonschema-go v0.3.66
github.com/swaggest/openapi-go v0.2.46
github.com/swaggest/openapi-go v0.2.47
github.com/swaggest/refl v1.3.0
github.com/swaggest/usecase v1.3.1
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ github.com/swaggest/form/v5 v5.1.1 h1:ct6/rOQBGrqWUQ0FUv3vW5sHvTUb31AwTUWj947N6c
github.com/swaggest/form/v5 v5.1.1/go.mod h1:X1hraaoONee20PMnGNLQpO32f9zbQ0Czfm7iZThuEKg=
github.com/swaggest/jsonschema-go v0.3.66 h1:4c5d7NRRqPLTswsbaypKqcMe3Z+CYHE3/lGsPIByp8o=
github.com/swaggest/jsonschema-go v0.3.66/go.mod h1:7N43/CwdaWgPUDfYV70K7Qm79tRqe/al7gLSt9YeGIE=
github.com/swaggest/openapi-go v0.2.46 h1:T7gCeErrNLu93aACb2/IacAPAe/lOcGz141a1+LYEx8=
github.com/swaggest/openapi-go v0.2.46/go.mod h1:MK5O26lG289kFgMOyXK1VXDoTZ89KJ8Vt0v0ic23zZw=
github.com/swaggest/openapi-go v0.2.47 h1:qBh28FHz0M1QSJmGRCcY/Xt9WKRkECKXGUbw/U8IcJ4=
github.com/swaggest/openapi-go v0.2.47/go.mod h1:MK5O26lG289kFgMOyXK1VXDoTZ89KJ8Vt0v0ic23zZw=
github.com/swaggest/refl v1.3.0 h1:PEUWIku+ZznYfsoyheF97ypSduvMApYyGkYF3nabS0I=
github.com/swaggest/refl v1.3.0/go.mod h1:3Ujvbmh1pfSbDYjC6JGG7nMgPvpG0ehQL4iNonnLNbg=
github.com/swaggest/usecase v1.3.1 h1:JdKV30MTSsDxAXxkldLNcEn8O2uf565khyo6gr5sS+w=
Expand Down
3 changes: 2 additions & 1 deletion request/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ func (df *DecoderFactory) jsonParams(formDecoder *form.Decoder, in rest.ParamIn,

fieldVal := v.Interface()

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

0 comments on commit 3f5a2b9

Please sign in to comment.