Skip to content

Commit

Permalink
fix: google.protobuf.Any type_url fixes (protobufjs#1068)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored and Taylor McIntyre committed Oct 16, 2020
1 parent a72da6b commit 938b920
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
27 changes: 23 additions & 4 deletions src/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,20 @@ wrappers[".google.protobuf.Any"] = {

// unwrap value type if mapped
if (object && object["@type"]) {
var type = this.lookup(object["@type"]);
// Only use fully qualified type name after the last '/'
var name = object["@type"].substring(object["@type"].lastIndexOf("/") + 1);
var type = this.lookup(name);
/* istanbul ignore else */
if (type) {
// type_url does not accept leading "."
var type_url = object["@type"].charAt(0) === "." ?
object["@type"].substr(1) : object["@type"];
// type_url prefix is optional, but path seperator is required
if (type_url.indexOf("/") === -1) {
type_url = "/" + type_url;
}
return this.create({
type_url: "/" + type_url,
type_url: type_url,
value: type.encode(type.fromObject(object)).finish()
});
}
Expand All @@ -61,10 +66,17 @@ wrappers[".google.protobuf.Any"] = {

toObject: function(message, options) {

// Default prefix
var googleApi = "type.googleapis.com/";
var prefix = "";
var name = "";

// decode value if requested and unmapped
if (options && options.json && message.type_url && message.value) {
// Only use fully qualified type name after the last '/'
var name = message.type_url.substring(message.type_url.lastIndexOf("/") + 1);
name = message.type_url.substring(message.type_url.lastIndexOf("/") + 1);
// Separate the prefix used
prefix = message.type_url.substring(0, message.type_url.lastIndexOf("/") + 1);
var type = this.lookup(name);
/* istanbul ignore else */
if (type)
Expand All @@ -74,7 +86,14 @@ wrappers[".google.protobuf.Any"] = {
// wrap value if unmapped
if (!(message instanceof this.ctor) && message instanceof Message) {
var object = message.$type.toObject(message, options);
object["@type"] = message.$type.fullName;
var messageName = message.$type.fullName[0] === "." ?
message.$type.fullName.substr(1) : message.$type.fullName;
// Default to type.googleapis.com prefix if no prefix is used
if (prefix === "") {
prefix = googleApi;
}
name = prefix + messageName;
object["@type"] = name;
return object;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/comp_google_protobuf_any.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ tape.test("google.protobuf.Any", function(test) {
test.same(obj.foo, { type_url: "Bar", value: [10, 1, 97] }, "should keep explicit Any in toObject properly");

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

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

test.end();
});

0 comments on commit 938b920

Please sign in to comment.