From c7535095fbfc8c45c2520b42a3b15b3c1d54a327 Mon Sep 17 00:00:00 2001 From: Philip Constantinou Date: Tue, 13 Dec 2022 20:36:01 -0800 Subject: [PATCH 1/2] Add support for form tags --- field_parser.go | 7 +++++++ operation.go | 1 + 2 files changed, 8 insertions(+) diff --git a/field_parser.go b/field_parser.go index beee28ac3..6af4f2753 100644 --- a/field_parser.go +++ b/field_parser.go @@ -67,6 +67,13 @@ func (ps *tagBaseFieldParser) ShouldSkip() bool { func (ps *tagBaseFieldParser) FieldName() (string, error) { var name string + + // use "form" tag over json tag + name = strings.TrimSpace(strings.Split(ps.tag.Get(formTag), ",")[0]) + if name != "" { + return name, nil + } + if ps.field.Tag != nil { // json:"tag,hoge" name = strings.TrimSpace(strings.Split(ps.tag.Get(jsonTag), ",")[0]) diff --git a/operation.go b/operation.go index 8cadc6216..297de8e7a 100644 --- a/operation.go +++ b/operation.go @@ -372,6 +372,7 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F } const ( + formTag = "form" jsonTag = "json" bindingTag = "binding" defaultTag = "default" From 76b4c25ccdc5c150d1b0181c2ab878192c7d3608 Mon Sep 17 00:00:00 2001 From: Philip Constantinou Date: Wed, 14 Dec 2022 08:07:29 -0800 Subject: [PATCH 2/2] Changed the order to improve backwards compatibility --- field_parser.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/field_parser.go b/field_parser.go index 6af4f2753..b040caafd 100644 --- a/field_parser.go +++ b/field_parser.go @@ -68,12 +68,6 @@ func (ps *tagBaseFieldParser) ShouldSkip() bool { func (ps *tagBaseFieldParser) FieldName() (string, error) { var name string - // use "form" tag over json tag - name = strings.TrimSpace(strings.Split(ps.tag.Get(formTag), ",")[0]) - if name != "" { - return name, nil - } - if ps.field.Tag != nil { // json:"tag,hoge" name = strings.TrimSpace(strings.Split(ps.tag.Get(jsonTag), ",")[0]) @@ -81,6 +75,12 @@ func (ps *tagBaseFieldParser) FieldName() (string, error) { if name != "" { return name, nil } + + // use "form" tag over json tag + name = strings.TrimSpace(strings.Split(ps.tag.Get(formTag), ",")[0]) + if name != "" { + return name, nil + } } if ps.field.Names == nil {