@@ -45,8 +45,10 @@ var simpleStructPropertiesMap = map[string]spec.Schema{
45
45
"jsonname2" : * spec .StringProperty (),
46
46
}
47
47
48
+ var simpleStructKey = schemaKey (reflect .TypeOf (simpleStruct {}))
49
+
48
50
var simpleStructMetadata = ObjectMetadata {
49
- ID : "simpleStruct" ,
51
+ ID : simpleStructKey ,
50
52
Properties : simpleStructPropertiesMap ,
51
53
Required : []string {"Prop1" , "propname" , "Prop5" , "jsonname2" },
52
54
AdditionalProperties : false ,
@@ -59,15 +61,17 @@ type complexStruct struct {
59
61
Prop3 * complexStruct `metadata:",optional"`
60
62
}
61
63
64
+ var complexStructKey = schemaKey (reflect .TypeOf (complexStruct {}))
65
+
62
66
var complexStructPropertiesMap = map [string ]spec.Schema {
63
67
"Prop0" : * spec .StringProperty (),
64
68
"Prop1" : * spec .StringProperty (),
65
- "Prop2" : * spec .RefSchema ("simpleStruct" ),
66
- "Prop3" : * spec .RefSchema ("complexStruct" ),
69
+ "Prop2" : * spec .RefSchema (simpleStructKey ),
70
+ "Prop3" : * spec .RefSchema (complexStructKey ),
67
71
}
68
72
69
73
var complexStructMetadata = ObjectMetadata {
70
- ID : "complexStruct" ,
74
+ ID : complexStructKey ,
71
75
Properties : complexStructPropertiesMap ,
72
76
Required : []string {"Prop0" , "Prop1" , "Prop2" },
73
77
AdditionalProperties : false ,
@@ -84,16 +88,18 @@ type superComplexStruct struct {
84
88
var superComplexStructPropertiesMap = map [string ]spec.Schema {
85
89
"Prop0" : * spec .StringProperty (),
86
90
"Prop1" : * spec .StringProperty (),
87
- "Prop2" : * spec .RefSchema ("simpleStruct" ),
88
- "Prop3" : * spec .RefSchema ("complexStruct" ),
89
- "Prop4" : * spec .ArrayProperty (spec .RefSchema ("complexStruct" )),
90
- "Prop5" : * spec .ArrayProperty (spec .RefSchema ("simpleStruct" )),
91
- "Prop6" : * spec .MapProperty (spec .RefSchema ("complexStruct" )),
92
- "Prop7" : * spec .MapProperty (spec .ArrayProperty (spec .RefSchema ("simpleStruct" ))),
91
+ "Prop2" : * spec .RefSchema (simpleStructKey ),
92
+ "Prop3" : * spec .RefSchema (complexStructKey ),
93
+ "Prop4" : * spec .ArrayProperty (spec .RefSchema (complexStructKey )),
94
+ "Prop5" : * spec .ArrayProperty (spec .RefSchema (simpleStructKey )),
95
+ "Prop6" : * spec .MapProperty (spec .RefSchema (complexStructKey )),
96
+ "Prop7" : * spec .MapProperty (spec .ArrayProperty (spec .RefSchema (simpleStructKey ))),
93
97
}
94
98
99
+ var superComplexStructKey = schemaKey (reflect .TypeOf (superComplexStruct {}))
100
+
95
101
var superComplexStructMetadata = ObjectMetadata {
96
- ID : "superComplexStruct" ,
102
+ ID : superComplexStructKey ,
97
103
Properties : superComplexStructPropertiesMap ,
98
104
Required : append (complexStructMetadata .Required , "Prop4" , "Prop5" , "Prop6" , "Prop7" ),
99
105
AdditionalProperties : false ,
@@ -201,18 +207,18 @@ func TestAddComponentIfNotExists(t *testing.T) {
201
207
202
208
components = new (ComponentMetadata )
203
209
components .Schemas = make (map [string ]ObjectMetadata )
204
- components .Schemas ["simpleStruct" ] = someObject
210
+ components .Schemas [simpleStructKey ] = someObject
205
211
206
212
err = addComponentIfNotExists (reflect .TypeOf (simpleStruct {}), components )
207
- assert .Nil (t , err , "should return nil for error when component of name already exists" )
213
+ assert .NoError (t , err , "should return nil for error when component of name already exists" )
208
214
assert .Equal (t , len (components .Schemas ), 1 , "should not have added a new component when one already exists" )
209
- _ , ok = components .Schemas ["simpleStruct" ].Properties ["some property" ]
215
+ _ , ok = components .Schemas [simpleStructKey ].Properties ["some property" ]
210
216
assert .True (t , ok , "should not overwrite existing component" )
211
217
212
218
err = addComponentIfNotExists (reflect .TypeOf (new (simpleStruct )), components )
213
219
assert .Nil (t , err , "should return nil for error when component of name already exists for pointer" )
214
220
assert .Equal (t , len (components .Schemas ), 1 , "should not have added a new component when one already exists for pointer" )
215
- _ , ok = components .Schemas ["simpleStruct" ].Properties ["some property" ]
221
+ _ , ok = components .Schemas [simpleStructKey ].Properties ["some property" ]
216
222
assert .True (t , ok , "should not overwrite existing component when already exists and pointer passed" )
217
223
218
224
err = addComponentIfNotExists (reflect .TypeOf (badStruct {}), components )
@@ -222,13 +228,13 @@ func TestAddComponentIfNotExists(t *testing.T) {
222
228
components .Schemas = nil
223
229
err = addComponentIfNotExists (reflect .TypeOf (simpleStruct {}), components )
224
230
assert .Nil (t , err , "should not error when adding new component when schemas not initialised" )
225
- assert .Equal (t , components .Schemas ["simpleStruct" ], simpleStructMetadata , "should set correct metadata for new component when schemas not initialised" )
231
+ assert .Equal (t , components .Schemas [simpleStructKey ], simpleStructMetadata , "should set correct metadata for new component when schemas not initialised" )
226
232
227
- delete (components .Schemas , "simpleStruct" )
233
+ delete (components .Schemas , simpleStructKey )
228
234
components .Schemas ["otherStruct" ] = someObject
229
235
err = addComponentIfNotExists (reflect .TypeOf (simpleStruct {}), components )
230
236
assert .Nil (t , err , "should not error when adding new component" )
231
- assert .Equal (t , components .Schemas ["simpleStruct" ], simpleStructMetadata , "should set correct metadata for new component" )
237
+ assert .Equal (t , components .Schemas [simpleStructKey ], simpleStructMetadata , "should set correct metadata for new component" )
232
238
assert .Equal (t , components .Schemas ["otherStruct" ], someObject , "should not affect existing components" )
233
239
}
234
240
@@ -247,21 +253,21 @@ func TestBuildStructSchema(t *testing.T) {
247
253
248
254
schema , err = buildStructSchema (reflect .TypeOf (simpleStruct {}), components , false )
249
255
assert .Nil (t , err , "should not return error when struct is good" )
250
- assert .Equal (t , schema , spec .RefSchema ("#/components/schemas/simpleStruct" ) , "should make schema ref to component" )
251
- _ , ok := components .Schemas ["simpleStruct" ]
256
+ assert .Equal (t , spec .RefSchema ("#/components/schemas/" + simpleStructKey ), schema , "should make schema ref to component" )
257
+ _ , ok := components .Schemas [simpleStructKey ]
252
258
assert .True (t , ok , "should have added component" )
253
259
254
260
schema , err = buildStructSchema (reflect .TypeOf (simpleStruct {}), components , true )
255
261
assert .Nil (t , err , "should not return error when struct is good" )
256
- assert .Equal (t , schema , spec .RefSchema ("simpleStruct" ) , "should make schema ref to component for nested ref" )
257
- _ , ok = components .Schemas ["simpleStruct" ]
262
+ assert .Equal (t , spec .RefSchema (simpleStructKey ), schema , "should make schema ref to component for nested ref" )
263
+ _ , ok = components .Schemas [simpleStructKey ]
258
264
assert .True (t , ok , "should have added component for nested ref" )
259
265
260
266
schema , err = buildStructSchema (reflect .TypeOf (new (simpleStruct )), components , false )
261
267
assert .Nil (t , err , "should not return error when pointer to struct is good" )
262
- assert .Equal (t , schema , spec .RefSchema ("#/components/schemas/simpleStruct" ) , "should make schema ref to component" )
268
+ assert .Equal (t , spec .RefSchema ("#/components/schemas/" + simpleStructKey ), schema , "should make schema ref to component" )
263
269
264
- _ , ok = components .Schemas ["simpleStruct" ]
270
+ _ , ok = components .Schemas [simpleStructKey ]
265
271
assert .True (t , ok , "should have use already added component" )
266
272
}
267
273
@@ -444,9 +450,9 @@ func TestGetSchema(t *testing.T) {
444
450
schema , err = GetSchema (reflect .TypeOf (simpleStruct {}), components )
445
451
446
452
assert .Nil (t , err , "should return nil when valid object" )
447
- assert .Equal (t , len (components .Schemas ), 1 , "should have added a new component" )
448
- assert .Equal (t , components .Schemas ["simpleStruct" ], simpleStructMetadata , "should have added correct metadata to components" )
449
- assert .Equal (t , schema , spec .RefSchema ("#/components/schemas/simpleStruct" ) )
453
+ assert .Equal (t , 1 , len (components .Schemas ), "should have added a new component" )
454
+ assert .Equal (t , simpleStructMetadata , components .Schemas [simpleStructKey ] , "should have added correct metadata to components" )
455
+ assert .Equal (t , spec .RefSchema ("#/components/schemas/" + simpleStructKey ), schema )
450
456
451
457
// should handle pointer to struct
452
458
components = new (ComponentMetadata )
@@ -455,9 +461,9 @@ func TestGetSchema(t *testing.T) {
455
461
schema , err = GetSchema (reflect .TypeOf (new (simpleStruct )), components )
456
462
457
463
assert .Nil (t , err , "should return nil when valid object" )
458
- assert .Equal (t , len (components .Schemas ), 1 , "should have added a new component" )
459
- assert .Equal (t , components .Schemas ["simpleStruct" ], simpleStructMetadata , "should have added correct metadata to components" )
460
- assert .Equal (t , schema , spec .RefSchema ("#/components/schemas/simpleStruct" ) )
464
+ assert .Equal (t , 1 , len (components .Schemas ), "should have added a new component" )
465
+ assert .Equal (t , simpleStructMetadata , components .Schemas [simpleStructKey ] , "should have added correct metadata to components" )
466
+ assert .Equal (t , spec .RefSchema ("#/components/schemas/" + simpleStructKey ), schema )
461
467
462
468
// Should handle an array of structs
463
469
components = new (ComponentMetadata )
@@ -466,9 +472,9 @@ func TestGetSchema(t *testing.T) {
466
472
schema , err = GetSchema (reflect .TypeOf ([1 ]simpleStruct {}), components )
467
473
468
474
assert .Nil (t , err , "should return nil when valid object" )
469
- assert .Equal (t , len (components .Schemas ), 1 , "should have added a new component" )
470
- assert .Equal (t , components .Schemas ["simpleStruct" ], simpleStructMetadata , "should have added correct metadata to components" )
471
- assert .Equal (t , schema , spec .ArrayProperty (spec .RefSchema ("#/components/schemas/simpleStruct" )) )
475
+ assert .Equal (t , 1 , len (components .Schemas ), "should have added a new component" )
476
+ assert .Equal (t , simpleStructMetadata , components .Schemas [simpleStructKey ] , "should have added correct metadata to components" )
477
+ assert .Equal (t , spec .ArrayProperty (spec .RefSchema ("#/components/schemas/" + simpleStructKey )), schema )
472
478
473
479
// Should handle a slice of structs
474
480
components = new (ComponentMetadata )
@@ -477,9 +483,9 @@ func TestGetSchema(t *testing.T) {
477
483
schema , err = GetSchema (reflect .TypeOf ([]simpleStruct {}), components )
478
484
479
485
assert .Nil (t , err , "should return nil when valid object" )
480
- assert .Equal (t , len (components .Schemas ), 1 , "should have added a new component" )
481
- assert .Equal (t , components .Schemas ["simpleStruct" ], simpleStructMetadata , "should have added correct metadata to components" )
482
- assert .Equal (t , schema , spec .ArrayProperty (spec .RefSchema ("#/components/schemas/simpleStruct" )) )
486
+ assert .Equal (t , 1 , len (components .Schemas ), "should have added a new component" )
487
+ assert .Equal (t , simpleStructMetadata , components .Schemas [simpleStructKey ] , "should have added correct metadata to components" )
488
+ assert .Equal (t , spec .ArrayProperty (spec .RefSchema ("#/components/schemas/" + simpleStructKey )), schema )
483
489
484
490
// Should handle a valid struct with struct property and add to components
485
491
components = new (ComponentMetadata )
@@ -488,10 +494,10 @@ func TestGetSchema(t *testing.T) {
488
494
schema , err = GetSchema (reflect .TypeOf (new (complexStruct )), components )
489
495
490
496
assert .Nil (t , err , "should return nil when valid object" )
491
- assert .Equal (t , len (components .Schemas ), 2 , "should have added two new components" )
492
- assert .Equal (t , components .Schemas ["simpleStruct" ], simpleStructMetadata , "should have added correct metadata to components for sub struct" )
493
- assert .Equal (t , components .Schemas ["complexStruct" ], complexStructMetadata , "should have added correct metadata to components for main struct" )
494
- assert .Equal (t , schema , spec .RefSchema ("#/components/schemas/complexStruct" ) )
497
+ assert .Equal (t , 2 , len (components .Schemas ), "should have added two new components" )
498
+ assert .Equal (t , simpleStructMetadata , components .Schemas [simpleStructKey ] , "should have added correct metadata to components for sub struct" )
499
+ assert .Equal (t , complexStructMetadata , components .Schemas [complexStructKey ] , "should have added correct metadata to components for main struct" )
500
+ assert .Equal (t , spec .RefSchema ("#/components/schemas/" + complexStructKey ), schema )
495
501
496
502
// Should handle a valid struct with struct properties of array, slice and map types
497
503
components = new (ComponentMetadata )
@@ -500,11 +506,11 @@ func TestGetSchema(t *testing.T) {
500
506
schema , err = GetSchema (reflect .TypeOf (new (superComplexStruct )), components )
501
507
502
508
assert .Nil (t , err , "should return nil when valid object" )
503
- assert .Equal (t , len (components .Schemas ), 3 , "should have added two new components" )
504
- assert .Equal (t , components .Schemas ["simpleStruct" ], simpleStructMetadata , "should have added correct metadata to components for sub struct" )
505
- assert .Equal (t , components .Schemas ["complexStruct" ], complexStructMetadata , "should have added correct metadata to components for sub struct" )
506
- assert .Equal (t , components .Schemas ["superComplexStruct" ], superComplexStructMetadata , "should have added correct metadata to components for main struct" )
507
- assert .Equal (t , schema , spec .RefSchema ("#/components/schemas/superComplexStruct" ) )
509
+ assert .Equal (t , 3 , len (components .Schemas ), "should have added two new components" )
510
+ assert .Equal (t , simpleStructMetadata , components .Schemas [simpleStructKey ] , "should have added correct metadata to components for sub struct" )
511
+ assert .Equal (t , complexStructMetadata , components .Schemas [complexStructKey ] , "should have added correct metadata to components for sub struct" )
512
+ assert .Equal (t , superComplexStructMetadata , components .Schemas [superComplexStructKey ] , "should have added correct metadata to components for main struct" )
513
+ assert .Equal (t , spec .RefSchema ("#/components/schemas/" + superComplexStructKey ), schema )
508
514
509
515
// Should return an error for a bad struct
510
516
components = new (ComponentMetadata )
0 commit comments