Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type URLs can have any host and even include URI scheme #636

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ func (d *ErrorDetail) Type() string {
// than plain type names, but there aren't any descriptor registries
// deployed. With the current state of the `Any` code, it's not possible to
// build a useful type registry either. To hide this from users, we should
// trim the static hostname that `Any` adds to the type name.
// trim the URL prefix is added to the type name.
//
// If we ever want to support remote registries, we can add an explicit
// `TypeURL` method.
return strings.TrimPrefix(d.pb.GetTypeUrl(), defaultAnyResolverPrefix)
return typeNameFromURL(d.pb.GetTypeUrl())
}

// Bytes returns a copy of the Protobuf-serialized detail.
Expand Down Expand Up @@ -409,3 +409,7 @@ func asMaxBytesError(err error, tmpl string, args ...any) *Error {
prefix := fmt.Sprintf(tmpl, args...)
return errorf(CodeResourceExhausted, "%s: exceeded %d byte http.MaxBytesReader limit", prefix, maxBytesErr.Limit)
}

func typeNameFromURL(url string) string {
return url[strings.LastIndexByte(url, '/')+1:]
}
42 changes: 42 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,45 @@ func TestErrorIs(t *testing.T) {
assert.False(t, errors.Is(connectErr, NewError(CodeUnavailable, err)))
assert.True(t, errors.Is(connectErr, connectErr))
}

func TestTypeNameFromURL(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
url string
typeName string
}{
{
name: "no-prefix",
url: "foo.bar.Baz",
typeName: "foo.bar.Baz",
},
{
name: "standard-prefix",
url: defaultAnyResolverPrefix + "foo.bar.Baz",
typeName: "foo.bar.Baz",
},
{
name: "different-hostname",
url: "abc.com/foo.bar.Baz",
typeName: "foo.bar.Baz",
},
{
name: "additional-path-elements",
url: defaultAnyResolverPrefix + "abc/def/foo.bar.Baz",
typeName: "foo.bar.Baz",
},
{
name: "full-url",
url: "https://abc.com/abc/def/foo.bar.Baz",
typeName: "foo.bar.Baz",
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, typeNameFromURL(testCase.url), testCase.typeName)
})
}
}
2 changes: 1 addition & 1 deletion protocol_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ func (d *connectWireDetail) MarshalJSON() ([]byte, error) {
Value string `json:"value"`
Debug json.RawMessage `json:"debug,omitempty"`
}{
Type: strings.TrimPrefix(d.pb.GetTypeUrl(), defaultAnyResolverPrefix),
Type: typeNameFromURL(d.pb.GetTypeUrl()),
Value: base64.RawStdEncoding.EncodeToString(d.pb.GetValue()),
}
// Try to produce debug info, but expect failure when we don't have
Expand Down