Skip to content

Commit

Permalink
fix: fix error on decoding service type (#731)
Browse files Browse the repository at this point in the history
* fix error on decoding service type

* changelog

* fix

* fix

* fix

* fix

* fix

* add back test
  • Loading branch information
chenyan-dfinity authored Jun 28, 2023
1 parent 97ff6f8 commit 8a9b6cf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/generated/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ <h2>Version x.x.x</h2>
<li>
fix: typo in JsonnableWebAuthnIdentitiy, breaking change that requires users to update their imports to JsonnableWebAuthnIdentity when this type is used
</li>
<li>
fix: fix a bug in decoding service types, when function types come after the service type in the type table
</li>
</ul>
<h2>Version 0.15.7</h2>
<ul>
Expand Down
10 changes: 9 additions & 1 deletion packages/candid/src/idl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ test('IDL encoding (arraybuffer)', () => {
IDL.encode([IDL.Vec(IDL.Nat8)], [new Uint8Array()]);
IDL.encode([IDL.Vec(IDL.Nat8)], [new Uint8Array(100).fill(42)]);
IDL.encode([IDL.Vec(IDL.Nat16)], [new Uint16Array(200).fill(42)]);
expect(() => IDL.encode([IDL.Vec(IDL.Int8)], [new Uint16Array(10).fill(420)])).toThrow(/Invalid vec int8 argument/);
expect(() => IDL.encode([IDL.Vec(IDL.Int8)], [new Uint16Array(10).fill(420)])).toThrow(
/Invalid vec int8 argument/,
);
});

test('IDL encoding (array)', () => {
Expand Down Expand Up @@ -349,6 +351,12 @@ test('IDL encoding (service)', () => {
'4449444c026a0171017d00690103666f6f0001010103caffee',
'service',
);
testDecode(
IDL.Service({ foo: IDL.Func([IDL.Text], [IDL.Nat], []) }),
Principal.fromText('w7x7r-cok77-xa'),
'4449444c02690103666f6f016a0171017d0001010103caffee',
'service',
);
test_(
IDL.Service({ foo: IDL.Func([IDL.Text], [IDL.Nat], ['query']) }),
Principal.fromText('w7x7r-cok77-xa'),
Expand Down
13 changes: 11 additions & 2 deletions packages/candid/src/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1796,8 +1796,17 @@ export function decode(retTypes: Type[], bytes: ArrayBuffer): JsonValue[] {
}

rawTable.forEach((entry, i) => {
const t = buildType(entry);
table[i].fill(t);
// Process function type first, so that we can construct the correct service type
if (entry[0] === IDLTypeIds.Func) {
const t = buildType(entry);
table[i].fill(t);
}
});
rawTable.forEach((entry, i) => {
if (entry[0] !== IDLTypeIds.Func) {
const t = buildType(entry);
table[i].fill(t);
}
});

const types = rawTypes.map(t => getType(t));
Expand Down

0 comments on commit 8a9b6cf

Please sign in to comment.