From 3f0a4b03fffb8709efc62dee854e74991406306e Mon Sep 17 00:00:00 2001 From: sdghchj Date: Tue, 21 Mar 2023 06:35:21 +0800 Subject: [PATCH] support form tag (#1502) Signed-off-by: sdghchj --- field_parser.go | 10 ++++++++-- operation.go | 11 +++++++++-- parser.go | 8 ++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/field_parser.go b/field_parser.go index b040caafd..98ad0ddc7 100644 --- a/field_parser.go +++ b/field_parser.go @@ -71,13 +71,12 @@ func (ps *tagBaseFieldParser) FieldName() (string, error) { if ps.field.Tag != nil { // json:"tag,hoge" name = strings.TrimSpace(strings.Split(ps.tag.Get(jsonTag), ",")[0]) - if name != "" { return name, nil } // use "form" tag over json tag - name = strings.TrimSpace(strings.Split(ps.tag.Get(formTag), ",")[0]) + name = ps.FormName() if name != "" { return name, nil } @@ -97,6 +96,13 @@ func (ps *tagBaseFieldParser) FieldName() (string, error) { } } +func (ps *tagBaseFieldParser) FormName() string { + if ps.field.Tag != nil { + return strings.TrimSpace(strings.Split(ps.tag.Get(formTag), ",")[0]) + } + return "" +} + func toSnakeCase(in string) string { var ( runes = []rune(in) diff --git a/operation.go b/operation.go index 5f97a75e1..61b1f9c32 100644 --- a/operation.go +++ b/operation.go @@ -310,14 +310,21 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F continue } + var formName = name + if item.Schema.Extensions != nil { + if nameVal, ok := item.Schema.Extensions[formTag]; ok { + formName = nameVal.(string) + } + } + switch { case prop.Type[0] == ARRAY && prop.Items.Schema != nil && len(prop.Items.Schema.Type) > 0 && IsSimplePrimitiveType(prop.Items.Schema.Type[0]): - param = createParameter(paramType, prop.Description, name, prop.Type[0], prop.Items.Schema.Type[0], findInSlice(schema.Required, name), enums, operation.parser.collectionFormatInQuery) + param = createParameter(paramType, prop.Description, formName, prop.Type[0], prop.Items.Schema.Type[0], findInSlice(schema.Required, name), enums, operation.parser.collectionFormatInQuery) case IsSimplePrimitiveType(prop.Type[0]): - param = createParameter(paramType, prop.Description, name, PRIMITIVE, prop.Type[0], findInSlice(schema.Required, name), enums, operation.parser.collectionFormatInQuery) + param = createParameter(paramType, prop.Description, formName, PRIMITIVE, prop.Type[0], findInSlice(schema.Required, name), enums, operation.parser.collectionFormatInQuery) default: operation.parser.debug.Printf("skip field [%s] in %s is not supported type for %s", name, refType, paramType) diff --git a/parser.go b/parser.go index a233ea3d3..3480d971b 100644 --- a/parser.go +++ b/parser.go @@ -179,6 +179,7 @@ type FieldParserFactory func(ps *Parser, field *ast.Field) FieldParser type FieldParser interface { ShouldSkip() bool FieldName() (string, error) + FormName() string CustomSchema() (*spec.Schema, error) ComplementSchema(schema *spec.Schema) error IsRequired() (bool, error) @@ -1389,6 +1390,13 @@ func (parser *Parser) parseStructField(file *ast.File, field *ast.Field) (map[st tagRequired = append(tagRequired, fieldName) } + if formName := ps.FormName(); len(formName) > 0 { + if schema.Extensions == nil { + schema.Extensions = make(spec.Extensions) + } + schema.Extensions[formTag] = formName + } + return map[string]spec.Schema{fieldName: *schema}, tagRequired, nil }