Skip to content

Commit

Permalink
fix: Compilation error for nested repeated fields with useOptionals=a…
Browse files Browse the repository at this point in the history
…ll (#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.
  • Loading branch information
vecerek committed Sep 29, 2024
1 parent ffacf3a commit 08981bf
Show file tree
Hide file tree
Showing 223 changed files with 5,168 additions and 2,140 deletions.
3 changes: 2 additions & 1 deletion integration/angular/simple-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ export const SimpleMessage: MessageFns<SimpleMessage> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 8) {
break;
}

message.numberField = reader.int32();
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down
3 changes: 2 additions & 1 deletion integration/async-iterable-services-abort-signal/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ export const EchoMsg: MessageFns<EchoMsg> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 10) {
break;
}

message.body = reader.string();
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down
3 changes: 2 additions & 1 deletion integration/async-iterable-services/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ export const EchoMsg: MessageFns<EchoMsg> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 10) {
break;
}

message.body = reader.string();
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down
6 changes: 4 additions & 2 deletions integration/avoid-import-conflicts-folder-name/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,22 @@ export const Simple: MessageFns<Simple> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 10) {
break;
}

message.simple2Name = reader.string();
continue;
case 2:
}
case 2: {
if (tag !== 16) {
break;
}

message.simple2Age = reader.int32();
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down
12 changes: 8 additions & 4 deletions integration/avoid-import-conflicts-folder-name/ui/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,22 @@ export const Simple: MessageFns<Simple> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 10) {
break;
}

message.name = reader.string();
continue;
case 2:
}
case 2: {
if (tag !== 18) {
break;
}

message.otherSimple = Simple2.decode(reader, reader.uint32());
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down Expand Up @@ -159,20 +161,22 @@ export const SimpleEnums: MessageFns<SimpleEnums> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 8) {
break;
}

message.localEnum = reader.int32() as any;
continue;
case 2:
}
case 2: {
if (tag !== 16) {
break;
}

message.importEnum = reader.int32() as any;
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down
24 changes: 16 additions & 8 deletions integration/avoid-import-conflicts/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,22 @@ export const Simple: MessageFns<Simple> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 10) {
break;
}

message.name = reader.string();
continue;
case 2:
}
case 2: {
if (tag !== 18) {
break;
}

message.otherSimple = Simple3.decode(reader, reader.uint32());
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down Expand Up @@ -175,20 +177,22 @@ export const DifferentSimple: MessageFns<DifferentSimple> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 10) {
break;
}

message.name = reader.string();
continue;
case 2:
}
case 2: {
if (tag !== 18) {
break;
}

message.otherOptionalSimple2 = Simple3.decode(reader, reader.uint32());
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down Expand Up @@ -253,20 +257,22 @@ export const SimpleEnums: MessageFns<SimpleEnums> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 8) {
break;
}

message.localEnum = reader.int32() as any;
continue;
case 2:
}
case 2: {
if (tag !== 16) {
break;
}

message.importEnum = reader.int32() as any;
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down Expand Up @@ -324,13 +330,14 @@ export const FooServiceCreateRequest: MessageFns<FooServiceCreateRequest> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 8) {
break;
}

message.kind = reader.int32() as any;
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down Expand Up @@ -381,13 +388,14 @@ export const FooServiceCreateResponse: MessageFns<FooServiceCreateResponse> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 8) {
break;
}

message.kind = reader.int32() as any;
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down
6 changes: 4 additions & 2 deletions integration/avoid-import-conflicts/simple2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,22 @@ export const Simple: MessageFns<Simple> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 10) {
break;
}

message.name = reader.string();
continue;
case 2:
}
case 2: {
if (tag !== 16) {
break;
}

message.age = reader.int32();
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down
6 changes: 4 additions & 2 deletions integration/barrel-imports/bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@ export const Bar: MessageFns<Bar> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 10) {
break;
}

message.name = reader.string();
continue;
case 2:
}
case 2: {
if (tag !== 16) {
break;
}

message.age = reader.int32();
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down
6 changes: 4 additions & 2 deletions integration/barrel-imports/foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ export const Foo: MessageFns<Foo> = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
case 1: {
if (tag !== 10) {
break;
}

message.name = reader.string();
continue;
case 2:
}
case 2: {
if (tag !== 18) {
break;
}

message.bar = Bar.decode(reader, reader.uint32());
continue;
}
}
if ((tag & 7) === 4 || tag === 0) {
break;
Expand Down
Loading

0 comments on commit 08981bf

Please sign in to comment.