Skip to content

Commit

Permalink
Operation param add path struct model support and tests
Browse files Browse the repository at this point in the history
wip: fix merge
  • Loading branch information
tanenbaum committed Jan 21, 2024
1 parent 7b8f6bd commit c7eb9f5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
6 changes: 5 additions & 1 deletion field_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
Expand All @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -2130,13 +2131,19 @@ func TestParseParamStructCodeExample(t *testing.T) {
CommonValidations: spec.CommonValidations{
MaxLength: &maxLen,
},
SimpleSchema: spec.SimpleSchema{
Type: "string",
},
},
spec.Parameter{
ParamProps: spec.ParamProps{
Name: "b",
Description: "B is another field",
In: param,
},
SimpleSchema: spec.SimpleSchema{
Type: "boolean",
},
})
})
}
Expand All @@ -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",
Expand All @@ -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",
},
})
})
}
Expand Down
4 changes: 4 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 7 additions & 1 deletion testdata/param_structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"`
}

0 comments on commit c7eb9f5

Please sign in to comment.