[JavaScript] Getters returning nulls instead of default values for primitives

Open
#35 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript
Domain
tooling

Research direction

Start by tracing generated primitive getters that call jspb.Message.getFieldWithDefault and how the js_out parameter is parsed. Review the generator's existing option handling and getter-generation tests, if present. Done means a documented option controls whether optional primitive getters preserve unset values, with coverage for proto2 numeric fields.

Written by the indexing model from the issue text.

Description

enhancement javascript port-fix triaged

What language does this apply to?

JavaScript

Describe the problem you are trying to solve.

Currently, all generated getters for primitives in _pb.js files call to jspb.Message.getFieldWithDefault and there seem to be no way to tell generator to simply call jspb.Message.getField instead.
This is particularly problematic when using proto2 syntax with required/optional numeric fields. If an optional field is not set, it will default to "0".

Describe the solution you'd like

Ideally there would be an option passed to the js_out parameter of protoc where we could say whether or not to use defaults.

Describe alternatives you've considered

One could potentially leverage hasMyField() to determine if the field has been set or not, but it would make the mapping layer between proto and internal app model even more verbose, as this would need to be done on all optional fields, which we have quite a few.

Dominant language
JavaScript
Stars
471
Forks
92
PR merge metrics
No merged PRs in 30d

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from protocolbuffers/protobuf-javascript

All issues in protocolbuffers/protobuf-javascript

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.