Fix Struct decoding for null (#4112)

* Fix Struct decoding for null

* CHANGELOG

* 6.5.2
This commit is contained in:
Jaco
2021-10-26 08:46:03 +03:00
committed by GitHub
parent 9ae87bed78
commit 12a54efde5
14 changed files with 84 additions and 91 deletions
+4 -5
View File
@@ -10,7 +10,6 @@
"license": "Apache-2.0",
"maintainers": [],
"name": "@polkadot/api-contract",
"private": false,
"repository": {
"directory": "packages/api-contract",
"type": "git",
@@ -21,13 +20,13 @@
"./detectPackage.cjs"
],
"type": "module",
"version": "6.5.2-3",
"version": "6.5.2",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.15.4",
"@polkadot/api": "6.5.2-3",
"@polkadot/types": "6.5.2-3",
"@polkadot/api": "6.5.2",
"@polkadot/types": "6.5.2",
"@polkadot/util": "^7.6.1",
"rxjs": "^7.4.0"
}
}
}
+6 -7
View File
@@ -10,7 +10,6 @@
"license": "Apache-2.0",
"maintainers": [],
"name": "@polkadot/api-derive",
"private": false,
"repository": {
"directory": "packages/api-derive",
"type": "git",
@@ -21,19 +20,19 @@
"./detectPackage.cjs"
],
"type": "module",
"version": "6.5.2-3",
"version": "6.5.2",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.15.4",
"@polkadot/api": "6.5.2-3",
"@polkadot/rpc-core": "6.5.2-3",
"@polkadot/types": "6.5.2-3",
"@polkadot/api": "6.5.2",
"@polkadot/rpc-core": "6.5.2",
"@polkadot/types": "6.5.2",
"@polkadot/util": "^7.6.1",
"@polkadot/util-crypto": "^7.6.1",
"rxjs": "^7.4.0"
},
"devDependencies": {
"@polkadot/keyring": "^7.6.1",
"@polkadot/rpc-provider": "6.5.2-3"
"@polkadot/rpc-provider": "6.5.2"
}
}
}
+7 -8
View File
@@ -10,7 +10,6 @@
"license": "Apache-2.0",
"maintainers": [],
"name": "@polkadot/api",
"private": false,
"repository": {
"directory": "packages/api",
"type": "git",
@@ -21,19 +20,19 @@
"./detectPackage.cjs"
],
"type": "module",
"version": "6.5.2-3",
"version": "6.5.2",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.15.4",
"@polkadot/api-derive": "6.5.2-3",
"@polkadot/api-derive": "6.5.2",
"@polkadot/keyring": "^7.6.1",
"@polkadot/rpc-core": "6.5.2-3",
"@polkadot/rpc-provider": "6.5.2-3",
"@polkadot/types": "6.5.2-3",
"@polkadot/types-known": "6.5.2-3",
"@polkadot/rpc-core": "6.5.2",
"@polkadot/rpc-provider": "6.5.2",
"@polkadot/types": "6.5.2",
"@polkadot/types-known": "6.5.2",
"@polkadot/util": "^7.6.1",
"@polkadot/util-crypto": "^7.6.1",
"eventemitter3": "^4.0.7",
"rxjs": "^7.4.0"
}
}
}
+4 -5
View File
@@ -10,7 +10,6 @@
"license": "Apache-2.0",
"maintainers": [],
"name": "@polkadot/rpc-core",
"private": false,
"repository": {
"directory": "packages/rpc-core",
"type": "git",
@@ -21,16 +20,16 @@
"./detectPackage.cjs"
],
"type": "module",
"version": "6.5.2-3",
"version": "6.5.2",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.15.4",
"@polkadot/rpc-provider": "6.5.2-3",
"@polkadot/types": "6.5.2-3",
"@polkadot/rpc-provider": "6.5.2",
"@polkadot/types": "6.5.2",
"@polkadot/util": "^7.6.1",
"rxjs": "^7.4.0"
},
"devDependencies": {
"@polkadot/keyring": "^7.6.1"
}
}
}
+4 -5
View File
@@ -10,7 +10,6 @@
"license": "Apache-2.0",
"maintainers": [],
"name": "@polkadot/rpc-provider",
"private": false,
"repository": {
"directory": "packages/rpc-provider",
"type": "git",
@@ -21,11 +20,11 @@
"./detectPackage.cjs"
],
"type": "module",
"version": "6.5.2-3",
"version": "6.5.2",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.15.4",
"@polkadot/types": "6.5.2-3",
"@polkadot/types": "6.5.2",
"@polkadot/util": "^7.6.1",
"@polkadot/util-crypto": "^7.6.1",
"@polkadot/x-fetch": "^7.6.1",
@@ -35,8 +34,8 @@
},
"devDependencies": {
"@polkadot/keyring": "^7.6.1",
"@polkadot/types": "6.5.2-3",
"@polkadot/types": "6.5.2",
"mock-socket": "^9.0.6",
"nock": "^13.1.4"
}
}
}
+6 -7
View File
@@ -10,7 +10,6 @@
"license": "Apache-2.0",
"maintainers": [],
"name": "@polkadot/typegen",
"private": false,
"repository": {
"directory": "packages/typegen",
"type": "git",
@@ -21,7 +20,7 @@
"./detectPackage.cjs"
],
"type": "module",
"version": "6.5.2-3",
"version": "6.5.2",
"main": "index.js",
"bin": {
"polkadot-types-chain-info": "./scripts/polkadot-types-chain-info.cjs",
@@ -34,10 +33,10 @@
"@babel/core": "^7.15.8",
"@babel/register": "^7.15.3",
"@babel/runtime": "^7.15.4",
"@polkadot/api": "6.5.2-3",
"@polkadot/rpc-provider": "6.5.2-3",
"@polkadot/types": "6.5.2-3",
"@polkadot/types-support": "6.5.2-3",
"@polkadot/api": "6.5.2",
"@polkadot/rpc-provider": "6.5.2",
"@polkadot/types": "6.5.2",
"@polkadot/types-support": "6.5.2",
"@polkadot/util": "^7.6.1",
"handlebars": "^4.7.7",
"websocket": "^1.0.34",
@@ -47,4 +46,4 @@
"@types/websocket": "^1.0.4",
"@types/yargs": "^17.0.4"
}
}
}
+3 -4
View File
@@ -10,7 +10,6 @@
"license": "Apache-2.0",
"maintainers": [],
"name": "@polkadot/types-known",
"private": false,
"repository": {
"directory": "packages/types-known",
"type": "git",
@@ -21,12 +20,12 @@
"./detectPackage.cjs"
],
"type": "module",
"version": "6.5.2-3",
"version": "6.5.2",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.15.4",
"@polkadot/networks": "^7.6.1",
"@polkadot/types": "6.5.2-3",
"@polkadot/types": "6.5.2",
"@polkadot/util": "^7.6.1"
}
}
}
+2 -3
View File
@@ -10,7 +10,6 @@
"license": "Apache-2.0",
"maintainers": [],
"name": "@polkadot/types-support",
"private": false,
"repository": {
"directory": "packages/types-support",
"type": "git",
@@ -21,10 +20,10 @@
"./detectPackage.cjs"
],
"type": "module",
"version": "6.5.2-3",
"version": "6.5.2",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.15.4",
"@polkadot/util": "^7.6.1"
}
}
}
+3 -4
View File
@@ -10,7 +10,6 @@
"license": "Apache-2.0",
"maintainers": [],
"name": "@polkadot/types",
"private": false,
"repository": {
"directory": "packages/types",
"type": "git",
@@ -21,7 +20,7 @@
"./detectPackage.cjs"
],
"type": "module",
"version": "6.5.2-3",
"version": "6.5.2",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.15.4",
@@ -31,8 +30,8 @@
},
"devDependencies": {
"@polkadot/keyring": "^7.6.1",
"@polkadot/types-support": "6.5.2-3",
"@polkadot/types-support": "6.5.2",
"@types/bn.js": "^4.11.6",
"bn.js": "^4.12.0"
}
}
}
+10 -9
View File
@@ -52,15 +52,16 @@ describe('Struct', (): void => {
testEncode('toString', '{"foo":"bazzing","bar":69}');
});
it('decodes null', (): void => {
expect(
new (
Struct.with({
txt: Text,
u32: U32
})
)(registry, null).toString()
).toEqual('{}');
it('decodes null/undefined/empty correctly (& equivalently)', (): void => {
const Clazz = Struct.with({
txt: Text,
u32: U32
});
const expected = { txt: '', u32: '0' };
expect(new Clazz(registry, {}).toHuman()).toEqual(expected);
expect(new Clazz(registry, null).toHuman()).toEqual(expected);
expect(new Clazz(registry, undefined).toHuman()).toEqual(expected);
});
it('decodes reusing instantiated inputs', (): void => {
+2 -4
View File
@@ -113,12 +113,10 @@ function decodeStruct <T> (registry: Registry, Types: ConstructorDef, value: unk
return raw;
}, {} as T);
} else if (!value) {
return {} as T;
}
// We assume from here that value is a JS object (Array, Map, Object)
return decodeStructFromObject(registry, Types, value, jsonMap);
return decodeStructFromObject(registry, Types, value || {}, jsonMap);
}
/**
@@ -147,7 +145,7 @@ export class Struct<
readonly #Types: ConstructorDef;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
constructor (registry: Registry, Types: S, value: V | Map<unknown, unknown> | unknown[] | string = {} as V, jsonMap: Map<keyof S, string> = new Map()) {
constructor (registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | string | null, jsonMap: Map<keyof S, string> = new Map()) {
super(Object.entries(decodeStruct(registry, mapToTypeMap(registry, Types), value, jsonMap)));
this.registry = registry;