Skip to content

Commit

Permalink
V2: Fix create() with wrapper oneof field (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm authored Jun 13, 2024
1 parent 55953f2 commit cf191b1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
10 changes: 5 additions & 5 deletions packages/bundle-size/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ usually do. We repeat this for an increasing number of files.
<!--- TABLE-START -->
| code generator | files | bundle size | minified | compressed |
|-----------------|----------|------------------------:|-----------------------:|-------------------:|
| protobuf-es | 1 | 123,170 b | 64,097 b | 14,966 b |
| protobuf-es | 4 | 125,365 b | 65,607 b | 15,622 b |
| protobuf-es | 8 | 128,143 b | 67,378 b | 16,140 b |
| protobuf-es | 16 | 138,651 b | 75,359 b | 18,439 b |
| protobuf-es | 32 | 166,546 b | 97,374 b | 23,911 b |
| protobuf-es | 1 | 123,186 b | 64,107 b | 14,974 b |
| protobuf-es | 4 | 125,381 b | 65,617 b | 15,624 b |
| protobuf-es | 8 | 128,159 b | 67,388 b | 16,147 b |
| protobuf-es | 16 | 138,667 b | 75,369 b | 18,490 b |
| protobuf-es | 32 | 166,562 b | 97,384 b | 23,962 b |
| protobuf-javascript | 1 | 339,613 b | 255,820 b | 42,481 b |
| protobuf-javascript | 4 | 366,281 b | 271,092 b | 43,912 b |
| protobuf-javascript | 8 | 388,324 b | 283,409 b | 45,038 b |
Expand Down
12 changes: 6 additions & 6 deletions packages/bundle-size/chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 29 additions & 1 deletion packages/protobuf-test/src/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,39 @@ describe("create()", () => {
});
});
describe("wkt wrapper field", () => {
test("accepts unwrapped value", () => {
test("accepts unwrapped value only for singular field", () => {
const msg = create(proto3_ts.Proto3MessageDesc, {
singularWrappedUint32Field: 123,
either: {
case: "oneofWrappedUint32Field",
value: {
value: 123,
},
},
repeatedWrappedUint32Field: [{ value: 123 }],
mapInt32WrappedUint32Field: {
123: {
value: 123,
},
},
});
expect(msg.singularWrappedUint32Field).toBe(123);
expect(msg.either.case).toBe("oneofWrappedUint32Field");
if (msg.either.case == "oneofWrappedUint32Field") {
expect(msg.either.value.$typeName).toBe(
"google.protobuf.UInt32Value",
);
expect(msg.either.value.value).toBe(123);
}
expect(msg.repeatedWrappedUint32Field.length).toBe(1);
expect(msg.repeatedWrappedUint32Field[0].$typeName).toBe(
"google.protobuf.UInt32Value",
);
expect(msg.repeatedWrappedUint32Field[0].value).toBe(123);
expect(msg.mapInt32WrappedUint32Field[123].$typeName).toBe(
"google.protobuf.UInt32Value",
);
expect(msg.mapInt32WrappedUint32Field[123].value).toBe(123);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function initMessage<Desc extends DescMessage>(
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- no need to convert enum
switch (field.fieldKind) {
case "message":
if (isWrapperDesc(field.message)) {
if (!field.oneof && isWrapperDesc(field.message)) {
value = initScalar(field.message.fields[0], value);
} else {
value = toMessage(value, field.message);
Expand Down

0 comments on commit cf191b1

Please sign in to comment.