Skip to content

Commit

Permalink
Fix missing encoding of nested maps in typescript bindings (#11746)
Browse files Browse the repository at this point in the history
The encoder just took the keys & values as is which clearly falls
apart once those actually contain anything else that requires encoding

changelog_begin

- [Typescript Bindings] Fix an issue where nested maps did not get
  encoded properly before sent to the JSON API which caused requests
  to fail with a decoding error on the JSON API.

changelog_end
  • Loading branch information
cocreature authored Nov 18, 2021
1 parent dd88ba2 commit 186ba10
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions language-support/ts/daml-types/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ describe('@daml/types', () => {
expect(m2.get(k)).toEqual(makeArr(m2.values())[makeArr(m2.keys()).findIndex((l) => _.isEqual(k, l))]);
}
});
it('nested genmap', () => {
const {encode, decoder} = Map(Text, Map(Text, Text));
const decoded: Map<Text, Map<Text, Text>> =
emptyMap<Text, Map<Text, Text>>().set("a", emptyMap<Text, Text>().set("b", "c"));
const decode = (u: unknown): Map<Text, Map<Text, Text>> => decoder.runWithException(u);
const encoded = [["a", [["b", "c"]]]];
expect(encode(decoded)).toEqual(encoded);
expect(decode(encoded)).toEqual(decoded);
});
});

test('memo', () => {
Expand Down
2 changes: 1 addition & 1 deletion language-support/ts/daml-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,5 +518,5 @@ export const emptyMap = <K, V>(): Map<K, V> => new MapImpl<K, V>([]);
*/
export const Map = <K, V>(kd: Serializable<K>, vd: Serializable<V>): Serializable<Map<K, V>> => ({
decoder: jtv.array(jtv.tuple([kd.decoder, vd.decoder])).map(kvs => new MapImpl(kvs)),
encode: (m: Map<K, V>): unknown => m.entriesArray(),
encode: (m: Map<K, V>): unknown => m.entriesArray().map(e => [kd.encode(e[0]), vd.encode(e[1])]),
});

0 comments on commit 186ba10

Please sign in to comment.