From c712447b309ae81134c7afd60f8dfa5ecd3be230 Mon Sep 17 00:00:00 2001 From: Manfred Rudigier Date: Tue, 13 Jun 2017 10:30:32 +0200 Subject: [PATCH] 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. --- src/wrappers.js | 8 ++++++-- tests/comp_google_protobuf_any.js | 8 ++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/wrappers.js b/src/wrappers.js index 6253a56c5..3c905e3e6 100644 --- a/src/wrappers.js +++ b/src/wrappers.js @@ -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); diff --git a/tests/comp_google_protobuf_any.js b/tests/comp_google_protobuf_any.js index 1ad01b52e..ee870980d 100644 --- a/tests/comp_google_protobuf_any.js +++ b/tests/comp_google_protobuf_any.js @@ -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"); @@ -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(); });