Skip to content

Commit de99ff9

Browse files
committed
1 parent c3325c3 commit de99ff9

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/wrappers.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,40 @@ 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
}
5762
}
58-
63+
5964
return this.fromObject(object);
6065
},
6166

6267
toObject: function(message, options) {
6368

69+
// Default prefix
70+
var googleApi = "type.googleapis.com/";
71+
var prefix = "";
72+
6473
// decode value if requested and unmapped
6574
if (options && options.json && message.type_url && message.value) {
6675
// Only use fully qualified type name after the last '/'
6776
var name = message.type_url.substring(message.type_url.lastIndexOf("/") + 1);
77+
// Separate the prefix used
78+
prefix = message.type_url.substring(0, message.type_url.lastIndexOf('/') + 1);
6879
var type = this.lookup(name);
6980
/* istanbul ignore else */
7081
if (type)
@@ -74,7 +85,14 @@ wrappers[".google.protobuf.Any"] = {
7485
// wrap value if unmapped
7586
if (!(message instanceof this.ctor) && message instanceof Message) {
7687
var object = message.$type.toObject(message, options);
77-
object["@type"] = message.$type.fullName;
88+
var messageName = message.$type.fullName[0] === "." ?
89+
message.$type.fullName.substr(1) : message.$type.fullName;
90+
// Default to type.googleapis.com prefix if no prefix is used
91+
if (prefix === "") {
92+
prefix = googleApi;
93+
}
94+
var name = prefix + messageName;
95+
object["@type"] = name;
7896
return object;
7997
}
8098

tests/comp_google_protobuf_any.js

Lines changed: 2 additions & 2 deletions
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)