Skip to content

Commit c18f7f7

Browse files
authored
Prefer less-known field tags to json when deciding on param content type (#70)
1 parent 8a71b2d commit c18f7f7

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

openapi3/example_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ func ExampleReflector_SetRequest_queryObject() {
514514
Quux float64 `query:"quux"`
515515
Deeper struct {
516516
Val string `query:"val"`
517-
} `json:"deeper"`
517+
} `query:"deeper"`
518518
}
519519

520520
type req struct {
@@ -559,12 +559,12 @@ func ExampleReflector_SetRequest_queryObject() {
559559
// $ref: '#/components/schemas/QueryOpenapi3TestJsonFilter'
560560
// in: query
561561
// name: json_filter
562-
// - content:
563-
// application/json:
564-
// schema:
565-
// $ref: '#/components/schemas/QueryOpenapi3TestDeepObjectFilter'
562+
// - explode: true
566563
// in: query
567564
// name: deep_object_filter
565+
// schema:
566+
// $ref: '#/components/schemas/QueryOpenapi3TestDeepObjectFilter'
567+
// style: deepObject
568568
// - in: path
569569
// name: id
570570
// required: true
@@ -578,9 +578,15 @@ func ExampleReflector_SetRequest_queryObject() {
578578
// schemas:
579579
// QueryOpenapi3TestDeepObjectFilter:
580580
// properties:
581+
// baz:
582+
// type: boolean
581583
// deeper:
582-
// nullable: true
584+
// properties:
585+
// val:
586+
// type: string
583587
// type: object
588+
// quux:
589+
// type: number
584590
// type: object
585591
// QueryOpenapi3TestJsonFilter:
586592
// properties:

openapi3/reflect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func (r *Reflector) parseParametersIn(
309309

310310
// Check if parameter is an JSON encoded object.
311311
property := reflect.New(field.Type).Interface()
312-
if refl.HasTaggedFields(property, tagJSON) {
312+
if refl.HasTaggedFields(property, tagJSON) && !refl.HasTaggedFields(property, string(in)) {
313313
propertySchema, err := r.Reflect(property,
314314
r.withOperation(oc, false, string(in)),
315315
jsonschema.DefinitionsPrefix(definitionsPrefix),

openapi3/reflect_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,8 @@ func TestReflector_SetupRequest_form_only(t *testing.T) {
878878
func TestReflector_SetRequest_queryObject(t *testing.T) {
879879
reflector := openapi3.Reflector{}
880880

881+
// JSON object is only enabled when at least one `json` tag is available on top-level property,
882+
// and there are no `in` tags, e.g. `query`.
881883
type jsonFilter struct {
882884
Foo string `json:"foo"`
883885
Bar int `json:"bar"`
@@ -886,9 +888,11 @@ func TestReflector_SetRequest_queryObject(t *testing.T) {
886888
} `json:"deeper"`
887889
}
888890

891+
// Deep object structure may have `json` tags, they are ignored in presence of
892+
// at least one top-level field with a matching `in` tag, e.g. `query`.
889893
type deepObjectFilter struct {
890-
Baz bool `query:"baz"`
891-
Quux float64 `query:"quux"`
894+
Baz bool `json:"baz" query:"baz"`
895+
Quux float64 `json:"quux" query:"quux"`
892896
Deeper struct {
893897
Val string `query:"val"`
894898
} `query:"deeper"`

0 commit comments

Comments
 (0)