Skip to content

Commit 4f07b9e

Browse files
authored
Merge pull request #11 from postmanlabs/fix-value-well-known-type
Fixed an issue where protoJSON format was not working for google.protobuf.Value
2 parents 2e2f872 + a255c0c commit 4f07b9e

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

src/converter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
4141
} gen
4242
("}");
4343
} else
44-
if (field.resolvedType.fullName === ".google.protobuf.Duration" || field.resolvedType.fullName === ".google.protobuf.Timestamp") { gen
44+
if (field.resolvedType.fullName === ".google.protobuf.Duration" || field.resolvedType.fullName === ".google.protobuf.Timestamp"
45+
|| field.resolvedType.fullName === ".google.protobuf.Value"
46+
) { gen
4547
("m%s=types[%i].fromObject(d%s)", prop, fieldIndex, prop);
4648
} else gen
4749
("if(typeof d%s!==\"object\")", prop)

src/util/is-legacy-struct.js renamed to src/util/is-legacy.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
"use strict";
2-
module.exports = isLegacyStruct;
2+
module.exports = {
3+
isLegacyStruct,
4+
isLegacyValue
5+
};
6+
7+
const valueKeysSet = new Set(["string_value", "number_value", "bool_value", "struct_value", "list_value", "null_value"]);
8+
9+
/**
10+
* Checks if the given payload is in the legacy value format.
11+
*
12+
* @param {object} payload The payload to check for legacy value format
13+
* @returns {boolean} True if the value is in legacy format, false otherwise
14+
*/
15+
function isLegacyValue(payload) {
16+
return payload && typeof payload === "object" && Object.keys(payload).length === 1 && valueKeysSet.has(Object.keys(payload)[0]);
17+
}
318

419
/**
520
* Identifies where the payload for a struct is in the form of a legacy struct.
@@ -25,15 +40,11 @@ module.exports = isLegacyStruct;
2540
* @returns {boolean} True if the payload is in legacy struct format, false otherwise
2641
*/
2742
function isLegacyStruct(payload) {
28-
// Value types in a struct
29-
const valueKeysSet = new Set(["string_value", "number_value", "bool_value", "struct_value", "list_value", "null_value"]);
30-
3143
// If object has only one key and that key is "fields" which is an object
3244
if (payload && Object.keys(payload).length === 1 && payload.fields && typeof payload.fields === "object") {
3345
if (Array.isArray(payload.fields)) {
3446
return payload.fields.every(field => Object.keys(field).length === 2 &&
35-
field.key && field.value && Object.keys(field.value).length === 1
36-
&& valueKeysSet.has(Object.keys(field.value)[0]));
47+
field.key && isLegacyValue(field.value));
3748
}
3849

3950
// Get all the values of the fields object
@@ -42,10 +53,7 @@ function isLegacyStruct(payload) {
4253
const fieldValues = Object.values(payload.fields);
4354

4455
// Check if all the fieldValues have only one key and that key is a valid value type
45-
if (fieldValues.every(fieldValue => {
46-
const fieldValueKeys = fieldValue ? Object.keys(fieldValue) : [];
47-
return fieldValueKeys.length === 1 && valueKeysSet.has(fieldValueKeys[0]);
48-
})) {
56+
if (fieldValues.every(fieldValue => isLegacyValue(fieldValue))) {
4957
return true;
5058
}
5159
}

src/wrappers.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
var wrappers = exports;
99

1010
var Message = require("./message");
11-
var isLegacyStruct = require("./util/is-legacy-struct");
11+
var { isLegacyValue, isLegacyStruct } = require("./util/is-legacy");
1212
var util = require("./util");
1313

1414
/**
@@ -157,6 +157,10 @@ wrappers[".google.protobuf.Value"] = {
157157
// If already a Value instance, return as is
158158
if (object instanceof this.ctor) return object;
159159

160+
if (isLegacyValue(object)) {
161+
return this.create(object);
162+
}
163+
160164
// Handle different types and convert to appropriate Value field
161165
if (object === null || object === undefined) {
162166
return this.create({ null_value: 0 });

0 commit comments

Comments
 (0)