Skip to content
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: Compilation error for nested repeated fields with useOptionals=all #1113

Merged

Conversation

vecerek
Copy link
Contributor

@vecerek vecerek commented Sep 29, 2024

Fixes #1112.

The fix is to push the decoded element to the array only if it's not an undefined value.

@vecerek vecerek changed the title fix: Compilation error for nested repeated fields with useOptionals=all fix: Compilation error for nested repeated fields with useOptionals=all Sep 29, 2024
Copy link
Owner

@stephenh stephenh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question about empty strings but otherwise looks great, thanks for the PR @vecerek !

}

const el = StringValue.decode(reader, reader.uint32()).value;
if (el) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vecerek if el was an empty string, we probably want to still .push it? I.e. should we just for undefined instead of truthiness?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're correct! I'll make that change and I've just noticed I didn't push the re-generated TS files and I noticed another problem related to multiple case statements:

Cannot redeclare block-scoped variable 'el'.ts(2451)

I'll have to wrap the cases in a block to address this issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephenh I've just force-pushed the rest of the changes. Thank you for the swift review!

…ll (stephenh#1112)

The TS code generated for the following protobuf code with the `useOptionals=all` parameter does not compile.

```proto
syntax = "proto3";

import "google/protobuf/wrappers.proto";

message NestedList { repeated google.protobuf.StringValue a_string = 1; }

message Example { NestedList list = 1; }
```

The problem lies in the `decode` function not taking into account that the `value` after decoding could also be `undefined`. Therefore the following generated line:

```ts
message.aString!.push(StringValue.decode(reader, reader.uint32()).value);
```

results in the following TS compilation error:

```
Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.ts(2345)
```

The fix is to push the decoded element to the array only if it's not an `undefined` value.
@vecerek vecerek force-pushed the avecerek-fix-repeated-fields-with-use-optional branch from 5d2ffd3 to 08981bf Compare September 29, 2024 18:33
Copy link
Owner

@stephenh stephenh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!

@stephenh stephenh merged commit e89fc51 into stephenh:main Sep 29, 2024
7 checks passed
stephenh pushed a commit that referenced this pull request Sep 29, 2024
## [2.2.1](v2.2.0...v2.2.1) (2024-09-29)

### Bug Fixes

* Compilation error for nested repeated fields with `useOptionals=all` ([#1113](#1113)) ([e89fc51](e89fc51)), closes [#1112](#1112)
@stephenh
Copy link
Owner

🎉 This issue has been resolved in version 2.2.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compilation error for nested repeated fields with useOptionals=all
2 participants