@@ -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.
6461type 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
0 commit comments