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
  • Loading branch information
tanenbaum committed Jan 15, 2024
1 parent e254a2a commit 42d478d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
4 changes: 4 additions & 0 deletions field_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -2096,6 +2096,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 @@ -2128,13 +2129,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 @@ -2153,6 +2160,9 @@ func TestParseParamStructCodeExample(t *testing.T) {
In: "header",
Required: true,
},
SimpleSchema: spec.SimpleSchema{
Type: "string",
},
}, spec.Parameter{
ParamProps: spec.ParamProps{
Name: "anotherHeader",
Expand All @@ -2163,6 +2173,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 @@ -189,6 +189,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 @@ -1511,6 +1512,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 42d478d

Please sign in to comment.