Skip to content

Commit c5b6972

Browse files
authored
Fix support for form tag (#57)
1 parent f5952f4 commit c5b6972

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

openapi3/reflect.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ func (r *Reflector) parseRequestBody(
152152
}
153153

154154
hasTaggedFields := refl.HasTaggedFields(input, tag)
155+
for _, t := range additionalTags {
156+
if hasTaggedFields {
157+
break
158+
}
159+
160+
hasTaggedFields = refl.HasTaggedFields(input, t)
161+
}
155162

156163
// Form data can not have map or array as body.
157164
if !hasTaggedFields && len(mapping) == 0 && tag != tagJSON {

openapi3/reflect_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,3 +855,48 @@ func TestReflector_SetupRequest_form(t *testing.T) {
855855
}
856856
}`), r.SpecEns())
857857
}
858+
859+
func TestReflector_SetupRequest_form_only(t *testing.T) {
860+
// Fields with `form` will be present as both `query` parameter and `formData` property.
861+
type req struct {
862+
Bar int `form:"bar"`
863+
Quux string `form:"quux"`
864+
}
865+
866+
r := openapi3.Reflector{}
867+
oc := openapi3.OperationContext{
868+
Operation: &openapi3.Operation{},
869+
Input: req{},
870+
}
871+
872+
require.NoError(t, r.SetupRequest(oc))
873+
require.NoError(t, r.SpecEns().AddOperation(http.MethodPost, "/foo", *oc.Operation))
874+
875+
assertjson.EqualMarshal(t, []byte(`{
876+
"openapi":"3.0.3","info":{"title":"","version":""},
877+
"paths":{
878+
"/foo":{
879+
"post":{
880+
"parameters":[
881+
{"name":"bar","in":"query","schema":{"type":"integer"}},
882+
{"name":"quux","in":"query","schema":{"type":"string"}}
883+
],
884+
"requestBody":{
885+
"content":{
886+
"application/x-www-form-urlencoded":{"schema":{"$ref":"#/components/schemas/FormDataOpenapi3TestReq"}}
887+
}
888+
},
889+
"responses":{"204":{"description":"No Content"}}
890+
}
891+
}
892+
},
893+
"components":{
894+
"schemas":{
895+
"FormDataOpenapi3TestReq":{
896+
"type":"object",
897+
"properties":{"bar":{"type":"integer"},"quux":{"type":"string"}}
898+
}
899+
}
900+
}
901+
}`), r.SpecEns())
902+
}

0 commit comments

Comments
 (0)