Skip to content

Make Message.prototype.toObject({defaults: true}) use undefined instead of null? #658

@fnlctrl

Description

@fnlctrl

protobuf.js version: 6.5.0

I'm trying to send an object that's converted from a message (via toObject({defaults: true})) back to server, but I got something like:

Uncaught TypeError: Cannot read property 'freq' of null

The messages looks like

message Foo {
   Bar bar = 1;  // sometimes not provided by server
}
message Bar {
  string freq = 1;
}

and the js

// bar is not provided by server
var foo = Foo.decode(...).toObejct({defaults: true})   // {bar: null}

....some time later

var message = Foo.encode(foo)  // Uncaught TypeError: Cannot read property 'freq' of null

It was working perfectly fine before 6.5.0. Seems to be caused by the generated code only checking for undefined but not null.
image

So maybe we can make toObject use undefined instead of null? Or let the generated code check for both null and undefined?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions