@@ -2,6 +2,7 @@ package swag
22
33import (
44 "encoding/json"
5+ "fmt"
56 "go/ast"
67 goparser "go/parser"
78 "go/token"
@@ -1177,11 +1178,17 @@ func TestOperation_ParseParamComment(t *testing.T) {
11771178 t .Parallel ()
11781179 for _ , paramType := range []string {"header" , "path" , "query" , "formData" } {
11791180 t .Run (paramType , func (t * testing.T ) {
1181+ // unknown object returns error
11801182 assert .Error (t , NewOperation (nil ).ParseComment (`@Param some_object ` + paramType + ` main.Object true "Some Object"` , nil ))
1183+
1184+ // verify objects are supported here
1185+ o := NewOperation (nil )
1186+ o .parser .addTestType ("main.TestObject" )
1187+ err := o .ParseComment (`@Param some_object ` + paramType + ` main.TestObject true "Some Object"` , nil )
1188+ assert .NoError (t , err )
11811189 })
11821190 }
11831191 })
1184-
11851192}
11861193
11871194// Test ParseParamComment Query Params
@@ -2067,6 +2074,146 @@ func TestParseParamCommentByExtensions(t *testing.T) {
20672074 assert .Equal (t , expected , string (b ))
20682075}
20692076
2077+ func TestParseParamStructCodeExample (t * testing.T ) {
2078+ t .Parallel ()
2079+
2080+ fset := token .NewFileSet ()
2081+ ast , err := goparser .ParseFile (fset , "operation_test.go" , `package swag
2082+ import structs "github.com/swaggo/swag/testdata/param_structs"
2083+ ` , goparser .ParseComments )
2084+ assert .NoError (t , err )
2085+
2086+ parser := New ()
2087+ err = parser .parseFile ("github.com/swaggo/swag/testdata/param_structs" , "testdata/param_structs/structs.go" , nil , ParseModels )
2088+ assert .NoError (t , err )
2089+ _ , err = parser .packages .ParseTypes ()
2090+ assert .NoError (t , err )
2091+
2092+ validateParameters := func (operation * Operation , params ... spec.Parameter ) {
2093+ assert .Equal (t , len (params ), len (operation .Parameters ))
2094+
2095+ for _ , param := range params {
2096+ found := false
2097+ for _ , p := range operation .Parameters {
2098+ if p .Name == param .Name {
2099+ assert .Equal (t , param .ParamProps , p .ParamProps )
2100+ assert .Equal (t , param .CommonValidations , p .CommonValidations )
2101+ assert .Equal (t , param .SimpleSchema , p .SimpleSchema )
2102+ found = true
2103+ break
2104+ }
2105+ }
2106+ assert .True (t , found , "found parameter %s" , param .Name )
2107+ }
2108+ }
2109+
2110+ // values used in validation checks
2111+ max := float64 (10 )
2112+ maxLen := int64 (10 )
2113+ min := float64 (0 )
2114+
2115+ // query and form behave the same
2116+ for _ , param := range []string {"query" , "formData" } {
2117+ t .Run (param + " struct" , func (t * testing.T ) {
2118+ operation := NewOperation (parser )
2119+ comment := fmt .Sprintf (`@Param model %s structs.FormModel true "query params"` , param )
2120+ err = operation .ParseComment (comment , ast )
2121+ assert .NoError (t , err )
2122+
2123+ validateParameters (operation ,
2124+ spec.Parameter {
2125+ ParamProps : spec.ParamProps {
2126+ Name : "f" ,
2127+ Description : "" ,
2128+ In : param ,
2129+ Required : true ,
2130+ },
2131+ CommonValidations : spec.CommonValidations {
2132+ MaxLength : & maxLen ,
2133+ },
2134+ SimpleSchema : spec.SimpleSchema {
2135+ Type : "string" ,
2136+ },
2137+ },
2138+ spec.Parameter {
2139+ ParamProps : spec.ParamProps {
2140+ Name : "b" ,
2141+ Description : "B is another field" ,
2142+ In : param ,
2143+ },
2144+ SimpleSchema : spec.SimpleSchema {
2145+ Type : "boolean" ,
2146+ },
2147+ })
2148+ })
2149+ }
2150+
2151+ t .Run ("header struct" , func (t * testing.T ) {
2152+ operation := NewOperation (parser )
2153+ comment := `@Param auth header structs.AuthHeader true "auth header"`
2154+ err = operation .ParseComment (comment , ast )
2155+ assert .NoError (t , err )
2156+
2157+ validateParameters (operation ,
2158+ spec.Parameter {
2159+ ParamProps : spec.ParamProps {
2160+ Name : "X-Auth-Token" ,
2161+ Description : "Token is the auth token" ,
2162+ In : "header" ,
2163+ Required : true ,
2164+ },
2165+ SimpleSchema : spec.SimpleSchema {
2166+ Type : "string" ,
2167+ },
2168+ }, spec.Parameter {
2169+ ParamProps : spec.ParamProps {
2170+ Name : "anotherHeader" ,
2171+ Description : "AnotherHeader is another header" ,
2172+ In : "header" ,
2173+ },
2174+ CommonValidations : spec.CommonValidations {
2175+ Maximum : & max ,
2176+ Minimum : & min ,
2177+ },
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+ },
2213+ })
2214+ })
2215+ }
2216+
20702217func TestParseIdComment (t * testing.T ) {
20712218 t .Parallel ()
20722219
0 commit comments