protocolbuffers / protocolbuffers/protobuf-javascript

JS Unexpected behavior when serializing/deserializing

Open
#51 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hello opening this issue because I've seen an unexpected behavior and want to discuss about it and see what's the best pattern

Given the following message:

message MyMessage {
    String pkid = 1;
}

If I set a number field into pkid, I'm able to retrieve it correctly. Once I serialize and then deserialize the message, the value gets coerced as an empty string:

> protoConfig.setPkid(123);
> protoConfig.getPkid();
123
> MyMessage.deserializeBinary((protoConfig.serializeBinary())).getPkid()
""

I wasn't expecting the field to be transformed silently once the message is serialized. What I would expect from order of preference:

  1. Setting the field with setPkid to crash (or a warning) because the type is not what was expected
  2. Serialization crashing (or a warning) because the type is not expected
  3. Coercing the type using toString which would set it to '123'

I understand suggested behaviors may have performance implications but I'm not sure what's the reason of current behavior because this still forces the user to do type checks before setting fields in a protobuf message when using javascript? IMO silently changing the value of a field when serializing a message is dangerous and I would aim for correctness of data first.

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

Reproduce the setPkid/serializeBinary/deserializeBinary sequence shown in the issue. Trace the MyMessage setter and serialization/deserialization entry points to identify where the numeric value becomes an empty string. Done means the expected type handling has a maintainer-approved fix or documented decision.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.