From bd74bcc661dbc5cedd8b4ad24cc55a051c7deb26 Mon Sep 17 00:00:00 2001 From: myron Date: Thu, 25 Jul 2024 10:36:30 +0800 Subject: [PATCH 1/6] code work --- packages/web3-utils/src/formatter.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/web3-utils/src/formatter.ts b/packages/web3-utils/src/formatter.ts index 665965a8eed..a25c69c26a4 100644 --- a/packages/web3-utils/src/formatter.ts +++ b/packages/web3-utils/src/formatter.ts @@ -131,6 +131,11 @@ export const convertScalarValue = (value: unknown, ethType: string, format: Data throw new FormatterError(`Invalid format: ${String(format.bytes)}`); } } + + if (baseType === 'string') { + return String(value); + } + } catch (error) { // If someone didn't use `eth` keyword we can return original value // as the scope of this code is formatting not validation @@ -289,7 +294,7 @@ export const convert = ( } else { for (const [key, value] of Object.entries(object)) { dataPath.push(key); - const schemaProp = findSchemaByDataPath(schema, dataPath, oneOfPath); + let schemaProp = findSchemaByDataPath(schema, dataPath, oneOfPath); // If value is a scaler value if (isNullish(schemaProp)) { @@ -322,6 +327,20 @@ export const convert = ( continue; } + // The following code is basically saying: + // if the schema specifies oneOf, then we are to loop + // over each possible schema and check if they type of the schema specifies format + // and if so we use the oneOfSchemaProp as the schema for formatting + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + if ((schemaProp?.format === undefined) && (schemaProp?.oneOf !== undefined)) { + for (const [_index, oneOfSchemaProp] of schemaProp.oneOf.entries()) { + if ((oneOfSchemaProp?.format !== undefined)) { + schemaProp = oneOfSchemaProp; + break; + } + }; + } + object[key] = convertScalarValue(value, schemaProp.format as string, format); dataPath.pop(); From 40ddd19d704f455b2b2e9ab6775597ce04e0a6a8 Mon Sep 17 00:00:00 2001 From: MyronFanQiu Date: Thu, 25 Jul 2024 12:05:22 +0800 Subject: [PATCH 2/6] add test --- .../web3-utils/test/unit/formatter.test.ts | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/packages/web3-utils/test/unit/formatter.test.ts b/packages/web3-utils/test/unit/formatter.test.ts index aa1a14fb6c9..49658c486a0 100644 --- a/packages/web3-utils/test/unit/formatter.test.ts +++ b/packages/web3-utils/test/unit/formatter.test.ts @@ -479,6 +479,26 @@ describe('formatter', () => { ).toEqual(new Uint8Array([16, 11, 202])); }); }); + + describe('string', () => { + it('should format string for 123', () => { + expect( + format({ format: 'string' }, 123, { + number: FMT_NUMBER.STR, + bytes: FMT_BYTES.HEX, + }), + ).toBe('123'); + }); + + it('should format string for 0x1234', () => { + expect( + format({ format: 'string' }, 0x1234, { + number: FMT_NUMBER.STR, + bytes: FMT_BYTES.UINT8ARRAY, + }), + ).toBe('0x1234'); + }); + }); }); describe('array values', () => { @@ -827,6 +847,32 @@ describe('formatter', () => { expect(result).toEqual(expected); }); + + it('should format object with oneOf', () => { + const schema = { + type: 'object', + properties: { + from: { + format: 'address', + }, + to: { + oneOf: [{ format: 'string' }, { type: 'null' }], + }, + }, + }; + + const data ={ + from: '0x7ed0e85b8e1e925600b4373e6d108f34ab38a401', + to: 123, + } + ; + + const result = { from: '0x7ed0e85b8e1e925600b4373e6d108f34ab38a401', to: '123' }; + + expect( + format(schema, data, { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }), + ).toEqual(result); + }); }); describe('isDataFormat', () => { describe('valid cases', () => { From e14f4c6d8cee687cc19f39c7369db0e4a5769e8f Mon Sep 17 00:00:00 2001 From: mmyyrroonn Date: Tue, 6 Aug 2024 16:06:43 +0800 Subject: [PATCH 3/6] add more test --- .../web3-utils/test/unit/formatter.test.ts | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/web3-utils/test/unit/formatter.test.ts b/packages/web3-utils/test/unit/formatter.test.ts index 49658c486a0..75496662e94 100644 --- a/packages/web3-utils/test/unit/formatter.test.ts +++ b/packages/web3-utils/test/unit/formatter.test.ts @@ -489,15 +489,6 @@ describe('formatter', () => { }), ).toBe('123'); }); - - it('should format string for 0x1234', () => { - expect( - format({ format: 'string' }, 0x1234, { - number: FMT_NUMBER.STR, - bytes: FMT_BYTES.UINT8ARRAY, - }), - ).toBe('0x1234'); - }); }); }); @@ -873,6 +864,30 @@ describe('formatter', () => { format(schema, data, { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }), ).toEqual(result); }); + + it('should format object with oneOf', () => { + const schema = { + type: 'object', + properties: { + from: { + format: 'address', + }, + to: { + oneOf: [{ format: 'string' }, { type: 'null' }], + }, + }, + }; + + const data ={ + from: '0x7ed0e85b8e1e925600b4373e6d108f34ab38a401' + }; + + const result = { from: '0x7ed0e85b8e1e925600b4373e6d108f34ab38a401'}; + + expect( + format(schema, data, { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }), + ).toEqual(result); + }); }); describe('isDataFormat', () => { describe('valid cases', () => { From 4189e86583b7523cea465b24104dc645be41f892 Mon Sep 17 00:00:00 2001 From: mmyyrroonn Date: Tue, 6 Aug 2024 16:20:59 +0800 Subject: [PATCH 4/6] update change log --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f9695c527f..6c0cba8ea45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2650,6 +2650,12 @@ If there are any bugs, improvements, optimizations or any new feature proposal f ## [Unreleased] +### Fixed + +#### web3-utils + +- Fixed ormat schema with `oneOf` doesn't work correctly (#7055) + ### Added #### web3-eth-accounts From 0d6715429eff2f2fe50c23c28a255f181ede0f5f Mon Sep 17 00:00:00 2001 From: mmyyrroonn Date: Tue, 6 Aug 2024 16:29:01 +0800 Subject: [PATCH 5/6] refine change log --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c0cba8ea45..081fb17b6c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2654,7 +2654,7 @@ If there are any bugs, improvements, optimizations or any new feature proposal f #### web3-utils -- Fixed ormat schema with `oneOf` doesn't work correctly (#7055) +- Fixed format schema with `oneOf` doesn't work correctly (#7055) ### Added From 708c442b047f92b01be0c9d308b677ef335f486b Mon Sep 17 00:00:00 2001 From: mmyyrroonn Date: Tue, 6 Aug 2024 16:31:09 +0800 Subject: [PATCH 6/6] refine test --- packages/web3-utils/test/unit/formatter.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web3-utils/test/unit/formatter.test.ts b/packages/web3-utils/test/unit/formatter.test.ts index 75496662e94..9f2bbfc3f1b 100644 --- a/packages/web3-utils/test/unit/formatter.test.ts +++ b/packages/web3-utils/test/unit/formatter.test.ts @@ -865,7 +865,7 @@ describe('formatter', () => { ).toEqual(result); }); - it('should format object with oneOf', () => { + it('should format object with oneOf when property is undefined', () => { const schema = { type: 'object', properties: {