protocolbuffers / protocolbuffers/protobuf-javascript

[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.

enhancement javascript port-fix triaged
Dominant language
JavaScript
Stars
471
Forks
91
Avg merge
3h 57m
Merged PRs (30d)
2

Description

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.

Contributor guide

No contributing guide indexed for this repository

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.

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.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.