Skip to content

Commit

Permalink
Add JSON collectionFormat (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Mar 5, 2024
1 parent 6f9b857 commit d6a28d7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ For automated HTTP REST service framework built with this library please check [
* `csv` comma-separated values,
* `ssv` space-separated values,
* `pipes` pipe-separated values (`|`),
* `multi` ampersand-separated values (`&`),
* `multi` ampersand-separated values (`&`),
* `json` additionally to slices unpacks maps and structs,
* Flexible schema control with [`jsonschema-go`](https://github.com/swaggest/jsonschema-go#implementing-interfaces-on-a-type)

## Example
Expand Down
10 changes: 6 additions & 4 deletions openapi3/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,10 @@ func (r *Reflector) parseParametersIn(
Content: nil,
}

swg2CollectionFormat := ""
refl.ReadStringTag(field.Tag, "collectionFormat", &swg2CollectionFormat)
collectionFormat := ""
refl.ReadStringTag(field.Tag, "collectionFormat", &collectionFormat)

switch swg2CollectionFormat {
switch collectionFormat {
case "csv":
p.WithStyle(string(QueryParameterStyleForm)).WithExplode(false)
case "ssv":
Expand All @@ -463,7 +463,9 @@ func (r *Reflector) parseParametersIn(

// Check if parameter is an JSON encoded object.
property := reflect.New(field.Type).Interface()
if refl.HasTaggedFields(property, tagJSON) && !refl.HasTaggedFields(property, string(in)) {

if collectionFormat == "json" ||
(refl.HasTaggedFields(property, tagJSON) && !refl.HasTaggedFields(property, string(in))) {
propertySchema, err := r.Reflect(property,
openapi.WithOperationCtx(oc, false, in),
jsonschema.DefinitionsPrefix(componentsSchemas),
Expand Down
12 changes: 12 additions & 0 deletions openapi3/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ func TestReflector_AddOperation_request_jsonQuery(t *testing.T) {
One filter `query:"one"`
Two filter `query:"two"`
Three map[string]int `query:"three"`
Four map[string]int `query:"four" collectionFormat:"json"`
}

r := openapi3.Reflector{}
Expand Down Expand Up @@ -585,6 +586,17 @@ func TestReflector_AddOperation_request_jsonQuery(t *testing.T) {
{
"name":"three","in":"query","style":"deepObject","explode":true,
"schema":{"type":"object","additionalProperties":{"type":"integer"}}
},
{
"name":"four","in":"query",
"content":{
"application/json":{
"schema":{
"type":"object","additionalProperties":{"type":"integer"},
"nullable":true
}
}
}
}
],
"responses":{"204":{"description":"No Content"}}
Expand Down
9 changes: 5 additions & 4 deletions openapi31/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,10 @@ func (r *Reflector) parseParametersIn(
Content: nil,
}

swg2CollectionFormat := ""
refl.ReadStringTag(field.Tag, "collectionFormat", &swg2CollectionFormat)
collectionFormat := ""
refl.ReadStringTag(field.Tag, "collectionFormat", &collectionFormat)

switch swg2CollectionFormat {
switch collectionFormat {
case "csv":
p.WithStyle(ParameterStyleForm).WithExplode(false)
case "ssv":
Expand All @@ -408,7 +408,8 @@ func (r *Reflector) parseParametersIn(

// Check if parameter is an JSON encoded object.
property := reflect.New(field.Type).Interface()
if refl.HasTaggedFields(property, tagJSON) && !refl.HasTaggedFields(property, string(in)) { //nolint:nestif
if collectionFormat == "json" || //nolint:nestif
(refl.HasTaggedFields(property, tagJSON) && !refl.HasTaggedFields(property, string(in))) {
propertySchema, err := r.Reflect(property,
openapi.WithOperationCtx(oc, false, in),
jsonschema.DefinitionsPrefix(componentsSchemas),
Expand Down
12 changes: 12 additions & 0 deletions openapi31/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ func TestReflector_AddOperation_request_jsonQuery(t *testing.T) {
One filter `query:"one"`
Two filter `query:"two"`
Three map[string]int `query:"three"`
Four map[string]int `query:"four" collectionFormat:"json"`
}

r := openapi31.Reflector{}
Expand Down Expand Up @@ -609,6 +610,17 @@ func TestReflector_AddOperation_request_jsonQuery(t *testing.T) {
"type":["object","null"]
},
"style":"deepObject","explode":true
},
{
"name":"four","in":"query",
"content":{
"application/json":{
"schema":{
"additionalProperties":{"type":"integer"},
"type":["null","object"]
}
}
}
}
],
"responses":{"204":{"description":"No Content"}}
Expand Down

0 comments on commit d6a28d7

Please sign in to comment.