Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rclark committed Feb 3, 2019
1 parent c3325c3 commit de99ff9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
26 changes: 22 additions & 4 deletions src/wrappers.js
Expand Up @@ -42,29 +42,40 @@ 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()
});
}
}

return this.fromObject(object);
},

toObject: function(message, options) {

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

// 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);
// 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 +85,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;
}
var 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();
});

0 comments on commit de99ff9

Please sign in to comment.