Skip to content

Commit 7790dc6

Browse files
committed
add test; fix checkType bug (replace by normalization)
1 parent 4cbabbd commit 7790dc6

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

packages/candid/src/idl.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,8 @@ test('IDL opt edge cases', () => {
759759
testDecode(
760760
IDL.Record({a: IDL.Opt(IDL.Variant({ x: IDL.Null, y: IDL.Null }))}),
761761
{a: []},
762-
`4449444c026c0161016b03787f797f7a7f010002`,
763-
// Motoko: {a = #z } : {a : {#x;#y;#z}}
764-
'opt expected, wire type non-opt, extended, with unexpected tag - defaulting',
762+
`4449444c016c01617d010001`,
763+
// Motoko: {a = 1} : {a : Nat}
764+
'opt expected, wire type non-opt, other type - defaulting',
765765
);
766766
});

packages/candid/src/idl.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ export abstract class Type<T = any> {
215215
public abstract encodeType(typeTable: TypeTable): ArrayBuffer;
216216

217217
public abstract checkType(t: Type): Type;
218+
218219
public abstract decodeValue(x: Pipe, t: Type): T;
219220

220221
protected abstract _buildTypeTableImpl(typeTable: TypeTable): void;
@@ -227,6 +228,7 @@ export abstract class PrimitiveType<T = any> extends Type<T> {
227228
}
228229
return t;
229230
}
231+
230232
// eslint-disable-next-line @typescript-eslint/no-unused-vars
231233
public _buildTypeTableImpl(typeTable: TypeTable): void {
232234
// No type table encoding for Primitive types.
@@ -920,7 +922,9 @@ export class OptClass<T> extends ConstructType<[T] | []> {
920922
if (t instanceof ReservedClass) {
921923
return []
922924
}
923-
const wireType = this.checkType(t);
925+
926+
const wireType = (t instanceof RecClass) ? t.getType() : t;
927+
924928
if (wireType instanceof OptClass) {
925929
switch (safeReadUint8(b)) {
926930
case 0:

0 commit comments

Comments
 (0)