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

fix: ReferenceArrayInput might pass non-array to children as values #4874

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from

Conversation

macrozone
Copy link
Contributor

I noticed that when you use multiple Filters using the same resource (or a sub-property) and one uses a ReferenceArrayInput, it might receive its value from another filter. When this is not an array, it will crash, at it will assume that the value is an array.

E.g. consider these filters:

 <ReferenceInput label="School" source="user.school"  reference="School">
   <AutocompleteInput {... some props} />
</ReferenceInput>
 <ReferenceArrayInput label="Teacher" source="user"  reference="User">
   <AutocompleteInput {... some props} />
</ReferenceArrayInput>
   

the second one will receive a value of { school: { id: "some-schoolId", ...}} and therefore crash

The quickfix is to define a format, altough this is not so obvious.

  format={(e) => {
        if (Array.isArray(e)) {
          return e;
        }
        return [];
      }}

I think it makes sense to define that as default for ReferenceArrayInput as they assume that if they have a non-falsy value, it is an array.

@djhi
Copy link
Contributor

djhi commented May 29, 2020

Thanks for your contribution. Would you mind opening an issue first with a link to a codesandbox showing the problem ?

@Luwangel
Copy link
Contributor

Luwangel commented Aug 4, 2020

@macrozone Hi, thanks for opening this PR. We need to understand better what you want to achieve with this PR. Could you open a linked issue containing a Code Sandbox in order to show the bug?

@macrozone
Copy link
Contributor Author

@Luwangel the problem is that react-admin (or underlying form library) mixes up sources like source="user.school" or source="user.role"`. There is a hook as far as i remember that returns a vallue based on the source. This hook mixes up the sources when there is a dot in the source name.

The proper solution would probably be to guarantee that source with dot notation are completly independent from each other.

My Pull request does not fix that, but fixes a runtime error that might occures because of this underlying problem. ReferenceArrayInput seems not to check whether a value is actually an array and tries to call array operations on it.

So there are actually two flaws:

  • ReferenceArrayInput does not sanitize the value
  • values leak into other inputs when there is dot notation

I failed to fix these as the code is quite complex, but defining default formatting at least sanitizes the value in the default case.

@fzaninotto fzaninotto changed the base branch from master to 3.x April 12, 2022 16:46
@fzaninotto fzaninotto added the v3 label Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants