Skip to content

Commit f82cc1f

Browse files
committed
handle any in default case to avoid more possible panics
1 parent 7049f31 commit f82cc1f

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

pkg/crd/gen_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ var _ = Describe("CRD Generation proper defaulting", func() {
194194
posRel, err := filepath.Rel(filepath.Join(wd, genDir), pkgError.Pos)
195195
Expect(err).NotTo(HaveOccurred())
196196
Expect(posRel).To(Equal("iface/iface_types.go:32:6"))
197-
Expect(pkgError.Msg).To(Equal("cannot generate schema for interface type any"))
197+
Expect(pkgError.Msg).To(Equal("cannot generate schema for any"))
198198
matches++
199199
}
200200
}

pkg/crd/schema.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,9 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
270270
if aliasInfo, isAlias := typeInfo.(*types.Alias); isAlias {
271271
typeInfo = aliasInfo.Rhs()
272272
}
273-
if basicInfo, isBasic := typeInfo.(*types.Basic); isBasic {
274-
typ, fmt, err := builtinToType(basicInfo, ctx.allowDangerousTypes)
273+
switch typeInfo := typeInfo.(type) {
274+
case *types.Basic:
275+
typ, fmt, err := builtinToType(typeInfo, ctx.allowDangerousTypes)
275276
if err != nil {
276277
ctx.pkg.AddError(loader.ErrFromNode(err, ident))
277278
}
@@ -280,7 +281,7 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
280281
// > For gotypesalias=1, alias declarations produce an Alias type.
281282
// > Otherwise, the alias information is only in the type name, which
282283
// > points directly to the actual (aliased) type.
283-
if basicInfo.Name() != ident.Name {
284+
if typeInfo.Name() != ident.Name {
284285
ctx.requestSchema("", ident.Name)
285286
link := TypeRefLink("", ident.Name)
286287
return &apiext.JSONSchemaProps{
@@ -293,24 +294,25 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
293294
Type: typ,
294295
Format: fmt,
295296
}
296-
}
297-
if _, isInterface := typeInfo.(*types.Interface); isInterface {
298-
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("cannot generate schema for interface type %s", ident.Name), ident))
297+
case interface{ Obj() *types.TypeName }:
298+
// NB(directxman12): if there are dot imports, this might be an external reference,
299+
// so use typechecking info to get the actual object
300+
typeNameInfo := typeInfo.Obj()
301+
pkg := typeNameInfo.Pkg()
302+
pkgPath := loader.NonVendorPath(pkg.Path())
303+
if pkg == ctx.pkg.Types {
304+
pkgPath = ""
305+
}
306+
ctx.requestSchema(pkgPath, typeNameInfo.Name())
307+
link := TypeRefLink(pkgPath, typeNameInfo.Name())
308+
return &apiext.JSONSchemaProps{
309+
Ref: &link,
310+
}
311+
default:
312+
// This happens for type any, and other scenarios.
313+
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("cannot generate schema for %s", ident.Name), ident))
299314
return &apiext.JSONSchemaProps{}
300315
}
301-
// NB(directxman12): if there are dot imports, this might be an external reference,
302-
// so use typechecking info to get the actual object
303-
typeNameInfo := typeInfo.(interface{ Obj() *types.TypeName }).Obj()
304-
pkg := typeNameInfo.Pkg()
305-
pkgPath := loader.NonVendorPath(pkg.Path())
306-
if pkg == ctx.pkg.Types {
307-
pkgPath = ""
308-
}
309-
ctx.requestSchema(pkgPath, typeNameInfo.Name())
310-
link := TypeRefLink(pkgPath, typeNameInfo.Name())
311-
return &apiext.JSONSchemaProps{
312-
Ref: &link,
313-
}
314316
}
315317

316318
// namedSchema creates a schema (ref) for an explicitly external type reference.

0 commit comments

Comments
 (0)