Skip to content

Commit

Permalink
Merge pull request #115 from danielgtaylor/generic-named-types
Browse files Browse the repository at this point in the history
fix: better support of generic named types
  • Loading branch information
danielgtaylor authored Aug 31, 2023
2 parents 5442462 + c79bf0d commit 35ab59f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
13 changes: 9 additions & 4 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"strings"
)

// Registry creates and stores schemas and their references, and supports
Expand All @@ -16,18 +17,22 @@ type Registry interface {
SchemaFromRef(ref string) *Schema
TypeFromRef(ref string) reflect.Type
Map() map[string]*Schema
MarshalJSON() ([]byte, error)
MarshalYAML() (interface{}, error)
}

// DefaultSchemaNamer provides schema names for types. It uses the type name
// when possible, ignoring the package name. If the type is unnamed, then
// the name hint is used.
// when possible, ignoring the package name. If the type is generic, e.g.
// `MyType[SubType]`, then the brackets are removed like `MyTypeSubType`.
// If the type is unnamed, then the name hint is used.
// Note: if you plan to use types with the same name from different packages,
// you should implement your own namer function to prevent issues. Nested
// anonymous types can also present naming issues.
func DefaultSchemaNamer(t reflect.Type, hint string) string {
name := deref(t).Name()

// Fix up generics, if used, for nicer refs & URLs.
name = strings.ReplaceAll(name, "[", "")
name = strings.ReplaceAll(name, "]", "")

if name == "" {
name = hint
}
Expand Down
14 changes: 14 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,20 @@ func TestSchemaOld(t *testing.T) {
// fmt.Println(string(b))
}

func TestSchemaGenericNaming(t *testing.T) {
type SchemaGeneric[T any] struct {
Value T `json:"value"`
}

r := NewMapRegistry("#/components/schemas/", DefaultSchemaNamer)
s := r.Schema(reflect.TypeOf(SchemaGeneric[int]{}), true, "")

b, _ := json.Marshal(s)
assert.JSONEq(t, `{
"$ref": "#/components/schemas/SchemaGenericint"
}`, string(b))
}

type BenchSub struct {
Visible bool `json:"visible" default:"true"`
Metrics []float64 `json:"metrics" maxItems:"31"`
Expand Down

0 comments on commit 35ab59f

Please sign in to comment.