From e443da3e9ad2f3f73b189ffaf21f788eb5f9c25f Mon Sep 17 00:00:00 2001 From: Mahe Tardy Date: Mon, 21 Oct 2024 13:03:33 +0200 Subject: [PATCH] pkg/crd: fix alias conversion to schema with gotypesalias=1 This patch will be needed when upgrading to Go 1.23 as type alias now generate a proper type (instead of directly being the basic type they alias to), see https://pkg.go.dev/go/types#Alias for more info. This just reproduces the old behavior by retrieving the underlying type from the alias. If needed, we could treat alias more gracefully to make the type ref link now that we have all the type information. Signed-off-by: Mahe Tardy --- pkg/crd/schema.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/crd/schema.go b/pkg/crd/schema.go index fcc50b5ab..a665fca3b 100644 --- a/pkg/crd/schema.go +++ b/pkg/crd/schema.go @@ -242,14 +242,18 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("unknown type %s", ident.Name), ident)) return &apiext.JSONSchemaProps{} } + // This reproduces the behavior we had pre gotypesalias=1 (needed if this + // project is compiled with default settings and Go >= 1.23). + if aliasInfo, isAlias := typeInfo.(*types.Alias); isAlias { + typeInfo = aliasInfo.Underlying() + } if basicInfo, isBasic := typeInfo.(*types.Basic); isBasic { typ, fmt, err := builtinToType(basicInfo, ctx.allowDangerousTypes) if err != nil { ctx.pkg.AddError(loader.ErrFromNode(err, ident)) } - // Check for type aliasing to a basic type. Note that this is no longer - // needed with gotypesalias=1 as the above isBasic check is false. See - // more https://pkg.go.dev/go/types#Alias: + // Check for type aliasing to a basic type for gotypesalias=0. See more + // in documentation https://pkg.go.dev/go/types#Alias: // > For gotypesalias=1, alias declarations produce an Alias type. // > Otherwise, the alias information is only in the type name, which // > points directly to the actual (aliased) type.