Skip to content

Generate alternate accessor for fields with presence #182

Description

@ksrnnb

When I executed the toObject method on a message with a field marked optional, that field became the default value like 0 or "". I expect it would be undefined.

I guess it is because the third argument of getFieldWithDefault is not undefined.
Since proto3 currently support optional keyword, when optional is specified, I think it should be undefined if it is not given.

proto

syntax = "proto3";

package com.book;

message OptTest {
    optional string name = 1;
}

generated toObject code

proto.com.book.OptTest.toObject = function(includeInstance, msg) {
  var f, obj = {
    name: jspb.Message.getFieldWithDefault(msg, 1, "")
  };

  if (includeInstance) {
    obj.$jspbMessageInstance = msg;
  }
  return obj;
};

versions

$ npm list --depth=0 -g
/usr/local/lib
+-- google-protobuf@3.21.2
+-- grpc_tools_node_protoc_ts@5.3.3
`-- grpc-tools@1.12.4

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions