diff --git a/field_parser.go b/field_parser.go index 315faeea2..9b24e7872 100644 --- a/field_parser.go +++ b/field_parser.go @@ -98,7 +98,7 @@ func (ps *tagBaseFieldParser) FieldName() (string, error) { func (ps *tagBaseFieldParser) firstTagValue(tag string) string { if ps.field.Tag != nil { - return strings.TrimRight(strings.TrimSpace(strings.Split(ps.tag.Get(formTag), ",")[0]), "[]") + return strings.TrimRight(strings.TrimSpace(strings.Split(ps.tag.Get(tag), ",")[0]), "[]") } return "" } @@ -111,6 +111,10 @@ func (ps *tagBaseFieldParser) HeaderName() string { return ps.firstTagValue(headerTag) } +func (ps *tagBaseFieldParser) PathName() string { + return ps.firstTagValue(uriTag) +} + func toSnakeCase(in string) string { var ( runes = []rune(in) diff --git a/operation_test.go b/operation_test.go index 14526895a..fb43a446e 100644 --- a/operation_test.go +++ b/operation_test.go @@ -2098,6 +2098,7 @@ func TestParseParamStructCodeExample(t *testing.T) { if p.Name == param.Name { assert.Equal(t, param.ParamProps, p.ParamProps) assert.Equal(t, param.CommonValidations, p.CommonValidations) + assert.Equal(t, param.SimpleSchema, p.SimpleSchema) found = true break } @@ -2130,6 +2131,9 @@ func TestParseParamStructCodeExample(t *testing.T) { CommonValidations: spec.CommonValidations{ MaxLength: &maxLen, }, + SimpleSchema: spec.SimpleSchema{ + Type: "string", + }, }, spec.Parameter{ ParamProps: spec.ParamProps{ @@ -2137,6 +2141,9 @@ func TestParseParamStructCodeExample(t *testing.T) { Description: "B is another field", In: param, }, + SimpleSchema: spec.SimpleSchema{ + Type: "boolean", + }, }) }) } @@ -2155,6 +2162,9 @@ func TestParseParamStructCodeExample(t *testing.T) { In: "header", Required: true, }, + SimpleSchema: spec.SimpleSchema{ + Type: "string", + }, }, spec.Parameter{ ParamProps: spec.ParamProps{ Name: "anotherHeader", @@ -2165,6 +2175,41 @@ func TestParseParamStructCodeExample(t *testing.T) { Maximum: &max, Minimum: &min, }, + SimpleSchema: spec.SimpleSchema{ + Type: "integer", + }, + }) + }) + + t.Run("path struct", func(t *testing.T) { + operation := NewOperation(parser) + comment := `@Param path path structs.PathModel true "path params"` + err = operation.ParseComment(comment, ast) + assert.NoError(t, err) + + validateParameters(operation, + spec.Parameter{ + ParamProps: spec.ParamProps{ + Name: "id", + Description: "ID is the id", + In: "path", + Required: true, + }, + SimpleSchema: spec.SimpleSchema{ + Type: "integer", + }, + }, spec.Parameter{ + ParamProps: spec.ParamProps{ + Name: "name", + Description: "", + In: "path", + }, + CommonValidations: spec.CommonValidations{ + MaxLength: &maxLen, + }, + SimpleSchema: spec.SimpleSchema{ + Type: "string", + }, }) }) } diff --git a/parser.go b/parser.go index 0616b421a..6832d5886 100644 --- a/parser.go +++ b/parser.go @@ -190,6 +190,7 @@ type FieldParser interface { FieldName() (string, error) FormName() string HeaderName() string + PathName() string CustomSchema() (*spec.Schema, error) ComplementSchema(schema *spec.Schema) error IsRequired() (bool, error) @@ -1516,6 +1517,9 @@ func (parser *Parser) parseStructField(file *ast.File, field *ast.Field) (map[st if headerName := ps.HeaderName(); len(headerName) > 0 { schema.Extensions["header"] = headerName } + if pathName := ps.PathName(); len(pathName) > 0 { + schema.Extensions["path"] = pathName + } return map[string]spec.Schema{fieldName: *schema}, tagRequired, nil } diff --git a/testdata/param_structs/structs.go b/testdata/param_structs/structs.go index 3a457a962..2c8673a5d 100644 --- a/testdata/param_structs/structs.go +++ b/testdata/param_structs/structs.go @@ -3,7 +3,7 @@ package structs type FormModel struct { Foo string `form:"f" binding:"required" validate:"max=10"` // B is another field - B string + B bool } type AuthHeader struct { @@ -12,3 +12,9 @@ type AuthHeader struct { // AnotherHeader is another header AnotherHeader int `validate:"gte=0,lte=10"` } + +type PathModel struct { + // ID is the id + Identifier int `uri:"id" binding:"required"` + Name string `validate:"max=10"` +}