Skip to content

Commit c712447

Browse files
committed
Fixed wrong type_url for any type (no leading '.' allowed).
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.
1 parent 145bda2 commit c712447

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

Diff for: src/wrappers.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,15 @@ wrappers[".google.protobuf.Any"] = {
4444
if (object && object["@type"]) {
4545
var type = this.lookup(object["@type"]);
4646
/* istanbul ignore else */
47-
if (type)
47+
if (type) {
48+
// type_url does not accept leading "."
49+
var type_url = object["@type"].charAt(0) === "." ?
50+
object["@type"].substr(1) : object["@type"];
4851
return this.create({
49-
type_url: object["@type"],
52+
type_url: type_url,
5053
value: type.encode(type.fromObject(object)).finish()
5154
});
55+
}
5256
}
5357

5458
return this.fromObject(object);

Diff for: tests/comp_google_protobuf_any.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ tape.test("google.protobuf.Any", function(test) {
3131

3232
var foo = Foo.fromObject({
3333
foo: {
34-
type_url: ".Bar",
34+
type_url: "Bar",
3535
value: [1 << 3 | 2, 1, 97] // value = "a"
3636
}
3737
});
3838
test.ok(foo.foo instanceof Any.ctor, "should keep explicit Any in fromObject");
39-
test.same(foo.foo, { type_url: ".Bar", value: [10, 1, 97] }, "should keep explicit Any in fromObject properly");
39+
test.same(foo.foo, { type_url: "Bar", value: [10, 1, 97] }, "should keep explicit Any in fromObject properly");
4040

4141
var obj = Foo.toObject(foo);
42-
test.same(obj.foo, { type_url: ".Bar", value: [10, 1, 97] }, "should keep explicit Any in toObject properly");
42+
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 });
4545
test.same(obj.foo, { "@type": ".Bar", bar: "a" }, "should decode explicitly Any in toObject if requested");
@@ -51,7 +51,7 @@ tape.test("google.protobuf.Any", function(test) {
5151
}
5252
});
5353
test.ok(foo.foo instanceof Any.ctor, "should convert to Any in fromObject");
54-
test.same(foo.foo, { type_url: ".Bar", value: protobuf.util.newBuffer([10, 1, 97]) }, "should have correct Any object when converted with fromObject");
54+
test.same(foo.foo, { type_url: "Bar", value: protobuf.util.newBuffer([10, 1, 97]) }, "should have correct Any object when converted with fromObject");
5555

5656
test.end();
5757
});

0 commit comments

Comments
 (0)