-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested_struct_test.go
More file actions
312 lines (258 loc) · 8.43 KB
/
nested_struct_test.go
File metadata and controls
312 lines (258 loc) · 8.43 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
package fiberoapi
import (
"encoding/json"
"io"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
)
// Define nested structs for testing
type Address struct {
Street string `json:"street" example:"123 Main St"`
City string `json:"city" example:"New York"`
Country string `json:"country" example:"USA"`
ZipCode string `json:"zip_code" example:"10001"`
}
type Contact struct {
Email string `json:"email" example:"user@example.com"`
Phone string `json:"phone" example:"+1234567890"`
}
type User struct {
ID int `json:"id" example:"1"`
Name string `json:"name" example:"John Doe"`
Address Address `json:"address"`
Contact Contact `json:"contact"`
}
type CreateUserRequest struct {
Name string `json:"name" validate:"required" example:"John Doe"`
Address Address `json:"address" validate:"required"`
Contact Contact `json:"contact" validate:"required"`
}
type CreateUserResponse struct {
User User `json:"user"`
Message string `json:"message" example:"User created successfully"`
}
func TestNestedStructSchemaGeneration(t *testing.T) {
app := fiber.New()
oapi := New(app)
// Add a route with nested structs
Post(oapi, "/users", func(c *fiber.Ctx, req *CreateUserRequest) (*CreateUserResponse, *ErrorResponse) {
user := User{
ID: 1,
Name: req.Name,
Address: req.Address,
Contact: req.Contact,
}
return &CreateUserResponse{
User: user,
Message: "User created successfully",
}, nil
}, OpenAPIOptions{
OperationID: "createUser",
Summary: "Create a new user",
Tags: []string{"users"},
})
// Setup documentation
oapi.SetupDocs()
// Test OpenAPI spec generation
req := httptest.NewRequest("GET", "/openapi.json", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if resp.StatusCode != 200 {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
}
// Parse the OpenAPI spec
body, _ := io.ReadAll(resp.Body)
var spec map[string]interface{}
if err := json.Unmarshal(body, &spec); err != nil {
t.Fatalf("Failed to parse OpenAPI JSON: %v", err)
}
// Check that components/schemas exists
components, ok := spec["components"].(map[string]interface{})
if !ok {
t.Fatal("Expected components section in OpenAPI spec")
}
schemas, ok := components["schemas"].(map[string]interface{})
if !ok {
t.Fatal("Expected schemas section in components")
}
// Test that all nested structs are present in schemas
expectedSchemas := []string{
"CreateUserRequest",
"CreateUserResponse",
"User",
"Address",
"Contact",
"ErrorResponse",
}
for _, schemaName := range expectedSchemas {
if _, exists := schemas[schemaName]; !exists {
t.Errorf("Expected schema '%s' to be present in OpenAPI spec", schemaName)
}
}
// Verify Address schema structure
addressSchema, ok := schemas["Address"].(map[string]interface{})
if !ok {
t.Fatal("Expected Address schema to be an object")
}
addressProperties, ok := addressSchema["properties"].(map[string]interface{})
if !ok {
t.Fatal("Expected Address schema to have properties")
}
expectedAddressFields := []string{"street", "city", "country", "zip_code"}
for _, field := range expectedAddressFields {
if _, exists := addressProperties[field]; !exists {
t.Errorf("Expected Address schema to have field '%s'", field)
}
}
// Verify Contact schema structure
contactSchema, ok := schemas["Contact"].(map[string]interface{})
if !ok {
t.Fatal("Expected Contact schema to be an object")
}
contactProperties, ok := contactSchema["properties"].(map[string]interface{})
if !ok {
t.Fatal("Expected Contact schema to have properties")
}
expectedContactFields := []string{"email", "phone"}
for _, field := range expectedContactFields {
if _, exists := contactProperties[field]; !exists {
t.Errorf("Expected Contact schema to have field '%s'", field)
}
}
// Verify User schema has references to nested structs
userSchema, ok := schemas["User"].(map[string]interface{})
if !ok {
t.Fatal("Expected User schema to be an object")
}
userProperties, ok := userSchema["properties"].(map[string]interface{})
if !ok {
t.Fatal("Expected User schema to have properties")
}
// Check that Address field has a $ref
addressField, ok := userProperties["address"].(map[string]interface{})
if !ok {
t.Fatal("Expected User schema to have address field")
}
if addressRef, exists := addressField["$ref"]; !exists || addressRef != "#/components/schemas/Address" {
t.Errorf("Expected User.address field to have $ref to Address schema, got %v", addressRef)
}
// Check that Contact field has a $ref
contactField, ok := userProperties["contact"].(map[string]interface{})
if !ok {
t.Fatal("Expected User schema to have contact field")
}
if contactRef, exists := contactField["$ref"]; !exists || contactRef != "#/components/schemas/Contact" {
t.Errorf("Expected User.contact field to have $ref to Contact schema, got %v", contactRef)
}
// Print the generated spec for manual inspection (optional)
if testing.Verbose() {
t.Logf("Generated OpenAPI spec:\n%s", string(body))
}
}
func TestDeeplyNestedStructs(t *testing.T) {
// Test for deeply nested structures
type InnerMost struct {
Value string `json:"value" example:"inner"`
}
type Middle struct {
Inner InnerMost `json:"inner"`
Name string `json:"name" example:"middle"`
}
type Outer struct {
Middle Middle `json:"middle"`
ID int `json:"id" example:"1"`
}
type DeepRequest struct {
Outer Outer `json:"outer"`
}
type DeepResponse struct {
Data Outer `json:"data"`
Success bool `json:"success" example:"true"`
}
app := fiber.New()
oapi := New(app)
Post(oapi, "/deep", func(c *fiber.Ctx, req *DeepRequest) (*DeepResponse, *ErrorResponse) {
return &DeepResponse{
Data: req.Outer,
Success: true,
}, nil
}, OpenAPIOptions{
OperationID: "testDeep",
Summary: "Test deeply nested structures",
})
oapi.SetupDocs()
req := httptest.NewRequest("GET", "/openapi.json", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
body, _ := io.ReadAll(resp.Body)
var spec map[string]interface{}
if err := json.Unmarshal(body, &spec); err != nil {
t.Fatalf("Failed to parse OpenAPI JSON: %v", err)
}
components := spec["components"].(map[string]interface{})
schemas := components["schemas"].(map[string]interface{})
// Check all nested structs are present
expectedSchemas := []string{"DeepRequest", "DeepResponse", "Outer", "Middle", "InnerMost", "ErrorResponse"}
for _, schemaName := range expectedSchemas {
if _, exists := schemas[schemaName]; !exists {
t.Errorf("Expected deeply nested schema '%s' to be present", schemaName)
}
}
}
func TestArrayOfNestedStructs(t *testing.T) {
type Item struct {
ID int `json:"id" example:"1"`
Name string `json:"name" example:"Item 1"`
}
type ListRequest struct {
Items []Item `json:"items"`
}
type ListResponse struct {
Items []Item `json:"items"`
Total int `json:"total" example:"10"`
}
app := fiber.New()
oapi := New(app)
Post(oapi, "/items", func(c *fiber.Ctx, req *ListRequest) (*ListResponse, *ErrorResponse) {
return &ListResponse{
Items: req.Items,
Total: len(req.Items),
}, nil
}, OpenAPIOptions{
OperationID: "createItems",
Summary: "Create multiple items",
})
oapi.SetupDocs()
req := httptest.NewRequest("GET", "/openapi.json", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
body, _ := io.ReadAll(resp.Body)
var spec map[string]interface{}
if err := json.Unmarshal(body, &spec); err != nil {
t.Fatalf("Failed to parse OpenAPI JSON: %v", err)
}
components := spec["components"].(map[string]interface{})
schemas := components["schemas"].(map[string]interface{})
// Check that Item struct is collected from array reference
if _, exists := schemas["Item"]; !exists {
t.Error("Expected Item schema to be present when referenced in array")
}
// Check that ListRequest has correct array schema with $ref
listRequestSchema := schemas["ListRequest"].(map[string]interface{})
properties := listRequestSchema["properties"].(map[string]interface{})
itemsField := properties["items"].(map[string]interface{})
if itemsField["type"] != "array" {
t.Error("Expected items field to be of type array")
}
itemsSchema := itemsField["items"].(map[string]interface{})
if itemsSchema["$ref"] != "#/components/schemas/Item" {
t.Errorf("Expected items array to reference Item schema, got %v", itemsSchema["$ref"])
}
}