Skip to content

Commit 60193b9

Browse files
committed
Avoid recrusive calls on Xvalidation pointer type alias with the same context
1 parent be71a1e commit 60193b9

File tree

4 files changed

+23
-24
lines changed

4 files changed

+23
-24
lines changed

pkg/crd/markers/validation.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -666,14 +666,6 @@ func (m XValidation) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
666666
}
667667
}
668668

669-
// Check if this validation rule already exists to prevent duplication
670-
for _, existingRule := range schema.XValidations {
671-
if existingRule.Rule == m.Rule {
672-
// Rule already exists, don't add it again
673-
return nil
674-
}
675-
}
676-
677669
schema.XValidations = append(schema.XValidations, apiext.ValidationRule{
678670
Rule: m.Rule,
679671
Message: m.Message,

pkg/crd/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func typeToSchema(ctx *schemaContext, rawType ast.Expr) *apiext.JSONSchemaProps
231231
case *ast.MapType:
232232
props = mapToSchema(ctx, expr)
233233
case *ast.StarExpr:
234-
props = typeToSchema(ctx, expr.X)
234+
props = typeToSchema(ctx.ForInfo(&markers.TypeInfo{}), expr.X)
235235
case *ast.StructType:
236236
props = structToSchema(ctx, expr)
237237
default:

pkg/crd/testdata/oneof/types.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ type OneofSpec struct {
3737

3838
TypeWithAllOneOf *TypeWithAllOneofs `json:"typeWithAllOneOf,omitempty"`
3939

40-
// The validation rule on TypeWithOneofs should not be duplicated.
41-
SameTypeWithOneof *TypeWithOneofs `json:"sameTypeWithOneof,omitempty"`
40+
FirstCustomTypeAlias CustomTypeAlias `json:"firstCustomTypeAlias,omitempty"`
41+
42+
// This verifies if the custom type alias XValidation is not duplicated.
43+
SecondCustomTypeAlias CustomTypeAlias `json:"secondCustomTypeAlias,omitempty"`
4244
}
4345

4446
// +kubebuilder:validation:XValidation:message="only one of foo|bar may be set",rule="!(has(self.foo) && has(self.bar))"
@@ -98,6 +100,10 @@ type TypeWithAllOneofs struct {
98100
F *string `json:"f,omitempty"`
99101
}
100102

103+
// CustomTypeAlias is a custom alias
104+
// +kubebuilder:validation:XValidation:rule="self >= 100 && self <= 1000",message="invalid CustomTypeAlias value"
105+
type CustomTypeAlias *int32
106+
101107
// Oneof is the Schema for the Oneof API
102108
type Oneof struct {
103109
/*

pkg/crd/testdata/testdata.kubebuilder.io_oneofs.yaml

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ spec:
3636
spec:
3737
description: OneofSpec is the spec for the oneofs API.
3838
properties:
39+
firstCustomTypeAlias:
40+
description: CustomTypeAlias is a custom alias
41+
format: int32
42+
type: integer
43+
x-kubernetes-validations:
44+
- message: invalid CustomTypeAlias value
45+
rule: self >= 100 && self <= 1000
3946
firstTypeWithExactOneof:
4047
properties:
4148
bar:
@@ -60,20 +67,14 @@ spec:
6067
- message: at most one of the fields in [foo bar] may be set
6168
rule: '[has(self.foo),has(self.bar)].filter(x,x==true).size() <=
6269
1'
63-
sameTypeWithOneof:
64-
description: The validation rule on TypeWithOneofs should not be duplicated.
65-
properties:
66-
bar:
67-
type: string
68-
foo:
69-
type: string
70-
type: object
70+
secondCustomTypeAlias:
71+
description: This verifies if the custom type alias XValidation is
72+
not duplicated.
73+
format: int32
74+
type: integer
7175
x-kubernetes-validations:
72-
- message: only one of foo|bar may be set
73-
rule: '!(has(self.foo) && has(self.bar))'
74-
- message: at most one of the fields in [foo bar] may be set
75-
rule: '[has(self.foo),has(self.bar)].filter(x,x==true).size() <=
76-
1'
76+
- message: invalid CustomTypeAlias value
77+
rule: self >= 100 && self <= 1000
7778
secondTypeWithExactOneof:
7879
properties:
7980
a:

0 commit comments

Comments
 (0)