From c3d049f3e5b67ed58979793183709bed0bf32f14 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Mon, 9 Dec 2024 16:23:02 +0100 Subject: [PATCH] fix: Custom typePath function instead of exposing private `jsonschema.typePath` function --- bundle/internal/schema/annotations.go | 6 +++++- bundle/internal/schema/parser.go | 2 +- libs/jsonschema/from_type.go | 14 +++++++------- libs/jsonschema/from_type_test.go | 4 ++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/bundle/internal/schema/annotations.go b/bundle/internal/schema/annotations.go index 05500a842..7c55942f7 100644 --- a/bundle/internal/schema/annotations.go +++ b/bundle/internal/schema/annotations.go @@ -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() +} diff --git a/bundle/internal/schema/parser.go b/bundle/internal/schema/parser.go index 3a24624cc..8f2217f82 100644 --- a/bundle/internal/schema/parser.go +++ b/bundle/internal/schema/parser.go @@ -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, diff --git a/libs/jsonschema/from_type.go b/libs/jsonschema/from_type.go index cda92ad22..18a2b3ba5 100644 --- a/libs/jsonschema/from_type.go +++ b/libs/jsonschema/from_type.go @@ -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 { diff --git a/libs/jsonschema/from_type_test.go b/libs/jsonschema/from_type_test.go index 249de0c50..174ffad88 100644 --- a/libs/jsonschema/from_type_test.go +++ b/libs/jsonschema/from_type_test.go @@ -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{})) }) }