Skip to content

Commit fe5d201

Browse files
committed
Revert "Start from local type declaration when applying schema"
This reverts commit 5d60153.
1 parent 97ea62c commit fe5d201

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

pkg/crd/parser.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,7 @@ func (p *Parser) NeedSchemaFor(typ TypeIdent) {
172172
// avoid tripping recursive schemata, like ManagedFields, by adding an empty WIP schema
173173
p.Schemata[typ] = apiext.JSONSchemaProps{}
174174

175-
schemaCtx := newSchemaContext(typ.Package, p, func(typ TypeIdent) *apiext.JSONSchemaProps {
176-
p.NeedSchemaFor(typ)
177-
178-
props := p.Schemata[typ]
179-
return &props
180-
}, p.AllowDangerousTypes, p.IgnoreUnexportedFields)
175+
schemaCtx := newSchemaContext(typ.Package, p, p.AllowDangerousTypes, p.IgnoreUnexportedFields)
181176
ctxForInfo := schemaCtx.ForInfo(info)
182177

183178
pkgMarkers, err := markers.PackageMarkers(p.Collector, typ.Package)

pkg/crd/schema.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ type applyFirstMarker interface {
5757
ApplyFirst()
5858
}
5959

60-
// schemaFetcher is a function that fetches a schema for a given type.
61-
type schemaFetcher func(TypeIdent) *apiext.JSONSchemaProps
62-
6360
// schemaRequester knows how to marker that another schema (e.g. via an external reference) is necessary.
6461
type schemaRequester interface {
6562
NeedSchemaFor(typ TypeIdent)
@@ -71,7 +68,6 @@ type schemaContext struct {
7168
info *markers.TypeInfo
7269

7370
schemaRequester schemaRequester
74-
schemaFetcher schemaFetcher
7571
PackageMarkers markers.MarkerValues
7672

7773
allowDangerousTypes bool
@@ -80,12 +76,11 @@ type schemaContext struct {
8076

8177
// newSchemaContext constructs a new schemaContext for the given package and schema requester.
8278
// It must have type info added before use via ForInfo.
83-
func newSchemaContext(pkg *loader.Package, req schemaRequester, fetcher schemaFetcher, allowDangerousTypes, ignoreUnexportedFields bool) *schemaContext {
79+
func newSchemaContext(pkg *loader.Package, req schemaRequester, allowDangerousTypes, ignoreUnexportedFields bool) *schemaContext {
8480
pkg.NeedTypesInfo()
8581
return &schemaContext{
8682
pkg: pkg,
8783
schemaRequester: req,
88-
schemaFetcher: fetcher,
8984
allowDangerousTypes: allowDangerousTypes,
9085
ignoreUnexportedFields: ignoreUnexportedFields,
9186
}
@@ -98,7 +93,6 @@ func (c *schemaContext) ForInfo(info *markers.TypeInfo) *schemaContext {
9893
pkg: c.pkg,
9994
info: info,
10095
schemaRequester: c.schemaRequester,
101-
schemaFetcher: c.schemaFetcher,
10296
allowDangerousTypes: c.allowDangerousTypes,
10397
ignoreUnexportedFields: c.ignoreUnexportedFields,
10498
}
@@ -240,9 +234,7 @@ func typeToSchema(ctx *schemaContext, rawType ast.Expr) *apiext.JSONSchemaProps
240234
return &apiext.JSONSchemaProps{}
241235
}
242236

243-
if ctx.info.Doc != "" {
244-
props.Description = ctx.info.Doc
245-
}
237+
props.Description = ctx.info.Doc
246238

247239
applyMarkers(ctx, ctx.info.Markers, props, rawType)
248240

@@ -278,7 +270,6 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
278270
if aliasInfo, isAlias := typeInfo.(*types.Alias); isAlias {
279271
typeInfo = aliasInfo.Rhs()
280272
}
281-
282273
if basicInfo, isBasic := typeInfo.(*types.Basic); isBasic {
283274
typ, fmt, err := builtinToType(basicInfo, ctx.allowDangerousTypes)
284275
if err != nil {
@@ -290,21 +281,32 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
290281
// > Otherwise, the alias information is only in the type name, which
291282
// > points directly to the actual (aliased) type.
292283
if basicInfo.Name() != ident.Name {
293-
return ctx.schemaFetcher(TypeIdent{
294-
Package: ctx.pkg,
295-
Name: ident.Name,
296-
})
284+
ctx.requestSchema("", ident.Name)
285+
link := TypeRefLink("", ident.Name)
286+
return &apiext.JSONSchemaProps{
287+
Type: typ,
288+
Format: fmt,
289+
Ref: &link,
290+
}
297291
}
298292
return &apiext.JSONSchemaProps{
299293
Type: typ,
300294
Format: fmt,
301295
}
302296
}
303-
304-
return ctx.schemaFetcher(TypeIdent{
305-
Package: ctx.pkg,
306-
Name: ident.Name,
307-
})
297+
// NB(directxman12): if there are dot imports, this might be an external reference,
298+
// so use typechecking info to get the actual object
299+
typeNameInfo := typeInfo.(interface{ Obj() *types.TypeName }).Obj()
300+
pkg := typeNameInfo.Pkg()
301+
pkgPath := loader.NonVendorPath(pkg.Path())
302+
if pkg == ctx.pkg.Types {
303+
pkgPath = ""
304+
}
305+
ctx.requestSchema(pkgPath, typeNameInfo.Name())
306+
link := TypeRefLink(pkgPath, typeNameInfo.Name())
307+
return &apiext.JSONSchemaProps{
308+
Ref: &link,
309+
}
308310
}
309311

310312
// namedSchema creates a schema (ref) for an explicitly external type reference.
@@ -504,9 +506,7 @@ func structToSchema(ctx *schemaContext, structType *ast.StructType) *apiext.JSON
504506
} else {
505507
propSchema = typeToSchema(ctx.ForInfo(&markers.TypeInfo{}), field.RawField.Type)
506508
}
507-
if field.Doc != "" {
508-
propSchema.Description = field.Doc
509-
}
509+
propSchema.Description = field.Doc
510510

511511
applyMarkers(ctx, field.Markers, propSchema, field.RawField)
512512

pkg/crd/schema_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func transform(t *testing.T, expr string) *apiext.JSONSchemaProps {
6464
pkg.NeedTypesInfo()
6565
failIfErrors(t, pkg.Errors)
6666

67-
schemaContext := newSchemaContext(pkg, nil, nil, true, false).ForInfo(&markers.TypeInfo{})
67+
schemaContext := newSchemaContext(pkg, nil, true, false).ForInfo(&markers.TypeInfo{})
6868
// yick: grab the only type definition
6969
definedType := pkg.Syntax[0].Decls[0].(*ast.GenDecl).Specs[0].(*ast.TypeSpec).Type
7070
result := typeToSchema(schemaContext, definedType)

0 commit comments

Comments
 (0)