Repository: swaggest/openapi-go
Pull request #16 (2020-10-26 08:20:21)

Use deepObject serialization for object in query

codecov[bot]: # [Codecov](https://codecov.io/gh/swaggest/openapi-go/pull/16?src=pr&el=h1) Report
> Merging [#16](https://codecov.io/gh/swaggest/openapi-go/pull/16?src=pr&el=desc) into [master](https://codecov.io/gh/swaggest/openapi-go/commit/d3c23d7737b917a7d5345bbb7c4cf998ad4949a7?el=desc) will **increase** coverage by `0.05%`.
> The diff coverage is `85.71%`.

[![Impacted file tree graph](https://codecov.io/gh/swaggest/openapi-go/pull/16/graphs/tree.svg?width=650&height=150&src=pr&token=3uETjeijpQ)](https://codecov.io/gh/swaggest/openapi-go/pull/16?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #16      +/-   ##
==========================================
+ Coverage   37.15%   37.20%   +0.05%     
==========================================
  Files           5        5              
  Lines        3467     3470       +3     
==========================================
+ Hits         1288     1291       +3     
  Misses       1895     1895              
  Partials      284      284              
```

| Flag | Coverage Δ | |
|---|---|---|
| #unittests | `37.20% <85.71%> (+0.05%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/swaggest/openapi-go/pull/16?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [openapi3/reflect.go](https://codecov.io/gh/swaggest/openapi-go/pull/16/diff?src=pr&el=tree#diff-b3BlbmFwaTMvcmVmbGVjdC5nbw==) | `85.57% <85.71%> (+0.21%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/swaggest/openapi-go/pull/16?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/swaggest/openapi-go/pull/16?src=pr&el=footer). Last update [d3c23d7...de2565e](https://codecov.io/gh/swaggest/openapi-go/pull/16?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/openapi3/reflect.go b/openapi3/reflect.go
index 2300d3c..3d257f1 100644
--- a/openapi3/reflect.go
+++ b/openapi3/reflect.go
@@ -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)


Test patch:
diff --git a/openapi3/reflect_test.go b/openapi3/reflect_test.go
index 7cb142e..036dc83 100644
--- a/openapi3/reflect_test.go
+++ b/openapi3/reflect_test.go
@@ -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))
+}