diff --git a/typeurl/types.go b/typeurl/types.go index 27fb6dde4..e12b8a3bf 100644 --- a/typeurl/types.go +++ b/typeurl/types.go @@ -22,13 +22,19 @@ var ( // Register a type with the base url of the type func Register(v interface{}, args ...string) { - t := tryDereference(v) + var ( + t = tryDereference(v) + p = path.Join(append([]string{Prefix}, args...)...) + ) mu.Lock() defer mu.Unlock() - if _, ok := registry[t]; ok { + if et, ok := registry[t]; ok { + if et != p { + panic(errors.Errorf("type registred with alternate path %q != %q", et, p)) + } return } - registry[t] = path.Join(append([]string{Prefix}, args...)...) + registry[t] = p } // TypeURL returns the type url for a registred type diff --git a/typeurl/types_test.go b/typeurl/types_test.go index 1535eeec1..fa5339237 100644 --- a/typeurl/types_test.go +++ b/typeurl/types_test.go @@ -130,6 +130,17 @@ func TestMarshalEvent(t *testing.T) { } } +func TestRegisterDiffUrls(t *testing.T) { + clear() + defer func() { + if err := recover(); err == nil { + t.Error("registering the same type with different urls should panic") + } + }() + Register(&test{}, "test") + Register(&test{}, "test", "two") +} + func BenchmarkMarshalEvent(b *testing.B) { ev := &eventsapi.TaskStart{} expected, err := MarshalAny(ev)