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

fix: google.protobuf.Any type_url fixes #1068

Merged
merged 4 commits into from May 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/wrappers.js
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
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();
});