@@ -38,7 +38,9 @@ var _ = Describe("General Schema Flattening", func() {
38
38
39
39
rootType = crd.TypeIdent {Name : "RootType" , Package : rootPkg }
40
40
subtypeWithRefs = crd.TypeIdent {Name : "SubtypeWithRefs" , Package : rootPkg }
41
+ leafAliasType = crd.TypeIdent {Name : "LeafAlias" , Package : rootPkg }
41
42
leafType = crd.TypeIdent {Name : "LeafType" , Package : otherPkg }
43
+ inPkgLeafType = crd.TypeIdent {Name : "InPkgLeafType" , Package : rootPkg }
42
44
)
43
45
44
46
BeforeEach (func () {
@@ -72,6 +74,76 @@ var _ = Describe("General Schema Flattening", func() {
72
74
}
73
75
})
74
76
77
+ Context ("when dealing with reference chains" , func () {
78
+ It ("should flatten them" , func () {
79
+ By ("setting up a RootType, LeafAlias --> Alias --> Int" )
80
+ toLeafAlias := crd .TypeRefLink ("" , leafAliasType .Name )
81
+ toLeaf := crd .TypeRefLink ("other" , leafType .Name )
82
+ fl .Parser .Schemata = map [crd.TypeIdent ]apiext.JSONSchemaProps {
83
+ rootType : apiext.JSONSchemaProps {
84
+ Properties : map [string ]apiext.JSONSchemaProps {
85
+ "refProp" : apiext.JSONSchemaProps {Ref : & toLeafAlias },
86
+ },
87
+ },
88
+ leafAliasType : apiext.JSONSchemaProps {Ref : & toLeaf },
89
+ leafType : apiext.JSONSchemaProps {
90
+ Type : "string" ,
91
+ Pattern : "^[abc]$" ,
92
+ },
93
+ }
94
+
95
+ By ("flattening the type hierarchy" )
96
+ // flattenAllOf to avoid the normalize the all-of forms to what we
97
+ // really want (instead of caring about nested all-ofs)
98
+ outSchema := crd .FlattenEmbedded (fl .FlattenType (rootType ), rootPkg )
99
+ Expect (rootPkg .Errors ).To (HaveLen (0 ))
100
+ Expect (otherPkg .Errors ).To (HaveLen (0 ))
101
+
102
+ By ("verifying that it was flattened to have no references" )
103
+ Expect (outSchema ).To (Equal (& apiext.JSONSchemaProps {
104
+ Properties : map [string ]apiext.JSONSchemaProps {
105
+ "refProp" : apiext.JSONSchemaProps {
106
+ Type : "string" , Pattern : "^[abc]$" ,
107
+ },
108
+ },
109
+ }))
110
+ })
111
+
112
+ It ("should not infinite-loop on circular references" , func () {
113
+ By ("setting up a RootType, LeafAlias --> Alias --> LeafAlias" )
114
+ toLeafAlias := crd .TypeRefLink ("" , leafAliasType .Name )
115
+ toLeaf := crd .TypeRefLink ("" , inPkgLeafType .Name )
116
+ fl .Parser .Schemata = map [crd.TypeIdent ]apiext.JSONSchemaProps {
117
+ rootType : apiext.JSONSchemaProps {
118
+ Properties : map [string ]apiext.JSONSchemaProps {
119
+ "refProp" : apiext.JSONSchemaProps {Ref : & toLeafAlias },
120
+ },
121
+ },
122
+ leafAliasType : apiext.JSONSchemaProps {Ref : & toLeaf },
123
+ inPkgLeafType : apiext.JSONSchemaProps {Ref : & toLeafAlias },
124
+ }
125
+
126
+ By ("flattening the type hierarchy" )
127
+ // flattenAllOf to avoid the normalize the all-of forms to what we
128
+ // really want (instead of caring about nested all-ofs)
129
+ outSchema := crd .FlattenEmbedded (fl .FlattenType (rootType ), rootPkg )
130
+
131
+ // This should *finish* to some degree, leaving the circular reference in
132
+ // place. It should be fine to error on circular references in the future, though.
133
+ Expect (rootPkg .Errors ).To (HaveLen (0 ))
134
+ Expect (otherPkg .Errors ).To (HaveLen (0 ))
135
+
136
+ By ("verifying that it was flattened to *something*" )
137
+ Expect (outSchema ).To (Equal (& apiext.JSONSchemaProps {
138
+ Properties : map [string ]apiext.JSONSchemaProps {
139
+ "refProp" : apiext.JSONSchemaProps {
140
+ Ref : & toLeafAlias ,
141
+ },
142
+ },
143
+ }))
144
+ })
145
+ })
146
+
75
147
It ("should flatten a hierarchy of references" , func () {
76
148
By ("setting up a series of types RootType --> SubtypeWithRef --> LeafType" )
77
149
toSubtype := crd .TypeRefLink ("" , subtypeWithRefs .Name )
0 commit comments