Skip to content

Commit

Permalink
Fixed wrong type_url for any type (no leading '.' allowed).
Browse files Browse the repository at this point in the history
The reference documentation in google/protobuf/any.proto
defines that a leading "." is not accepted for the type_url
field. So we have to change that to be compatible to the
official Google protobuf implementation.
  • Loading branch information
manni83 committed Jun 13, 2017
1 parent 145bda2 commit c712447
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ wrappers[".google.protobuf.Any"] = {
if (object && object["@type"]) {
var type = this.lookup(object["@type"]);
/* istanbul ignore else */
if (type)
if (type) {
// type_url does not accept leading "."
var type_url = object["@type"].charAt(0) === "." ?
object["@type"].substr(1) : object["@type"];
return this.create({
type_url: object["@type"],
type_url: type_url,
value: type.encode(type.fromObject(object)).finish()
});
}
}

return this.fromObject(object);
Expand Down
8 changes: 4 additions & 4 deletions tests/comp_google_protobuf_any.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ tape.test("google.protobuf.Any", function(test) {

var foo = Foo.fromObject({
foo: {
type_url: ".Bar",
type_url: "Bar",
value: [1 << 3 | 2, 1, 97] // value = "a"
}
});
test.ok(foo.foo instanceof Any.ctor, "should keep explicit Any in fromObject");
test.same(foo.foo, { type_url: ".Bar", value: [10, 1, 97] }, "should keep explicit Any in fromObject properly");
test.same(foo.foo, { type_url: "Bar", value: [10, 1, 97] }, "should keep explicit Any in fromObject properly");

var obj = Foo.toObject(foo);
test.same(obj.foo, { type_url: ".Bar", value: [10, 1, 97] }, "should keep explicit Any in toObject properly");
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");
Expand All @@ -51,7 +51,7 @@ tape.test("google.protobuf.Any", function(test) {
}
});
test.ok(foo.foo instanceof Any.ctor, "should convert to Any in fromObject");
test.same(foo.foo, { type_url: ".Bar", value: protobuf.util.newBuffer([10, 1, 97]) }, "should have correct Any object when converted with fromObject");
test.same(foo.foo, { type_url: "Bar", value: protobuf.util.newBuffer([10, 1, 97]) }, "should have correct Any object when converted with fromObject");

test.end();
});

0 comments on commit c712447

Please sign in to comment.