diff --git a/_examples/advanced-generic-openapi31/_testdata/openapi.json b/_examples/advanced-generic-openapi31/_testdata/openapi.json index 103c613..39c980a 100644 --- a/_examples/advanced-generic-openapi31/_testdata/openapi.json +++ b/_examples/advanced-generic-openapi31/_testdata/openapi.json @@ -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"}}} }, { @@ -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" }, diff --git a/_examples/advanced-generic-openapi31/query_object.go b/_examples/advanced-generic-openapi31/query_object.go index 148f404..bb2d783 100644 --- a/_examples/advanced-generic-openapi31/query_object.go +++ b/_examples/advanced-generic-openapi31/query_object.go @@ -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 diff --git a/_examples/advanced-generic-openapi31/query_object_test.go b/_examples/advanced-generic-openapi31/query_object_test.go index cf95747..471374f 100644 --- a/_examples/advanced-generic-openapi31/query_object_test.go +++ b/_examples/advanced-generic-openapi31/query_object_test.go @@ -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"} }`, }, diff --git a/_examples/go.mod b/_examples/go.mod index 395d380..7b75e96 100644 --- a/_examples/go.mod +++ b/_examples/go.mod @@ -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 diff --git a/_examples/go.sum b/_examples/go.sum index b181883..7c87d29 100644 --- a/_examples/go.sum +++ b/_examples/go.sum @@ -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= diff --git a/go.mod b/go.mod index a21e629..2fc33be 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index b800645..ffe9d70 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/request/factory.go b/request/factory.go index 823e245..940437d 100644 --- a/request/factory.go +++ b/request/factory.go @@ -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) {