Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/protobuf-test/src/constructor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,34 @@ import {
import { TestAllTypesProto3 as JS_TestAllTypesProto3 } from "./gen/js/google/protobuf/test_messages_proto3_pb.js";
import { testMT } from "./helpers.js";

describe("constructor takes message partial for message field", function () {
testMT(
{ ts: TS_TestAllTypesProto3, js: JS_TestAllTypesProto3 },
(messageType) => {
const m = new messageType({
recursiveMessage: {
optionalInt32: 123,
},
});
expect(m.recursiveMessage?.optionalInt32).toBe(123);
}
);
});

describe("constructor takes message instance for message field", function () {
testMT(
{ ts: TS_TestAllTypesProto3, js: JS_TestAllTypesProto3 },
(messageType) => {
const m = new messageType({
recursiveMessage: new messageType({
optionalInt32: 123,
}),
});
expect(m.recursiveMessage?.optionalInt32).toBe(123);
}
);
});

describe("constructor takes partial message for oneof field", function () {
testMT(
{ ts: TS_TestAllTypesProto3, js: JS_TestAllTypesProto3 },
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/private/util-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function makeUtilCommon(): Omit<Util, "newFieldList" | "initFields"> {
if (mt.fieldWrapper) {
t[localName] = val;
} else {
t[localName] = val instanceof mt ? mt : new mt(val);
t[localName] = val instanceof mt ? val : new mt(val);
Copy link
Member Author

Choose a reason for hiding this comment

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

This code runs when you call new MyMessage(data). We're walking through the metadata (message fields) here, and pick values from the data. If the value for a message field is already an instance of the expected message type, we can use it as is. But instead of doing that, we do something silly and use the message type.

Copy link
Member

Choose a reason for hiding this comment

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

Lol, yeah having it set to mt IS very weird. Definitely explains some other weird behaviour i was seeing.

}
}
break;
Expand Down