Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format schema with oneOf doesn't work correctly #7055

Open
avkos opened this issue May 22, 2024 · 6 comments
Open

Format schema with oneOf doesn't work correctly #7055

avkos opened this issue May 22, 2024 · 6 comments
Labels
4.x 4.0 related Bug Addressing a bug Good First Issue Great to learn the internals of web3.js

Comments

@avkos
Copy link
Contributor

avkos commented May 22, 2024

const schema = {
  type: 'object',
  properties: {
    from: {
      format: 'address',
    },
    to: {
      oneOf: [{ format: 'string' }, { type: 'null' }],
    },
  },
};

const data = {
  from: '0x7ed0e85b8e1e925600b4373e6d108f34ab38a401',
  to: 123,
};

const res = format(schema, data);

// res { from: '0x7ed0e85b8e1e925600b4373e6d108f34ab38a401', to: 123 }

Actual Behaviour:
to is not formatted

Expected Behaviour:
to should be formatted to a string

@avkos avkos added 4.x 4.0 related Bug Addressing a bug labels May 22, 2024
@Franklin-Osede
Copy link

Hello, I would like to contribute to resolve this issue. Thanks

@mmyyrroonn
Copy link

@Franklin-Osede Hi, are you working on this issue? I want to take over it if you're not yet. 😄

@mconnelly8
Copy link

Hey @mmyyrroonn, it doesn't look like @Franklin-Osede is working on this. Please go ahead and work on this one. We appreciate you wanting to help out.

@mmyyrroonn
Copy link

@avkos @mconnelly8 Hi, after debugging the code, I found the root cause.

The schemaProp would be {"oneOf":[{"format":"string"},{"type":"null"}]} during parsing the to property, which does not have format property. The covertScalarValue would throw an error and skip this property. The code path for this situation is missing in the current codebase.

object[key] = convertScalarValue(value, schemaProp.format as string, format);

Here are three ways to fix this bug in my mind.

  • fix it in the convertScalarValue function
  • fix it in the findSchemaByDataPath function
  • fix it in the convert function by adding extra logic, targeting this bug

I would like to know more about this bug and its background before I start my PR. Is there any guideline we should follow for the oneOf keyword? For example, if the schema is to: {oneOf: [{ format: 'string' }, { format: 'number' }],},, what should we do?

@luu-alex
Copy link
Contributor

Hey there @mmyyrroonn thanks for taking a look into this :) to: {oneOf: [{ format: 'string' }, { format: 'number' }],} I haven't dived into the formatter too much, but i dont believe this should be possible, maybe im not understanding why it would have these two different types of format?

@mmyyrroonn
Copy link

Hey there @mmyyrroonn thanks for taking a look into this :) to: {oneOf: [{ format: 'string' }, { format: 'number' }],} I haven't dived into the formatter too much, but i dont believe this should be possible, maybe im not understanding why it would have these two different types of format?

Hi, I'm glad to hear your response. I'm not familiar with the schema. what is the definition of the keyword oneOf? The above example may be a wrong or invalid schema according to my personal understanding of the word oneOf. It's much better if there is a doc I can check to follow. Right now, I understand why the situation happened this way. However, I’m unsure about the correct code logic for this keyword. I don't want to fix a bug by creating a new one 😄

@mconnelly8 mconnelly8 added the Good First Issue Great to learn the internals of web3.js label Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.x 4.0 related Bug Addressing a bug Good First Issue Great to learn the internals of web3.js
Projects
None yet
Development

No branches or pull requests

5 participants