Skip to content

Commit 49ae6f8

Browse files
authored
Merge pull request #1078 from mtardy/pr/mtardy/type-alias-validation
🐛 pkg/crd: support validation on type alias to basic types
2 parents 5656666 + e443da3 commit 49ae6f8

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

pkg/crd/schema.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,30 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
242242
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("unknown type %s", ident.Name), ident))
243243
return &apiext.JSONSchemaProps{}
244244
}
245+
// This reproduces the behavior we had pre gotypesalias=1 (needed if this
246+
// project is compiled with default settings and Go >= 1.23).
247+
if aliasInfo, isAlias := typeInfo.(*types.Alias); isAlias {
248+
typeInfo = aliasInfo.Underlying()
249+
}
245250
if basicInfo, isBasic := typeInfo.(*types.Basic); isBasic {
246251
typ, fmt, err := builtinToType(basicInfo, ctx.allowDangerousTypes)
247252
if err != nil {
248253
ctx.pkg.AddError(loader.ErrFromNode(err, ident))
249254
}
255+
// Check for type aliasing to a basic type for gotypesalias=0. See more
256+
// in documentation https://pkg.go.dev/go/types#Alias:
257+
// > For gotypesalias=1, alias declarations produce an Alias type.
258+
// > Otherwise, the alias information is only in the type name, which
259+
// > points directly to the actual (aliased) type.
260+
if basicInfo.Name() != ident.Name {
261+
ctx.requestSchema("", ident.Name)
262+
link := TypeRefLink("", ident.Name)
263+
return &apiext.JSONSchemaProps{
264+
Type: typ,
265+
Format: fmt,
266+
Ref: &link,
267+
}
268+
}
250269
return &apiext.JSONSchemaProps{
251270
Type: typ,
252271
Format: fmt,

pkg/crd/testdata/cronjob_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@ type CronJobSpec struct {
335335
// This tests that string alias is handled correctly.
336336
StringAlias StringAlias `json:"stringAlias,omitempty"`
337337

338+
// This tests that validation on a string alias type is handled correctly.
339+
// +kubebuilder:validation:MinLength=1
340+
// +kubebuilder:validation:MaxLength=255
341+
StringAliasAddedValidation StringAlias `json:"stringAliasAddedValidation,omitempty"`
342+
343+
// This tests that validation on a the string alias type itself is handled correctly.
344+
StringAliasAlreadyValidated StringAliasWithValidation `json:"stringAliasAlreadyValidated,omitempty"`
345+
338346
// This tests string slice validation.
339347
// +kubebuilder:validation:MinItems=2
340348
// +kubebuilder:validation:MaxItems=2
@@ -364,6 +372,10 @@ type CronJobSpec struct {
364372

365373
type StringAlias = string
366374

375+
// +kubebuilder:validation:MinLength=1
376+
// +kubebuilder:validation:MaxLength=255
377+
type StringAliasWithValidation = string
378+
367379
type ContainsNestedMap struct {
368380
InnerMap map[string]string `json:"innerMap,omitempty"`
369381
}

pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8984,6 +8984,16 @@ spec:
89848984
stringAlias:
89858985
description: This tests that string alias is handled correctly.
89868986
type: string
8987+
stringAliasAddedValidation:
8988+
description: This tests that validation on a string alias type is handled correctly.
8989+
maxLength: 255
8990+
minLength: 1
8991+
type: string
8992+
stringAliasAlreadyValidated:
8993+
description: This tests that validation on a the string alias type itself is handled correctly.
8994+
maxLength: 255
8995+
minLength: 1
8996+
type: string
89878997
stringPair:
89888998
description: This tests string slice validation.
89898999
items:

0 commit comments

Comments
 (0)