From 53e24664e4c660826eb10207b480848e677a73e6 Mon Sep 17 00:00:00 2001 From: Max <82761650+MaxMustermann2@users.noreply.github.com> Date: Mon, 22 Apr 2024 22:21:38 +0530 Subject: [PATCH] fix(validator): generate abi name if not provided (#6981) * fix(validator): generate abi name if not provided In the case of public mappings, the ABI generated has a blank name set. This results in blank ids for such inputs within the JSON schema. Post 1f81ff097cf548a327fb72537dbabf2126e616d0, the number of unique ids is used to convert the JSON scheme into Zod, and hence the presence of blank ids is a concern. This commit generates the `abiName` if one is not provided to a value equal to the `${level}/${index}`. Fixes #6965. * fix: add validator changes to root CHANGELOG --------- Co-authored-by: Alex --- CHANGELOG.md | 1 + packages/web3-validator/CHANGELOG.md | 2 +- packages/web3-validator/src/utils.ts | 2 +- .../test/fixtures/abi_to_json_schema.ts | 34 +++++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dcedf476330..873afc8c4ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2477,3 +2477,4 @@ If there are any bugs, improvements, optimizations or any new feature proposal f #### web3-validator +- The JSON schema conversion process now correctly assigns an id when the `abi.name` is not available, for example, in the case of public mappings. (#6981) \ No newline at end of file diff --git a/packages/web3-validator/CHANGELOG.md b/packages/web3-validator/CHANGELOG.md index 07ba353112d..dc5834852ca 100644 --- a/packages/web3-validator/CHANGELOG.md +++ b/packages/web3-validator/CHANGELOG.md @@ -172,4 +172,4 @@ Documentation: ### Fixed -- Nodejs Buffer is not recognized as valid bytes value +- The JSON schema conversion process now correctly assigns an id when the `abi.name` is not available, for example, in the case of public mappings. diff --git a/packages/web3-validator/src/utils.ts b/packages/web3-validator/src/utils.ts index 36a65e3f122..6f9fd6f7f55 100644 --- a/packages/web3-validator/src/utils.ts +++ b/packages/web3-validator/src/utils.ts @@ -141,7 +141,7 @@ export const abiSchemaToJsonSchema = ( // e.g. {name: 'a', type: 'uint'} if (isAbiParameterSchema(abi)) { abiType = abi.type; - abiName = abi.name; + abiName = abi.name || `${level}/${index}`; abiComponents = abi.components as FullValidationSchema; // If its short form string value e.g. ['uint'] } else if (typeof abi === 'string') { diff --git a/packages/web3-validator/test/fixtures/abi_to_json_schema.ts b/packages/web3-validator/test/fixtures/abi_to_json_schema.ts index 35ed35bce6f..e9e4391700c 100644 --- a/packages/web3-validator/test/fixtures/abi_to_json_schema.ts +++ b/packages/web3-validator/test/fixtures/abi_to_json_schema.ts @@ -157,6 +157,40 @@ const abiToJsonSchemaCases: AbiToJsonSchemaCase[] = [ }, }, + // this is for public mappings case where the abi has no name + { + title: 'multiple params of different types without name', + abi: { + fullSchema: [ + { name: '', type: 'uint' }, + { name: '', type: 'int' }, + ], + shortSchema: ['uint', 'int'], + data: [12, -1], + }, + json: { + fullSchema: { + type: 'array', + items: [ + { $id: '/0/0', format: 'uint', required: true }, + { $id: '/0/1', format: 'int', required: true }, + ], + minItems: 2, + maxItems: 2, + }, + shortSchema: { + type: 'array', + items: [ + { $id: '/0/0', format: 'uint', required: true }, + { $id: '/0/1', format: 'int', required: true }, + ], + minItems: 2, + maxItems: 2, + }, + data: [12, -1], + }, + }, + { title: 'single param array', abi: {