From 9ce74727ab4f1820d985757a4249ae3cf6721a6e Mon Sep 17 00:00:00 2001 From: Nick Pestov Date: Fri, 8 Jun 2018 12:35:42 +1000 Subject: [PATCH 1/3] google.protobuf.Any fixes In `fromObject`, only use type name after the last '/' when doing a lookup and make sure the `type_url` prefix is preserved. In `toObject`, use `type.googleapis.com` default prefix if no `type_url` prefix is used. --- src/wrappers.js | 26 ++++++++++++++++++++++---- tests/comp_google_protobuf_any.js | 4 ++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/wrappers.js b/src/wrappers.js index 710aab0d2..aad96c785 100644 --- a/src/wrappers.js +++ b/src/wrappers.js @@ -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) @@ -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; } diff --git a/tests/comp_google_protobuf_any.js b/tests/comp_google_protobuf_any.js index 9027754af..342dcfb08 100644 --- a/tests/comp_google_protobuf_any.js +++ b/tests/comp_google_protobuf_any.js @@ -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: { @@ -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(); }); From 206ae184fdf700dc1a0a11dd3be4a4121ef4b3de Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Fri, 29 May 2020 14:08:44 -0700 Subject: [PATCH 2/3] fix: lint --- src/wrappers.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wrappers.js b/src/wrappers.js index aad96c785..c56ca4a91 100644 --- a/src/wrappers.js +++ b/src/wrappers.js @@ -69,11 +69,12 @@ wrappers[".google.protobuf.Any"] = { // Default prefix var googleApi = "type.googleapis.com/"; var prefix = ""; + var name = ""; // 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); + 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); @@ -91,7 +92,7 @@ wrappers[".google.protobuf.Any"] = { if (prefix === "") { prefix = googleApi; } - var name = prefix + messageName; + name = prefix + messageName; object["@type"] = name; return object; } From f08df73a7c02e15e09cbbefe89af35c62487b43e Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Fri, 29 May 2020 14:19:36 -0700 Subject: [PATCH 3/3] fix: more lint --- src/wrappers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wrappers.js b/src/wrappers.js index c56ca4a91..097b8ff0b 100644 --- a/src/wrappers.js +++ b/src/wrappers.js @@ -60,7 +60,7 @@ wrappers[".google.protobuf.Any"] = { }); } } - + return this.fromObject(object); }, @@ -76,7 +76,7 @@ wrappers[".google.protobuf.Any"] = { // Only use fully qualified type name after the last '/' 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); + prefix = message.type_url.substring(0, message.type_url.lastIndexOf("/") + 1); var type = this.lookup(name); /* istanbul ignore else */ if (type)