Skip to content

Commit 192f5f1

Browse files
nickpestovmurgatroid99alexander-fenster
authored
fix: google.protobuf.Any type_url fixes (#1068)
* google.protobuf.Any fixes In `fromObject`, only use type name after the last '/' when doing a lookup and make sure the `type_url` prefix is preserved. In `toObject`, use `type.googleapis.com` default prefix if no `type_url` prefix is used. * fix: lint * fix: more lint Co-authored-by: Michael Lumish <[email protected]> Co-authored-by: Alexander Fenster <[email protected]>
1 parent 4ecfbce commit 192f5f1

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/wrappers.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,20 @@ wrappers[".google.protobuf.Any"] = {
4242

4343
// unwrap value type if mapped
4444
if (object && object["@type"]) {
45-
var type = this.lookup(object["@type"]);
45+
// Only use fully qualified type name after the last '/'
46+
var name = object["@type"].substring(object["@type"].lastIndexOf("/") + 1);
47+
var type = this.lookup(name);
4648
/* istanbul ignore else */
4749
if (type) {
4850
// type_url does not accept leading "."
4951
var type_url = object["@type"].charAt(0) === "." ?
5052
object["@type"].substr(1) : object["@type"];
5153
// type_url prefix is optional, but path seperator is required
54+
if (type_url.indexOf("/") === -1) {
55+
type_url = "/" + type_url;
56+
}
5257
return this.create({
53-
type_url: "/" + type_url,
58+
type_url: type_url,
5459
value: type.encode(type.fromObject(object)).finish()
5560
});
5661
}
@@ -61,10 +66,17 @@ wrappers[".google.protobuf.Any"] = {
6166

6267
toObject: function(message, options) {
6368

69+
// Default prefix
70+
var googleApi = "type.googleapis.com/";
71+
var prefix = "";
72+
var name = "";
73+
6474
// decode value if requested and unmapped
6575
if (options && options.json && message.type_url && message.value) {
6676
// Only use fully qualified type name after the last '/'
67-
var name = message.type_url.substring(message.type_url.lastIndexOf("/") + 1);
77+
name = message.type_url.substring(message.type_url.lastIndexOf("/") + 1);
78+
// Separate the prefix used
79+
prefix = message.type_url.substring(0, message.type_url.lastIndexOf("/") + 1);
6880
var type = this.lookup(name);
6981
/* istanbul ignore else */
7082
if (type)
@@ -74,7 +86,14 @@ wrappers[".google.protobuf.Any"] = {
7486
// wrap value if unmapped
7587
if (!(message instanceof this.ctor) && message instanceof Message) {
7688
var object = message.$type.toObject(message, options);
77-
object["@type"] = message.$type.fullName;
89+
var messageName = message.$type.fullName[0] === "." ?
90+
message.$type.fullName.substr(1) : message.$type.fullName;
91+
// Default to type.googleapis.com prefix if no prefix is used
92+
if (prefix === "") {
93+
prefix = googleApi;
94+
}
95+
name = prefix + messageName;
96+
object["@type"] = name;
7897
return object;
7998
}
8099

tests/comp_google_protobuf_any.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ tape.test("google.protobuf.Any", function(test) {
4242
test.same(obj.foo, { type_url: "Bar", value: [10, 1, 97] }, "should keep explicit Any in toObject properly");
4343

4444
obj = Foo.toObject(foo, { json: true });
45-
test.same(obj.foo, { "@type": ".Bar", bar: "a" }, "should decode explicitly Any in toObject if requested");
45+
test.same(obj.foo, { "@type": "type.googleapis.com/Bar", bar: "a" }, "should decode explicitly Any in toObject if requested");
4646

4747
foo = Foo.fromObject({
4848
foo: {
@@ -60,7 +60,7 @@ tape.test("google.protobuf.Any", function(test) {
6060
}
6161
});
6262
obj = Foo.toObject(baz, { json: true });
63-
test.same(obj.foo, { "@type": ".Bar", bar: "a" }, "should not care about prefix in type url");
63+
test.same(obj.foo, { "@type": "type.someurl.com/Bar", bar: "a" }, "should keep prefix in type url");
6464

6565
test.end();
6666
});

0 commit comments

Comments
 (0)