-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
fix: fromObject should not initialize oneof members #1597
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alexander-fenster
force-pushed
the
cli-test
branch
from
April 28, 2021 07:43
a2436b6
to
343e127
Compare
alexander-fenster
force-pushed
the
cli-test
branch
from
April 28, 2021 07:56
76e7cb2
to
38d45bb
Compare
alexander-fenster
force-pushed
the
cli-test
branch
from
April 28, 2021 08:45
38d45bb
to
893b164
Compare
murgatroid99
approved these changes
Apr 28, 2021
alexander-fenster
added a commit
that referenced
this pull request
Apr 29, 2021
* test: adding test for pbjs static code generation * fix: fromObject should not initialize oneof members
This was referenced Apr 29, 2021
Closed
This was referenced May 20, 2022
alexander-fenster
added a commit
that referenced
this pull request
Jul 7, 2022
* feat: proto3 optional support * chore: pre-release v6.11.0-pre * fix: rebuild * fix: fromObject should not initialize oneof members (#1597) * test: adding test for pbjs static code generation * fix: fromObject should not initialize oneof members * chore: release v6.11.0 * chore: rebuild * feat: add --no-service option for pbjs static target (#1577) This option skips generation of service clients. Co-authored-by: Alexander Fenster <[email protected]> * deps: set @types/node to >= (#1575) * deps: set @types/node to star version When using `protobuf.js` as a dependency in a project it is important that `@types/node` package gets de-duped and has the same version as for the rest of the modules in the project. Otherwise, typing conflicts could happen as they do between v13 and v14 node types. * fix: use @types/node >=13.7.0 * fix: use @types/node >=13.7.0 Co-authored-by: Alexander Fenster <[email protected]> Co-authored-by: Alexander Fenster <[email protected]> * chore: rebuild * docs: update changelog * fix: parse.js "parent.add(oneof)“ error (#1602) Co-authored-by: xiaoweili <[email protected]> * chore: release v6.11.1 * fix(types): bring back Field.rule to .d.ts * fix: rebuild type, release v6.11.2 * build: configure backports * build: configure 6.x as default branch * fix: do not let setProperty change the prototype (#1731) * fix(deps): use eslint 8.x (#1728) * build: run tests if ci label added (#1734) * build: publish to main * chore(6.x): release 6.11.3 (#1737) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Support parsing of complex options * Use readValue to read the proto value and add better example * Fix lint issues * fix: rollback files * Re-do parse logic to take arrays into account and make it simpler Co-authored-by: Alexander Fenster <[email protected]> Co-authored-by: Matthew Douglass <[email protected]> Co-authored-by: Fedor Indutny <[email protected]> Co-authored-by: Alexander Fenster <[email protected]> Co-authored-by: leon <[email protected]> Co-authored-by: xiaoweili <[email protected]> Co-authored-by: Benjamin Coe <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This was referenced Jul 8, 2022
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1596.
The problem happens when an instance of a statically generated class (e.g. generated with
pbjs
) is passed to a dynamically generated.fromObject
(e.g. from a protobufRoot
loaded in the runtime). Since the statically generated instance has all non-optional fields set to their default values in the prototype, including theoneof
fields, the resulting message is incorrect because it has all non-primitive members ofoneof
assigned to the default value (e.g. empty string), which make serialized object deserialize incorrectly by official protobuf implementations (e.g. Python). The implementation of dynamic.fromObject
has a safety check that does nothing if thetypeof
shows it's already an object of the correct type, but this safety check does not work since they technically have different types (static one vs dynamic one) in this particular case.Real life scenario: someone creates an object using
pbjs
generated static class andnew
, then passes it to gRPC client based on proto files loaded dynamically (so the.fromObject
it uses comes from a dynamic class).To fix it, we need to make sure the
oneof
fields don't get initialized with anything other thannull
. This is the same logic I added previously (in #1584), so since proto3optional
fields are actually implemented asoneof
members, I'm just extending the same logic to alloneof
members, not just proto3optional
fields. I think it became clearer.This PR consists of two parts. First, I'm adding a test to the static code generation (basically,
pbjs -t static
), because it's currently not covered by any test. The second commit will bring an actual fix to the problem described above.