Skip to content

Commit e443da3

Browse files
committed
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 <[email protected]>
1 parent 7587d2c commit e443da3

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

pkg/crd/schema.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,18 @@ 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
}
250-
// Check for type aliasing to a basic type. Note that this is no longer
251-
// needed with gotypesalias=1 as the above isBasic check is false. See
252-
// more https://pkg.go.dev/go/types#Alias:
255+
// Check for type aliasing to a basic type for gotypesalias=0. See more
256+
// in documentation https://pkg.go.dev/go/types#Alias:
253257
// > For gotypesalias=1, alias declarations produce an Alias type.
254258
// > Otherwise, the alias information is only in the type name, which
255259
// > points directly to the actual (aliased) type.

0 commit comments

Comments
 (0)