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 -1
View File
@@ -1,11 +1,14 @@
# CHANGELOG
## master
## 6.5.2 Oct 26, 2021
Upgrade priority: Low. Recommended for users with `.at` queries on unknown blocks.
Changes:
- Adjust `toHuman` on `Call` to return names & (not only) values
- Allow for optional known `RuntimeVersion` to short-circuit `api.at`
- Ensure `Struct` decodes fully on `null` input (equivalent to `undefined` & `{}`)
## 6.5.1 Oct 24, 2021
+2 -2
View File
@@ -12,7 +12,7 @@
},
"sideEffects": false,
"type": "commonjs",
"version": "6.5.2-3",
"version": "6.5.2",
"workspaces": [
"packages/*"
],
@@ -44,4 +44,4 @@
"resolutions": {
"typescript": "^4.4.4"
}
}
}
+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;
+27 -27
View File
@@ -1833,40 +1833,40 @@ __metadata:
resolution: "@polkadot/api-contract@workspace:packages/api-contract"
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
languageName: unknown
linkType: soft
"@polkadot/api-derive@6.5.2-3, @polkadot/api-derive@workspace:packages/api-derive":
"@polkadot/api-derive@6.5.2, @polkadot/api-derive@workspace:packages/api-derive":
version: 0.0.0-use.local
resolution: "@polkadot/api-derive@workspace:packages/api-derive"
dependencies:
"@babel/runtime": ^7.15.4
"@polkadot/api": 6.5.2-3
"@polkadot/api": 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/rpc-core": 6.5.2
"@polkadot/rpc-provider": 6.5.2
"@polkadot/types": 6.5.2
"@polkadot/util": ^7.6.1
"@polkadot/util-crypto": ^7.6.1
rxjs: ^7.4.0
languageName: unknown
linkType: soft
"@polkadot/api@6.5.2-3, @polkadot/api@workspace:packages/api":
"@polkadot/api@6.5.2, @polkadot/api@workspace:packages/api":
version: 0.0.0-use.local
resolution: "@polkadot/api@workspace:packages/api"
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
@@ -1991,26 +1991,26 @@ __metadata:
languageName: node
linkType: hard
"@polkadot/rpc-core@6.5.2-3, @polkadot/rpc-core@workspace:packages/rpc-core":
"@polkadot/rpc-core@6.5.2, @polkadot/rpc-core@workspace:packages/rpc-core":
version: 0.0.0-use.local
resolution: "@polkadot/rpc-core@workspace:packages/rpc-core"
dependencies:
"@babel/runtime": ^7.15.4
"@polkadot/keyring": ^7.6.1
"@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
languageName: unknown
linkType: soft
"@polkadot/rpc-provider@6.5.2-3, @polkadot/rpc-provider@workspace:packages/rpc-provider":
"@polkadot/rpc-provider@6.5.2, @polkadot/rpc-provider@workspace:packages/rpc-provider":
version: 0.0.0-use.local
resolution: "@polkadot/rpc-provider@workspace:packages/rpc-provider"
dependencies:
"@babel/runtime": ^7.15.4
"@polkadot/keyring": ^7.6.1
"@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
@@ -2038,10 +2038,10 @@ __metadata:
"@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
"@types/websocket": ^1.0.4
"@types/yargs": ^17.0.4
@@ -2057,18 +2057,18 @@ __metadata:
languageName: unknown
linkType: soft
"@polkadot/types-known@6.5.2-3, @polkadot/types-known@workspace:packages/types-known":
"@polkadot/types-known@6.5.2, @polkadot/types-known@workspace:packages/types-known":
version: 0.0.0-use.local
resolution: "@polkadot/types-known@workspace:packages/types-known"
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
languageName: unknown
linkType: soft
"@polkadot/types-support@6.5.2-3, @polkadot/types-support@workspace:packages/types-support":
"@polkadot/types-support@6.5.2, @polkadot/types-support@workspace:packages/types-support":
version: 0.0.0-use.local
resolution: "@polkadot/types-support@workspace:packages/types-support"
dependencies:
@@ -2077,13 +2077,13 @@ __metadata:
languageName: unknown
linkType: soft
"@polkadot/types@6.5.2-3, @polkadot/types@workspace:packages/types":
"@polkadot/types@6.5.2, @polkadot/types@workspace:packages/types":
version: 0.0.0-use.local
resolution: "@polkadot/types@workspace:packages/types"
dependencies:
"@babel/runtime": ^7.15.4
"@polkadot/keyring": ^7.6.1
"@polkadot/types-support": 6.5.2-3
"@polkadot/types-support": 6.5.2
"@polkadot/util": ^7.6.1
"@polkadot/util-crypto": ^7.6.1
"@types/bn.js": ^4.11.6