Skip to content

Commit b3e97b4

Browse files
authored
Use deepObject serialization for object in query (#17)
1 parent 1571c5f commit b3e97b4

File tree

7 files changed

+71
-10
lines changed

7 files changed

+71
-10
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
- v*
66
branches:
77
- master
8+
- main
89
pull_request:
910
jobs:
1011
golangci:
@@ -13,7 +14,7 @@ jobs:
1314
steps:
1415
- uses: actions/checkout@v2
1516
- name: golangci-lint
16-
uses: golangci/golangci-lint-action@v2.2.0
17+
uses: golangci/golangci-lint-action@v2.2.1
1718
with:
1819
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
1920
version: v1.31
@@ -25,4 +26,4 @@ jobs:
2526
# The secret `GITHUB_TOKEN` is automatically created by GitHub,
2627
# no need to create it manually.
2728
# https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#about-the-github_token-secret
28-
github-token: ${{ secrets.GITHUB_TOKEN }}
29+
github-token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ on:
55
- v*
66
branches:
77
- master
8+
- main
89
pull_request:
910
env:
1011
GO111MODULE: "on"
1112
jobs:
1213
test:
1314
strategy:
1415
matrix:
15-
go-version: [ 1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x ]
16+
go-version: [ 1.13.x, 1.14.x, 1.15.x ]
1617
runs-on: ubuntu-latest
1718
steps:
1819
- name: Install Go
@@ -36,4 +37,4 @@ jobs:
3637
uses: codecov/codecov-action@v1
3738
with:
3839
file: ./unit.coverprofile
39-
flags: unittests
40+
flags: unittests

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This library provides Go structures to marshal/unmarshal and reflect [OpenAPI Sc
77
[![Build Status](https://github.com/swaggest/openapi-go/workflows/test/badge.svg)](https://github.com/swaggest/openapi-go/actions?query=branch%3Amaster+workflow%3Atest)
88
[![Coverage Status](https://codecov.io/gh/swaggest/openapi-go/branch/master/graph/badge.svg)](https://codecov.io/gh/swaggest/openapi-go)
99
[![GoDevDoc](https://img.shields.io/badge/dev-doc-00ADD8?logo=go)](https://pkg.go.dev/github.com/swaggest/openapi-go)
10+
[![time tracker](https://wakatime.com/badge/github/swaggest/openapi-go.svg)](https://wakatime.com/badge/github/swaggest/openapi-go)
1011
![Code lines](https://sloc.xyz/github/swaggest/openapi-go/?category=code)
1112
![Comments](https://sloc.xyz/github/swaggest/openapi-go/?category=comments)
1213

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ module github.com/swaggest/openapi-go
33
go 1.13
44

55
require (
6-
github.com/bool64/dev v0.1.0
6+
github.com/bool64/dev v0.1.5
77
github.com/stretchr/testify v1.6.1
88
github.com/swaggest/assertjson v1.3.0
9-
github.com/swaggest/jsonschema-go v0.3.8
10-
github.com/swaggest/refl v0.1.3
9+
github.com/swaggest/jsonschema-go v0.3.11
10+
github.com/swaggest/refl v0.1.5
1111
github.com/swaggest/swgen v0.6.23
1212
gopkg.in/yaml.v2 v2.3.0
1313
)

go.sum

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
github.com/bool64/dev v0.1.0 h1:erHyUcwsuvZU8YvydZuZDCPR6CeQXALfiVl4lx7TW7o=
22
github.com/bool64/dev v0.1.0/go.mod h1:pn52JC52uSgpazChx9CeXyG+S3sW2V36HHoLNBbscdg=
3+
github.com/bool64/dev v0.1.2/go.mod h1:pn52JC52uSgpazChx9CeXyG+S3sW2V36HHoLNBbscdg=
4+
github.com/bool64/dev v0.1.5 h1:35/UOPpbQE2ol+amR3UCb9GtKgjbjPLGNX8Kpa5nqpQ=
5+
github.com/bool64/dev v0.1.5/go.mod h1:pn52JC52uSgpazChx9CeXyG+S3sW2V36HHoLNBbscdg=
36
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
47
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
58
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -66,15 +69,17 @@ github.com/swaggest/assertjson v1.3.0/go.mod h1:VR1/jzZHtROewu/SBEdFrBOBuNm1FO9L
6669
github.com/swaggest/jsonschema-go v0.2.1 h1:SVg+zXQ46w6ewR7jZDRVbpKKC+V7Y6Xn/teC2pRS2bM=
6770
github.com/swaggest/jsonschema-go v0.2.1/go.mod h1:QFauBdPTrU1UltwocM5FzOWnVjVVtcWkJWG3NlK9sV0=
6871
github.com/swaggest/jsonschema-go v0.2.4/go.mod h1:m4VV88Gbi7lCrt9ckJzigK1rMlEeFjdZUkJr1o5MnDE=
69-
github.com/swaggest/jsonschema-go v0.3.8 h1:QkRQXoO1jp9otq5i0blji6T2gVbsF0OfdiMHFuKeUwM=
70-
github.com/swaggest/jsonschema-go v0.3.8/go.mod h1:AQijowS82ZcUId9RStqvyGTxyhzYUiVDxelwQxEdOy8=
72+
github.com/swaggest/jsonschema-go v0.3.11 h1:rbYBJhAV2Uz1PB4iJyKbvzGO+o1FsdX60aLi+o67IHU=
73+
github.com/swaggest/jsonschema-go v0.3.11/go.mod h1:lJUbAc2E3OpGrrUGvayONeRBXsFJeedWTpm23jhOtKs=
7174
github.com/swaggest/openapi-go v0.1.3/go.mod h1:Zx4ZgJ7XvlFH9wCOHE7u8RAjLfiHAnCHeaD5kUDujVM=
7275
github.com/swaggest/refl v0.1.0 h1:mz3skba8ewcPmxDryM9rETv4fSuAX4GqNxTuDfPyUM0=
7376
github.com/swaggest/refl v0.1.0/go.mod h1:kmYWhxNEvjfRDdMRqpaR/vLULk/SotJs9HFUCIVMK8o=
7477
github.com/swaggest/refl v0.1.2 h1:BfxmzI4FabeaSHijDEObFB1qaDPevcDsRetAYxg30yw=
7578
github.com/swaggest/refl v0.1.2/go.mod h1:kmYWhxNEvjfRDdMRqpaR/vLULk/SotJs9HFUCIVMK8o=
7679
github.com/swaggest/refl v0.1.3 h1:cnzEBcCNhYeLPG8Yy9JQixUkxMDsF0mo0GyzblLWrjE=
7780
github.com/swaggest/refl v0.1.3/go.mod h1:kmYWhxNEvjfRDdMRqpaR/vLULk/SotJs9HFUCIVMK8o=
81+
github.com/swaggest/refl v0.1.5 h1:dsIhZmcVkNqsnnc4SZO7ezp7j8cTn+N/i8ND+0WxA/I=
82+
github.com/swaggest/refl v0.1.5/go.mod h1:qeng4aRKeRt8NYAuLpag+FY9A/pDMkSwe7zhXn6/LW4=
7883
github.com/swaggest/swgen v0.6.20 h1:mGg20AJQu18lkx5NfqCJ9rRpAZjMc4QlXVOIS3JB1Ck=
7984
github.com/swaggest/swgen v0.6.20/go.mod h1:ipkZNfwztgRfbOWUllLZawfmxXprT8flqNJO9XhoMwM=
8085
github.com/swaggest/swgen v0.6.23 h1:9COrDC1o34xJS0NeQ2js7qGfo/Rgs16xWT0pQRvGNyY=

openapi3/reflect.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,14 @@ func (r *Reflector) parseParametersIn(
241241
p.WithStyle(string(QueryParameterStyleForm)).WithExplode(true)
242242
}
243243

244+
// Check if query parameter is an object.
244245
if in == ParameterInQuery {
245-
if propertySchema.HasType(jsonschema.Object) {
246+
ps, err := r.Reflect(reflect.New(field.Type).Interface(), jsonschema.InlineRefs)
247+
if err != nil {
248+
return err
249+
}
250+
251+
if ps.HasType(jsonschema.Object) {
246252
p.WithStyle(string(QueryParameterStyleDeepObject)).WithExplode(true)
247253
}
248254
}

openapi3/reflect_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,50 @@ func TestReflector_SetupRequest_queryObject(t *testing.T) {
490490

491491
assertjson.Equal(t, expected, b, string(b))
492492
}
493+
494+
type namedType struct {
495+
InQuery labels `query:"in_query"`
496+
}
497+
498+
type labels map[int]float64
499+
500+
func TestReflector_SetupRequest_queryNamedObject(t *testing.T) {
501+
reflector := openapi3.Reflector{}
502+
op := openapi3.Operation{}
503+
504+
s := reflector.SpecEns()
505+
s.Info.Title = apiName
506+
s.Info.Version = apiVersion
507+
508+
require.NoError(t, reflector.SetupRequest(openapi3.OperationContext{
509+
Operation: &op,
510+
HTTPMethod: http.MethodGet,
511+
Input: new(namedType),
512+
}))
513+
require.NoError(t, s.AddOperation(http.MethodPost, "/somewhere", op))
514+
515+
b, err := assertjson.MarshalIndentCompact(s, "", " ", 100)
516+
assert.NoError(t, err)
517+
518+
expected := []byte(`{
519+
"openapi":"3.0.2","info":{"title":"SampleAPI","version":"1.2.3"},
520+
"paths":{
521+
"/somewhere":{
522+
"post":{
523+
"parameters":[
524+
{
525+
"name":"in_query","in":"query","style":"deepObject","explode":true,
526+
"schema":{"$ref":"#/components/schemas/Openapi3TestLabels"}
527+
}
528+
],
529+
"responses":{"204":{"description":"No Content"}}
530+
}
531+
}
532+
},
533+
"components":{
534+
"schemas":{"Openapi3TestLabels":{"type":"object","additionalProperties":{"type":"number"},"nullable":true}}
535+
}
536+
}`)
537+
538+
assertjson.Equal(t, expected, b, string(b))
539+
}

0 commit comments

Comments
 (0)