Skip to content

Commit

Permalink
Match typeUrls with leading slash in google.protobuf.Any (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
grod220 authored Nov 17, 2023
1 parent d1cb9fc commit 9777c0b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions packages/protobuf-test/src/google/protobuf/any.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ describe("google.protobuf.Any", () => {
expect(got.is("type.googleapis.com/google.protobuf.Value")).toBe(false);
});

test(`matches type name with leading slash`, () => {
const any = new Any({ typeUrl: "/google.protobuf.Value" });
expect(any.is(Value)).toBe(true);
});

test(`is returns false for an empty Any`, () => {
const got = new Any();

Expand All @@ -106,6 +111,21 @@ describe("google.protobuf.Any", () => {
expect(unpacked.kind.value).toBe(1);
});

test(`unpack correctly unpacks a message with a leading slash type url in the registry`, () => {
const typeRegistry = createRegistry(Value);
const val = new Value({
kind: { case: "numberValue", value: 1 },
});
const { value } = Any.pack(val);
const any = new Any({ typeUrl: "/google.protobuf.Value", value });

const unpacked = any.unpack(typeRegistry) as Value;

expect(unpacked).toBeDefined();
expect(unpacked.kind.case).toBe("numberValue");
expect(unpacked.kind.value).toBe(1);
});

test(`unpack returns undefined if message not in the registry`, () => {
const typeRegistry = createRegistry();
const val = new Value({
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/google/protobuf/any_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/protoc-gen-es/src/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
f.print(" throw new Error(`invalid type url: ${url}`);");
f.print(" }");
f.print(` const slash = url.lastIndexOf("/");`);
f.print(" const name = slash > 0 ? url.substring(slash + 1) : url;");
f.print(" const name = slash >= 0 ? url.substring(slash + 1) : url;");
f.print(" if (!name.length) {");
f.print(" throw new Error(`invalid type url: ${url}`);");
f.print(" }");
Expand Down
2 changes: 1 addition & 1 deletion packages/protoc-gen-es/src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
f.print(" throw new Error(`invalid type url: ${url}`);");
f.print(" }");
f.print(` const slash = url.lastIndexOf("/");`);
f.print(" const name = slash > 0 ? url.substring(slash + 1) : url;");
f.print(" const name = slash >= 0 ? url.substring(slash + 1) : url;");
f.print(" if (!name.length) {");
f.print(" throw new Error(`invalid type url: ${url}`);");
f.print(" }");
Expand Down

0 comments on commit 9777c0b

Please sign in to comment.