Skip to content

Commit

Permalink
Merge 3034d07 into 19ddb4d
Browse files Browse the repository at this point in the history
  • Loading branch information
sdghchj committed Mar 13, 2023
2 parents 19ddb4d + 3034d07 commit e28806e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
10 changes: 8 additions & 2 deletions field_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -1388,6 +1389,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
}

Expand Down

0 comments on commit e28806e

Please sign in to comment.