diff --git a/src/node/index.js b/src/node/index.js index ba73e3dac8f..071bfd7927b 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -64,6 +64,8 @@ grpc.setDefaultRootsPem(fs.readFileSync(SSL_ROOTS_PATH, 'ascii')); * Buffers. Defaults to false * - longsAsStrings: deserialize long values as strings instead of objects. * Defaults to true + * - enumsAsStrings: deserialize enum values as strings instead of numbers. + * Defaults to true * - deprecatedArgumentOrder: Use the beta method argument order for client * methods, with optional arguments after the callback. Defaults to false. * This option is only a temporary stopgap measure to smooth an API breakage. @@ -131,6 +133,8 @@ function applyProtoRoot(filename, root) { * Buffers. Defaults to false * - longsAsStrings: deserialize long values as strings instead of objects. * Defaults to true + * - enumsAsStrings: deserialize enum values as strings instead of numbers. + * Defaults to true * - deprecatedArgumentOrder: Use the beta method argument order for client * methods, with optional arguments after the callback. Defaults to false. * This option is only a temporary stopgap measure to smooth an API breakage. diff --git a/src/node/src/common.js b/src/node/src/common.js index 93df5138172..757969dbddb 100644 --- a/src/node/src/common.js +++ b/src/node/src/common.js @@ -87,5 +87,6 @@ exports.defaultGrpcOptions = { convertFieldsToCamelCase: false, binaryAsBase64: false, longsAsStrings: true, + enumsAsStrings: true, deprecatedArgumentOrder: false }; diff --git a/src/node/src/protobuf_js_6_common.js b/src/node/src/protobuf_js_6_common.js index 6a85e4ac23d..baa62cce866 100644 --- a/src/node/src/protobuf_js_6_common.js +++ b/src/node/src/protobuf_js_6_common.js @@ -50,7 +50,7 @@ exports.deserializeCls = function deserializeCls(cls, options) { defaults: true, bytes: options.binaryAsBase64 ? String : Buffer, longs: options.longsAsStrings ? String : null, - enums: String, + enums: options.enumsAsStrings ? String : null, oneofs: true }; /** diff --git a/src/node/test/common_test.js b/src/node/test/common_test.js index 4b7a8c22537..39ff6a5f1fc 100644 --- a/src/node/test/common_test.js +++ b/src/node/test/common_test.js @@ -179,3 +179,25 @@ describe('Proto message oneof serialize and deserialize', function() { assert.equal(deserialized2.oneof_choice, 'int_choice'); }); }); +describe('Proto message enum serialize and deserialize', function() { + var enumSerialize = serializeCls(messages_proto.EnumValues); + var enumDeserialize = deserializeCls( + messages_proto.EnumValues, default_options); + var enumIntOptions = _.defaults({enumsAsStrings: false}, default_options); + var enumIntDeserialize = deserializeCls( + messages_proto.EnumValues, enumIntOptions); + it('Should accept both names and numbers', function() { + var nameSerialized = enumSerialize({enum_value: 'ONE'}); + var numberSerialized = enumSerialize({enum_value: 1}); + assert.strictEqual(messages_proto.TestEnum.ONE, 1); + assert.deepEqual(enumDeserialize(nameSerialized), + enumDeserialize(numberSerialized)); + }); + it('Should deserialize as a string the enumsAsStrings option', function() { + var serialized = enumSerialize({enum_value: 'TWO'}); + var nameDeserialized = enumDeserialize(serialized); + var numberDeserialized = enumIntDeserialize(serialized); + assert.deepEqual(nameDeserialized, {enum_value: 'TWO'}); + assert.deepEqual(numberDeserialized, {enum_value: 2}); + }); +}); diff --git a/src/node/test/test_messages.proto b/src/node/test/test_messages.proto index 2cd133161b7..ae70f6e152a 100644 --- a/src/node/test/test_messages.proto +++ b/src/node/test/test_messages.proto @@ -47,4 +47,14 @@ message OneOfValues { int32 int_choice = 1; string string_choice = 2; } +} + +enum TestEnum { + ZERO = 0; + ONE = 1; + TWO = 2; +} + +message EnumValues { + TestEnum enum_value = 1; } \ No newline at end of file