Skip to content

Commit 52b3343

Browse files
committed
Add test case to prove MaxElements correctly set on IntOrString
1 parent def2854 commit 52b3343

File tree

1 file changed

+39
-2
lines changed
  • staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model

1 file changed

+39
-2
lines changed

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model/schemas_test.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ func TestSchemaDeclType(t *testing.T) {
3434
if cust.TypeName() != "object" {
3535
t.Errorf("incorrect type name, got %v, wanted object", cust.TypeName())
3636
}
37-
if len(cust.Fields) != 4 {
38-
t.Errorf("incorrect number of fields, got %d, wanted 4", len(cust.Fields))
37+
if len(cust.Fields) != 5 {
38+
t.Errorf("incorrect number of fields, got %d, wanted 5", len(cust.Fields))
3939
}
4040
for _, f := range cust.Fields {
4141
prop, found := ts.Properties[f.Name]
@@ -70,6 +70,13 @@ func TestSchemaDeclType(t *testing.T) {
7070
}
7171
}
7272
}
73+
if prop.ValueValidation != nil && prop.ValueValidation.MaxLength != nil {
74+
if f.Type.MaxElements != 4*(*prop.ValueValidation.MaxLength) {
75+
// When converting maxLength to maxElements, it's based on the number of bytes.]
76+
// Worst case is that one rune is 4 bytes, so maxElements should be 4x maxLength.
77+
t.Errorf("field maxElements does not match property 4x maxLength. field: %s, maxElements: %d, maxLength: %d", f.Name, f.Type.MaxElements, *prop.ValueValidation.MaxLength)
78+
}
79+
}
7380
}
7481
if ts.ValueValidation != nil {
7582
for _, name := range ts.ValueValidation.Required {
@@ -137,6 +144,7 @@ func testSchema() *schema.Structural {
137144
// properties:
138145
// name:
139146
// type: string
147+
// maxLength: 256
140148
// nested:
141149
// type: object
142150
// properties:
@@ -166,6 +174,12 @@ func testSchema() *schema.Structural {
166174
// format: int64
167175
// default: 1
168176
// enum: [1,2,3]
177+
// intOrString:
178+
// x-kubernetes-int-or-string: true
179+
// anyOf:
180+
// - type: "integer"
181+
// - type: "string"
182+
// maxLength: 20
169183
ts := &schema.Structural{
170184
Generic: schema.Generic{
171185
Type: "object",
@@ -175,6 +189,9 @@ func testSchema() *schema.Structural {
175189
Generic: schema.Generic{
176190
Type: "string",
177191
},
192+
ValueValidation: &schema.ValueValidation{
193+
MaxLength: maxPtr(256),
194+
},
178195
},
179196
"value": {
180197
Generic: schema.Generic{
@@ -245,6 +262,26 @@ func testSchema() *schema.Structural {
245262
},
246263
},
247264
},
265+
"intOrString": {
266+
Extensions: schema.Extensions{
267+
XIntOrString: true,
268+
},
269+
ValueValidation: &schema.ValueValidation{
270+
MaxLength: maxPtr(20),
271+
AnyOf: []schema.NestedValueValidation{
272+
{
273+
ForbiddenGenerics: schema.Generic{
274+
Type: "integer",
275+
},
276+
},
277+
{
278+
ForbiddenGenerics: schema.Generic{
279+
Type: "string",
280+
},
281+
},
282+
},
283+
},
284+
},
248285
},
249286
}
250287
return ts

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy