mirror of https://github.com/databricks/cli.git
fix: Custom typePath function instead of exposing private `jsonschema.typePath` function
This commit is contained in:
parent
79a88e945f
commit
c3d049f3e5
|
@ -64,7 +64,7 @@ func newAnnotationHandler(sources []string) (*annotationHandler, error) {
|
|||
}
|
||||
|
||||
func (d *annotationHandler) addAnnotations(typ reflect.Type, s jsonschema.Schema) jsonschema.Schema {
|
||||
refPath := jsonschema.TypePath(typ)
|
||||
refPath := getPath(typ)
|
||||
items := map[string]*jsonschema.Schema{}
|
||||
items[refPath] = &s
|
||||
|
||||
|
@ -128,3 +128,7 @@ func (d *annotationHandler) sync(outputPath string) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getPath(typ reflect.Type) string {
|
||||
return typ.PkgPath() + "." + typ.Name()
|
||||
}
|
||||
|
|
|
@ -115,7 +115,7 @@ func (p *openapiParser) extractAnnotations(typ reflect.Type, outputPath, overrid
|
|||
return s
|
||||
}
|
||||
|
||||
basePath := jsonschema.TypePath(typ)
|
||||
basePath := getPath(typ)
|
||||
annotations[basePath] = annotation{
|
||||
Description: ref.Description,
|
||||
Enum: ref.Enum,
|
||||
|
|
|
@ -52,7 +52,7 @@ func (c *constructor) Definitions() map[string]any {
|
|||
|
||||
// Remove the root type from the definitions. We don't need to include it in
|
||||
// the definitions because it will be inlined as the root of the generated JSON schema.
|
||||
delete(defs, TypePath(c.root))
|
||||
delete(defs, typePath(c.root))
|
||||
|
||||
if len(defs) == 0 {
|
||||
return nil
|
||||
|
@ -106,14 +106,14 @@ func FromType(typ reflect.Type, fns []func(typ reflect.Type, s Schema) Schema) (
|
|||
}
|
||||
}
|
||||
|
||||
res := c.definitions[TypePath(typ)]
|
||||
res := c.definitions[typePath(typ)]
|
||||
res.Definitions = c.Definitions()
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// TypePath computes a unique string representation of the type. $ref in the generated
|
||||
// typePath computes a unique string representation of the type. $ref in the generated
|
||||
// JSON schema will refer to this path. See TestTypePath for examples outputs.
|
||||
func TypePath(typ reflect.Type) string {
|
||||
func typePath(typ reflect.Type) string {
|
||||
// Pointers have a typ.Name() of "". Dereference them to get the underlying type.
|
||||
for typ.Kind() == reflect.Ptr {
|
||||
typ = typ.Elem()
|
||||
|
@ -125,7 +125,7 @@ func TypePath(typ reflect.Type) string {
|
|||
|
||||
// Recursively call typePath, to handle slices of slices / maps.
|
||||
if typ.Kind() == reflect.Slice {
|
||||
return path.Join("slice", TypePath(typ.Elem()))
|
||||
return path.Join("slice", typePath(typ.Elem()))
|
||||
}
|
||||
|
||||
if typ.Kind() == reflect.Map {
|
||||
|
@ -134,7 +134,7 @@ func TypePath(typ reflect.Type) string {
|
|||
}
|
||||
|
||||
// Recursively call typePath, to handle maps of maps / slices.
|
||||
return path.Join("map", TypePath(typ.Elem()))
|
||||
return path.Join("map", typePath(typ.Elem()))
|
||||
}
|
||||
|
||||
switch {
|
||||
|
@ -157,7 +157,7 @@ func (c *constructor) walk(typ reflect.Type) (string, error) {
|
|||
typ = typ.Elem()
|
||||
}
|
||||
|
||||
typPath := TypePath(typ)
|
||||
typPath := typePath(typ)
|
||||
|
||||
// Return early if the type has already been seen, to avoid infinite recursion.
|
||||
if _, ok := c.seen[typPath]; ok {
|
||||
|
|
|
@ -510,12 +510,12 @@ func TestTypePath(t *testing.T) {
|
|||
|
||||
for _, tc := range tcases {
|
||||
t.Run(tc.typ.String(), func(t *testing.T) {
|
||||
assert.Equal(t, tc.path, TypePath(tc.typ))
|
||||
assert.Equal(t, tc.path, typePath(tc.typ))
|
||||
})
|
||||
}
|
||||
|
||||
// Maps with non-string keys should panic.
|
||||
assert.PanicsWithValue(t, "found map with non-string key: int", func() {
|
||||
TypePath(reflect.TypeOf(map[int]int{}))
|
||||
typePath(reflect.TypeOf(map[int]int{}))
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue