Skip to content

Commit 3505efe

Browse files
committed
test: add tests for generics used with function scoped types
1 parent 1b57679 commit 3505efe

File tree

5 files changed

+402
-0
lines changed

5 files changed

+402
-0
lines changed

generics_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ func TestParseGenericsPackageAlias(t *testing.T) {
134134
assert.Equal(t, string(expected), string(b))
135135
}
136136

137+
func TestParseGenericsFunctionScoped(t *testing.T) {
138+
t.Parallel()
139+
140+
searchDir := "testdata/generics_function_scoped"
141+
expected, err := os.ReadFile(filepath.Join(searchDir, "expected.json"))
142+
assert.NoError(t, err)
143+
144+
p := New()
145+
err = p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
146+
assert.NoError(t, err)
147+
b, err := json.MarshalIndent(p.swagger, "", " ")
148+
149+
assert.NoError(t, err)
150+
assert.Equal(t, string(expected), string(b))
151+
}
152+
137153
func TestParametrizeStruct(t *testing.T) {
138154
pd := PackagesDefinitions{
139155
packages: make(map[string]*PackageDefinitions),
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/swaggo/swag/testdata/generics_function_scoped/types"
7+
)
8+
9+
// @Summary Generic Response
10+
// @Produce json
11+
// @Success 200 {object} types.GenericResponse[api.GetGeneric.User]
12+
// @Success 201 {object} types.GenericResponse[api.GetGeneric.Post]
13+
// @Router / [get]
14+
func GetGeneric(w http.ResponseWriter, r *http.Request) {
15+
type User struct {
16+
Username int `json:"username"`
17+
Email string `json:"email"`
18+
}
19+
type Post struct {
20+
Slug int `json:"slug"`
21+
Title string `json:"title"`
22+
}
23+
24+
_ = types.GenericResponse[any]{}
25+
}
26+
27+
// @Summary Generic Response With Custom Type Names
28+
// @Produce json
29+
// @Success 200 {object} types.GenericResponse[api.GetGenericRenamed.User]
30+
// @Success 201 {object} types.GenericResponse[api.GetGenericRenamed.Post]
31+
// @Router /renamed [get]
32+
func GetGenericRenamed(w http.ResponseWriter, r *http.Request) {
33+
type User struct {
34+
Username int `json:"username"`
35+
Email string `json:"email"`
36+
} // @Name RenamedUserData
37+
type Post struct {
38+
Slug int `json:"slug"`
39+
Title string `json:"title"`
40+
} // @Name RenamedPostData
41+
42+
_ = types.GenericResponse[any]{}
43+
}
44+
45+
// @Summary Multiple Generic Response
46+
// @Produce json
47+
// @Success 200 {object} types.GenericMultiResponse[api.GetGenericMulti.MyStructA, api.GetGenericMulti.MyStructB]
48+
// @Success 201 {object} types.GenericMultiResponse[api.GetGenericMulti.MyStructB, api.GetGenericMulti.MyStructA]
49+
// @Router /multi [get]
50+
func GetGenericMulti(w http.ResponseWriter, r *http.Request) {
51+
type MyStructA struct {
52+
SomeFieldA string `json:"some_field_a"`
53+
}
54+
type MyStructB struct {
55+
SomeFieldB string `json:"some_field_b"`
56+
}
57+
58+
_ = types.GenericMultiResponse[any, any]{}
59+
}
60+
61+
// @Summary Multiple Generic Response With Custom Type Names
62+
// @Produce json
63+
// @Success 200 {object} types.GenericMultiResponse[api.GetGenericMultiRenamed.MyStructA, api.GetGenericMultiRenamed.MyStructB]
64+
// @Success 201 {object} types.GenericMultiResponse[api.GetGenericMultiRenamed.MyStructB, api.GetGenericMultiRenamed.MyStructA]
65+
// @Router /multi-renamed [get]
66+
func GetGenericMultiRenamed(w http.ResponseWriter, r *http.Request) {
67+
type MyStructA struct {
68+
SomeFieldA string `json:"some_field_a"`
69+
} // @Name NameForMyStructA
70+
type MyStructB struct {
71+
SomeFieldB string `json:"some_field_b"`
72+
} // @Name NameForMyStructB
73+
74+
_ = types.GenericMultiResponse[any, any]{}
75+
}
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
{
2+
"swagger": "2.0",
3+
"info": {
4+
"description": "This is a sample server.",
5+
"title": "Swagger Example API",
6+
"contact": {},
7+
"version": "1.0"
8+
},
9+
"host": "localhost:8080",
10+
"basePath": "/api",
11+
"paths": {
12+
"/": {
13+
"get": {
14+
"produces": [
15+
"application/json"
16+
],
17+
"summary": "Generic Response",
18+
"responses": {
19+
"200": {
20+
"description": "OK",
21+
"schema": {
22+
"$ref": "#/definitions/types.GenericResponse-api_GetGeneric_User"
23+
}
24+
},
25+
"201": {
26+
"description": "Created",
27+
"schema": {
28+
"$ref": "#/definitions/types.GenericResponse-api_GetGeneric_Post"
29+
}
30+
}
31+
}
32+
}
33+
},
34+
"/multi": {
35+
"get": {
36+
"produces": [
37+
"application/json"
38+
],
39+
"summary": "Multiple Generic Response",
40+
"responses": {
41+
"200": {
42+
"description": "OK",
43+
"schema": {
44+
"$ref": "#/definitions/types.GenericMultiResponse-api_GetGenericMulti_MyStructA-api_GetGenericMulti_MyStructB"
45+
}
46+
},
47+
"201": {
48+
"description": "Created",
49+
"schema": {
50+
"$ref": "#/definitions/types.GenericMultiResponse-api_GetGenericMulti_MyStructB-api_GetGenericMulti_MyStructA"
51+
}
52+
}
53+
}
54+
}
55+
},
56+
"/multi-renamed": {
57+
"get": {
58+
"produces": [
59+
"application/json"
60+
],
61+
"summary": "Multiple Generic Response With Custom Type Names",
62+
"responses": {
63+
"200": {
64+
"description": "OK",
65+
"schema": {
66+
"$ref": "#/definitions/types.GenericMultiResponse-NameForMyStructA-NameForMyStructB"
67+
}
68+
},
69+
"201": {
70+
"description": "Created",
71+
"schema": {
72+
"$ref": "#/definitions/types.GenericMultiResponse-NameForMyStructB-NameForMyStructA"
73+
}
74+
}
75+
}
76+
}
77+
},
78+
"/renamed": {
79+
"get": {
80+
"produces": [
81+
"application/json"
82+
],
83+
"summary": "Generic Response With Custom Type Names",
84+
"responses": {
85+
"200": {
86+
"description": "OK",
87+
"schema": {
88+
"$ref": "#/definitions/types.GenericResponse-RenamedUserData"
89+
}
90+
},
91+
"201": {
92+
"description": "Created",
93+
"schema": {
94+
"$ref": "#/definitions/types.GenericResponse-RenamedPostData"
95+
}
96+
}
97+
}
98+
}
99+
}
100+
},
101+
"definitions": {
102+
"NameForMyStructA": {
103+
"type": "object",
104+
"properties": {
105+
"some_field_a": {
106+
"type": "string"
107+
}
108+
}
109+
},
110+
"NameForMyStructB": {
111+
"type": "object",
112+
"properties": {
113+
"some_field_b": {
114+
"type": "string"
115+
}
116+
}
117+
},
118+
"RenamedPostData": {
119+
"type": "object",
120+
"properties": {
121+
"slug": {
122+
"type": "integer"
123+
},
124+
"title": {
125+
"type": "string"
126+
}
127+
}
128+
},
129+
"RenamedUserData": {
130+
"type": "object",
131+
"properties": {
132+
"email": {
133+
"type": "string"
134+
},
135+
"username": {
136+
"type": "integer"
137+
}
138+
}
139+
},
140+
"api.GetGeneric.Post": {
141+
"type": "object",
142+
"properties": {
143+
"slug": {
144+
"type": "integer"
145+
},
146+
"title": {
147+
"type": "string"
148+
}
149+
}
150+
},
151+
"api.GetGeneric.User": {
152+
"type": "object",
153+
"properties": {
154+
"email": {
155+
"type": "string"
156+
},
157+
"username": {
158+
"type": "integer"
159+
}
160+
}
161+
},
162+
"api.GetGenericMulti.MyStructA": {
163+
"type": "object",
164+
"properties": {
165+
"some_field_a": {
166+
"type": "string"
167+
}
168+
}
169+
},
170+
"api.GetGenericMulti.MyStructB": {
171+
"type": "object",
172+
"properties": {
173+
"some_field_b": {
174+
"type": "string"
175+
}
176+
}
177+
},
178+
"types.GenericMultiResponse-NameForMyStructA-NameForMyStructB": {
179+
"type": "object",
180+
"properties": {
181+
"data_t": {
182+
"$ref": "#/definitions/NameForMyStructA"
183+
},
184+
"data_x": {
185+
"$ref": "#/definitions/NameForMyStructB"
186+
},
187+
"status": {
188+
"type": "string"
189+
}
190+
}
191+
},
192+
"types.GenericMultiResponse-NameForMyStructB-NameForMyStructA": {
193+
"type": "object",
194+
"properties": {
195+
"data_t": {
196+
"$ref": "#/definitions/NameForMyStructB"
197+
},
198+
"data_x": {
199+
"$ref": "#/definitions/NameForMyStructA"
200+
},
201+
"status": {
202+
"type": "string"
203+
}
204+
}
205+
},
206+
"types.GenericMultiResponse-api_GetGenericMulti_MyStructA-api_GetGenericMulti_MyStructB": {
207+
"type": "object",
208+
"properties": {
209+
"data_t": {
210+
"$ref": "#/definitions/api.GetGenericMulti.MyStructA"
211+
},
212+
"data_x": {
213+
"$ref": "#/definitions/api.GetGenericMulti.MyStructB"
214+
},
215+
"status": {
216+
"type": "string"
217+
}
218+
}
219+
},
220+
"types.GenericMultiResponse-api_GetGenericMulti_MyStructB-api_GetGenericMulti_MyStructA": {
221+
"type": "object",
222+
"properties": {
223+
"data_t": {
224+
"$ref": "#/definitions/api.GetGenericMulti.MyStructB"
225+
},
226+
"data_x": {
227+
"$ref": "#/definitions/api.GetGenericMulti.MyStructA"
228+
},
229+
"status": {
230+
"type": "string"
231+
}
232+
}
233+
},
234+
"types.GenericResponse-RenamedPostData": {
235+
"type": "object",
236+
"properties": {
237+
"data": {
238+
"$ref": "#/definitions/RenamedPostData"
239+
},
240+
"status": {
241+
"type": "string"
242+
}
243+
}
244+
},
245+
"types.GenericResponse-RenamedUserData": {
246+
"type": "object",
247+
"properties": {
248+
"data": {
249+
"$ref": "#/definitions/RenamedUserData"
250+
},
251+
"status": {
252+
"type": "string"
253+
}
254+
}
255+
},
256+
"types.GenericResponse-api_GetGeneric_Post": {
257+
"type": "object",
258+
"properties": {
259+
"data": {
260+
"$ref": "#/definitions/api.GetGeneric.Post"
261+
},
262+
"status": {
263+
"type": "string"
264+
}
265+
}
266+
},
267+
"types.GenericResponse-api_GetGeneric_User": {
268+
"type": "object",
269+
"properties": {
270+
"data": {
271+
"$ref": "#/definitions/api.GetGeneric.User"
272+
},
273+
"status": {
274+
"type": "string"
275+
}
276+
}
277+
}
278+
}
279+
}

0 commit comments

Comments
 (0)