Skip to content

Commit c7eb9f5

Browse files
committed
Operation param add path struct model support and tests
wip: fix merge
1 parent 7b8f6bd commit c7eb9f5

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

field_parser.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (ps *tagBaseFieldParser) FieldName() (string, error) {
9898

9999
func (ps *tagBaseFieldParser) firstTagValue(tag string) string {
100100
if ps.field.Tag != nil {
101-
return strings.TrimRight(strings.TrimSpace(strings.Split(ps.tag.Get(formTag), ",")[0]), "[]")
101+
return strings.TrimRight(strings.TrimSpace(strings.Split(ps.tag.Get(tag), ",")[0]), "[]")
102102
}
103103
return ""
104104
}
@@ -111,6 +111,10 @@ func (ps *tagBaseFieldParser) HeaderName() string {
111111
return ps.firstTagValue(headerTag)
112112
}
113113

114+
func (ps *tagBaseFieldParser) PathName() string {
115+
return ps.firstTagValue(uriTag)
116+
}
117+
114118
func toSnakeCase(in string) string {
115119
var (
116120
runes = []rune(in)

operation_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,7 @@ func TestParseParamStructCodeExample(t *testing.T) {
20982098
if p.Name == param.Name {
20992099
assert.Equal(t, param.ParamProps, p.ParamProps)
21002100
assert.Equal(t, param.CommonValidations, p.CommonValidations)
2101+
assert.Equal(t, param.SimpleSchema, p.SimpleSchema)
21012102
found = true
21022103
break
21032104
}
@@ -2130,13 +2131,19 @@ func TestParseParamStructCodeExample(t *testing.T) {
21302131
CommonValidations: spec.CommonValidations{
21312132
MaxLength: &maxLen,
21322133
},
2134+
SimpleSchema: spec.SimpleSchema{
2135+
Type: "string",
2136+
},
21332137
},
21342138
spec.Parameter{
21352139
ParamProps: spec.ParamProps{
21362140
Name: "b",
21372141
Description: "B is another field",
21382142
In: param,
21392143
},
2144+
SimpleSchema: spec.SimpleSchema{
2145+
Type: "boolean",
2146+
},
21402147
})
21412148
})
21422149
}
@@ -2155,6 +2162,9 @@ func TestParseParamStructCodeExample(t *testing.T) {
21552162
In: "header",
21562163
Required: true,
21572164
},
2165+
SimpleSchema: spec.SimpleSchema{
2166+
Type: "string",
2167+
},
21582168
}, spec.Parameter{
21592169
ParamProps: spec.ParamProps{
21602170
Name: "anotherHeader",
@@ -2165,6 +2175,41 @@ func TestParseParamStructCodeExample(t *testing.T) {
21652175
Maximum: &max,
21662176
Minimum: &min,
21672177
},
2178+
SimpleSchema: spec.SimpleSchema{
2179+
Type: "integer",
2180+
},
2181+
})
2182+
})
2183+
2184+
t.Run("path struct", func(t *testing.T) {
2185+
operation := NewOperation(parser)
2186+
comment := `@Param path path structs.PathModel true "path params"`
2187+
err = operation.ParseComment(comment, ast)
2188+
assert.NoError(t, err)
2189+
2190+
validateParameters(operation,
2191+
spec.Parameter{
2192+
ParamProps: spec.ParamProps{
2193+
Name: "id",
2194+
Description: "ID is the id",
2195+
In: "path",
2196+
Required: true,
2197+
},
2198+
SimpleSchema: spec.SimpleSchema{
2199+
Type: "integer",
2200+
},
2201+
}, spec.Parameter{
2202+
ParamProps: spec.ParamProps{
2203+
Name: "name",
2204+
Description: "",
2205+
In: "path",
2206+
},
2207+
CommonValidations: spec.CommonValidations{
2208+
MaxLength: &maxLen,
2209+
},
2210+
SimpleSchema: spec.SimpleSchema{
2211+
Type: "string",
2212+
},
21682213
})
21692214
})
21702215
}

parser.go

+4
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ type FieldParser interface {
190190
FieldName() (string, error)
191191
FormName() string
192192
HeaderName() string
193+
PathName() string
193194
CustomSchema() (*spec.Schema, error)
194195
ComplementSchema(schema *spec.Schema) error
195196
IsRequired() (bool, error)
@@ -1516,6 +1517,9 @@ func (parser *Parser) parseStructField(file *ast.File, field *ast.Field) (map[st
15161517
if headerName := ps.HeaderName(); len(headerName) > 0 {
15171518
schema.Extensions["header"] = headerName
15181519
}
1520+
if pathName := ps.PathName(); len(pathName) > 0 {
1521+
schema.Extensions["path"] = pathName
1522+
}
15191523

15201524
return map[string]spec.Schema{fieldName: *schema}, tagRequired, nil
15211525
}

testdata/param_structs/structs.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package structs
33
type FormModel struct {
44
Foo string `form:"f" binding:"required" validate:"max=10"`
55
// B is another field
6-
B string
6+
B bool
77
}
88

99
type AuthHeader struct {
@@ -12,3 +12,9 @@ type AuthHeader struct {
1212
// AnotherHeader is another header
1313
AnotherHeader int `validate:"gte=0,lte=10"`
1414
}
15+
16+
type PathModel struct {
17+
// ID is the id
18+
Identifier int `uri:"id" binding:"required"`
19+
Name string `validate:"max=10"`
20+
}

0 commit comments

Comments
 (0)