Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing null #1858

Open
lautarodragan opened this issue Jan 30, 2023 · 0 comments
Open

Allow passing null #1858

lautarodragan opened this issue Jan 30, 2023 · 0 comments

Comments

@lautarodragan
Copy link

lautarodragan commented Jan 30, 2023

protobuf.js version: <7.2.0>

Related issue: #677

Hey there! I'm in need of being able to pass null for fields from my public API to private gRPC services.

I understand the best approach for this is using the well-known wrapper types, such as StringValue.

I also want to do automatic wrapping and unwrapping of these wrapper types.

I am able to encode {} for these fields, which I can interpret as absence of value (but presence of key), but I'd like to use null everywhere.

I looked into a few already-existing reported issues, and understand I can override the toObject and fromObject of protobuf.wrappers['.google.protobuf.StringValue'] to achieve this, but this only seems to work in the toObject phase — the fromObject never receives null values.

Example code (js, proto and stdout): https://gist.github.com/lautarodragan/52fd05a9e261b7e22c27f4eae41baf1a

I see the generated code for MyHappyProto.fromObject is this:

(function anonymous(types,util
) {
return function MyHappyProto$fromObject(d){
  if(d instanceof this.ctor)
  return d
  var m=new this.ctor
  if(d.id!=null){
  m.id=String(d.id)
  }
  if(d.name!=null){
  if(typeof d.name!=="object")
  throw TypeError(".MyHappyProto.name: object expected")
  m.name=types[1].fromObject(d.name)
  }
  if(d.address!=null){
  if(typeof d.address!=="object")
  throw TypeError(".MyHappyProto.address: object expected")
  m.address=types[2].fromObject(d.address)
  }
  if(d.something!=null){
  if(typeof d.something!=="object")
  throw TypeError(".MyHappyProto.something: object expected")
  m.something=types[3].fromObject(d.something)
  }
  if(d.nullable!=null){
  if(typeof d.nullable!=="object")
  throw TypeError(".MyHappyProto.nullable: object expected")
  m.nullable=types[4].fromObject(d.nullable)
  }
  return m
}
})

If the field is null, fromObject is not called at all — I get no chance to transform null into {} in the fromObject phase.

I guess these checks come from

("if(d%s!=null){", prop); // !== undefined && !== null
genValuePartial_fromObject(gen, field, /* not sorted */ i, prop);
if (!(field.resolvedType instanceof Enum)) gen
("}");
, but I have no idea because I don't really understand this code gen.

The toObject correctly transforms {} into null and { value: 'string' } into string.

Can these checks be disabled somehow? Otherwise, is there an alternative to fromObject that I can work with?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant