-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenapi_test.go
More file actions
407 lines (359 loc) · 11.1 KB
/
openapi_test.go
File metadata and controls
407 lines (359 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package nimbus
import (
"net/http"
"testing"
)
type TestAPIUser struct {
Name string `json:"name" validate:"required,minlen=2"`
Email string `json:"email" validate:"required,email"`
Age int `json:"age" validate:"min=18"`
}
type TestAPIQuery struct {
Page int `json:"page" validate:"min=1"`
Limit int `json:"limit" validate:"min=1,max=100"`
Query string `json:"query" validate:"minlen=2"`
}
func TestGenerateOpenAPI(t *testing.T) {
router := NewRouter()
userSchema := NewSchema(TestAPIUser{})
querySchema := NewSchema(TestAPIQuery{})
// Add some routes with metadata
router.AddRoute(http.MethodGet, "/users", func(ctx *Context) (any, int, error) {
return nil, http.StatusOK, nil
})
router.Route("GET", "/users").WithDoc(RouteMetadata{
Summary: "List users",
Description: "Get a list of all users",
Tags: []string{"users"},
QuerySchema: querySchema,
})
router.AddRoute(http.MethodPost, "/users", func(ctx *Context) (any, int, error) {
return nil, http.StatusOK, nil
})
router.Route("POST", "/users").WithDoc(RouteMetadata{
Summary: "Create user",
Description: "Create a new user",
Tags: []string{"users"},
RequestSchema: userSchema,
})
router.AddRoute(http.MethodGet, "/users/:id", func(ctx *Context) (any, int, error) {
return nil, http.StatusOK, nil
})
router.Route("GET", "/users/:id").WithDoc(RouteMetadata{
Summary: "Get user by ID",
Tags: []string{"users"},
})
// Generate OpenAPI spec
config := OpenAPIConfig{
Title: "Test API",
Description: "A test API",
Version: "1.0.0",
}
spec := router.GenerateOpenAPI(config)
// Verify basic structure
if spec.OpenAPI != "3.0.3" {
t.Errorf("Expected OpenAPI version 3.0.3, got %s", spec.OpenAPI)
}
if spec.Info.Title != "Test API" {
t.Errorf("Expected title 'Test API', got '%s'", spec.Info.Title)
}
if spec.Info.Version != "1.0.0" {
t.Errorf("Expected version '1.0.0', got '%s'", spec.Info.Version)
}
// Verify paths were generated
if len(spec.Paths) == 0 {
t.Error("Expected paths to be generated")
}
// Verify /users path exists
usersPath, ok := spec.Paths["/users"]
if !ok {
t.Error("Expected /users path to be present")
}
// Verify GET /users
if usersPath.GET == nil {
t.Error("Expected GET operation for /users")
} else {
if usersPath.GET.Summary != "List users" {
t.Errorf("Expected summary 'List users', got '%s'", usersPath.GET.Summary)
}
if len(usersPath.GET.Tags) != 1 || usersPath.GET.Tags[0] != "users" {
t.Errorf("Expected tags [users], got %v", usersPath.GET.Tags)
}
if len(usersPath.GET.Parameters) == 0 {
t.Error("Expected query parameters to be present")
}
}
// Verify POST /users
if usersPath.POST == nil {
t.Error("Expected POST operation for /users")
} else {
if usersPath.POST.Summary != "Create user" {
t.Errorf("Expected summary 'Create user', got '%s'", usersPath.POST.Summary)
}
if usersPath.POST.RequestBody == nil {
t.Error("Expected request body for POST /users")
}
}
// Verify /users/{id} path exists (with path param conversion)
userIDPath, ok := spec.Paths["/users/{id}"]
if !ok {
t.Error("Expected /users/{id} path to be present")
}
// Verify path parameter
if userIDPath.GET != nil {
foundParam := false
for _, param := range userIDPath.GET.Parameters {
if param.Name == "id" && param.In == "path" && param.Required {
foundParam = true
break
}
}
if !foundParam {
t.Error("Expected path parameter 'id' to be present and required")
}
}
// Verify components/schemas were generated
if len(spec.Components.Schemas) == 0 {
t.Error("Expected schemas to be generated in components")
}
}
func TestConvertPathParams(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"/users/:id", "/users/{id}"},
{"/posts/:postId/comments/:commentId", "/posts/{postId}/comments/{commentId}"},
{"/users", "/users"},
{"/", "/"},
}
for _, tt := range tests {
result := convertPathParams(tt.input)
if result != tt.expected {
t.Errorf("convertPathParams(%s): expected %s, got %s", tt.input, tt.expected, result)
}
}
}
func TestExtractPathParams(t *testing.T) {
tests := []struct {
pattern string
expected []string
}{
{"/users/:id", []string{"id"}},
{"/posts/:postId/comments/:commentId", []string{"postId", "commentId"}},
{"/users", []string{}},
{"/api/v1/:resource/:id", []string{"resource", "id"}},
}
for _, tt := range tests {
result := extractPathParams(tt.pattern)
if len(result) != len(tt.expected) {
t.Errorf("extractPathParams(%s): expected %v, got %v", tt.pattern, tt.expected, result)
continue
}
for i, param := range result {
if param != tt.expected[i] {
t.Errorf("extractPathParams(%s): expected %v, got %v", tt.pattern, tt.expected, result)
break
}
}
}
}
func TestGenerateOperationID(t *testing.T) {
tests := []struct {
method string
pattern string
expected string
}{
{"GET", "/users", "getUsers"},
{"POST", "/users", "postUsers"},
{"GET", "/users/:id", "getUsersById"},
{"DELETE", "/api/posts/:id", "deleteApiPostsById"},
{"PUT", "/users/:userId/posts/:postId", "putUsersByUserIdPostsByPostId"},
}
for _, tt := range tests {
result := generateOperationID(tt.method, tt.pattern)
if result != tt.expected {
t.Errorf("generateOperationID(%s, %s): expected %s, got %s",
tt.method, tt.pattern, tt.expected, result)
}
}
}
func TestSchemaToOpenAPISchema(t *testing.T) {
userSchema := NewSchema(TestAPIUser{})
openAPISchema := schemaToOpenAPISchema(userSchema)
if openAPISchema.Type != "object" {
t.Errorf("Expected type 'object', got '%s'", openAPISchema.Type)
}
// Check name field
if nameSchema, ok := openAPISchema.Properties["name"]; ok {
if nameSchema.Type != "string" {
t.Errorf("Expected name type 'string', got '%s'", nameSchema.Type)
}
if nameSchema.MinLength == nil || *nameSchema.MinLength != 2 {
t.Error("Expected name to have minLength of 2")
}
} else {
t.Error("Expected 'name' property to be present")
}
// Check email field
if emailSchema, ok := openAPISchema.Properties["email"]; ok {
if emailSchema.Format != "email" {
t.Errorf("Expected email format 'email', got '%s'", emailSchema.Format)
}
} else {
t.Error("Expected 'email' property to be present")
}
// Check age field
if ageSchema, ok := openAPISchema.Properties["age"]; ok {
if ageSchema.Type != "integer" {
t.Errorf("Expected age type 'integer', got '%s'", ageSchema.Type)
}
if ageSchema.Minimum == nil || *ageSchema.Minimum != 18 {
t.Error("Expected age to have minimum of 18")
}
} else {
t.Error("Expected 'age' property to be present")
}
// Check required fields
requiredCount := 0
for _, field := range openAPISchema.Required {
if field == "name" || field == "email" {
requiredCount++
}
}
if requiredCount != 2 {
t.Errorf("Expected 2 required fields (name, email), found %d", requiredCount)
}
}
func TestSchemaToQueryParameters(t *testing.T) {
querySchema := NewSchema(TestAPIQuery{})
params := schemaToQueryParameters(querySchema)
if len(params) != 3 {
t.Errorf("Expected 3 query parameters, got %d", len(params))
}
// Check that all params are query params
for _, param := range params {
if param.In != "query" {
t.Errorf("Expected param.In to be 'query', got '%s'", param.In)
}
}
// Find and check page parameter
foundPage := false
for _, param := range params {
if param.Name == "page" {
foundPage = true
if param.Schema.Type != "integer" {
t.Errorf("Expected page type 'integer', got '%s'", param.Schema.Type)
}
if param.Schema.Minimum == nil || *param.Schema.Minimum != 1 {
t.Error("Expected page minimum to be 1")
}
}
}
if !foundPage {
t.Error("Expected to find 'page' parameter")
}
}
// TestStaticAndDynamicRoutes verifies that both static routes and dynamic routes
// are properly collected in OpenAPI spec generation
func TestStaticAndDynamicRoutes(t *testing.T) {
router := NewRouter()
// Add static routes (stored in exactRoutes map)
router.AddRoute(http.MethodGet, "/health", func(ctx *Context) (any, int, error) {
return map[string]string{"status": "ok"}, http.StatusOK, nil
})
router.Route("GET", "/health").WithDoc(RouteMetadata{
Summary: "Health check",
Tags: []string{"system"},
})
router.AddRoute(http.MethodGet, "/api/status", func(ctx *Context) (any, int, error) {
return map[string]string{"status": "running"}, http.StatusOK, nil
})
router.Route("GET", "/api/status").WithDoc(RouteMetadata{
Summary: "API status",
Tags: []string{"system"},
})
// Add dynamic routes (stored in trees)
router.AddRoute(http.MethodGet, "/users/:id", func(ctx *Context) (any, int, error) {
return nil, http.StatusOK, nil
})
router.Route("GET", "/users/:id").WithDoc(RouteMetadata{
Summary: "Get user by ID",
Tags: []string{"users"},
})
router.AddRoute(http.MethodGet, "/posts/:postId/comments/:commentId", func(ctx *Context) (any, int, error) {
return nil, http.StatusOK, nil
})
router.Route("GET", "/posts/:postId/comments/:commentId").WithDoc(RouteMetadata{
Summary: "Get comment",
Tags: []string{"comments"},
})
// Generate OpenAPI spec
config := OpenAPIConfig{
Title: "Test API",
Description: "Testing static and dynamic routes",
Version: "1.0.0",
}
spec := router.GenerateOpenAPI(config)
// Verify all routes are present
expectedPaths := map[string]bool{
"/health": false,
"/api/status": false,
"/users/{id}": false,
"/posts/{postId}/comments/{commentId}": false,
}
for path := range spec.Paths {
if _, exists := expectedPaths[path]; exists {
expectedPaths[path] = true
}
}
// Check that all expected paths were found
for path, found := range expectedPaths {
if !found {
t.Errorf("Expected path '%s' to be present in OpenAPI spec", path)
}
}
// Verify static route has correct metadata
if healthPath, ok := spec.Paths["/health"]; ok {
if healthPath.GET == nil {
t.Error("Expected GET operation for /health")
} else if healthPath.GET.Summary != "Health check" {
t.Errorf("Expected summary 'Health check', got '%s'", healthPath.GET.Summary)
}
}
// Verify dynamic route has path parameters
if userPath, ok := spec.Paths["/users/{id}"]; ok {
if userPath.GET == nil {
t.Error("Expected GET operation for /users/{id}")
} else {
foundParam := false
for _, param := range userPath.GET.Parameters {
if param.Name == "id" && param.In == "path" && param.Required {
foundParam = true
break
}
}
if !foundParam {
t.Error("Expected path parameter 'id' to be present and required")
}
}
}
// Verify route with multiple path parameters
if commentPath, ok := spec.Paths["/posts/{postId}/comments/{commentId}"]; ok {
if commentPath.GET == nil {
t.Error("Expected GET operation for /posts/{postId}/comments/{commentId}")
} else {
paramCount := 0
for _, param := range commentPath.GET.Parameters {
if param.In == "path" && param.Required {
if param.Name == "postId" || param.Name == "commentId" {
paramCount++
}
}
}
if paramCount != 2 {
t.Errorf("Expected 2 path parameters, found %d", paramCount)
}
}
}
}