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: MultiSchema getting incorrect id in arrays #2199

Closed
wants to merge 4 commits into from

Conversation

QingqiShi
Copy link

@QingqiShi QingqiShi commented Jan 18, 2021

Fixes #2197

Reasons for making this change

When MultiSchemaField is part of an array the idSchema is incorrectly generated. This commit fixes that by making sure the parent id is passed to toIdSchema.

#2197

Checklist

  • I'm updating documentation
  • I'm adding or updating code
    • I've added and/or updated tests
    • I've updated docs if needed
  • I'm adding a new feature
    • I've updated the playground with an example use of the feature

@epicfaace
Copy link
Member

epicfaace commented Jan 18, 2021

Hey @QingqiShi , sorry for the delay in reviewing. Is this PR essentially the same as the earlier PR #2127?

@QingqiShi
Copy link
Author

@epicfaace Yes! I accidentally force pushed the other branch which destroyed the PR, this one is identical.

@QingqiShi QingqiShi force-pushed the fix/multi-schema-id branch from 5bc6055 to 6e83df1 Compare April 20, 2021 19:30
@QingqiShi
Copy link
Author

Need some help with this one. Build is failing after rebasing to the latest master branch. It seems to be an unrelated test.

@epicfaace
Copy link
Member

This is technically a breaking change (though correct behavior), and so it can go into the 3.x release. @QingqiShi can you add a section to the "3.x upgrade guide" in the documentation that describes what changed and what users should watch out for?

@epicfaace
Copy link
Member

@QingqiShi It looks like the failing test is happening because the old test is specifically depending on the previous behavior, that is, four dropdowns with the same id in arrays. Can you update the test so it works with this PR's changes with respect to ids?

@QingqiShi
Copy link
Author

@epicfaace Thank you for pointing me to the failing test!

The failing test is now fixed, and I've added a section to the upgrade guide as you've suggested. Thanks!

Copy link
Member

@epicfaace epicfaace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks all good to me, can you just explain one part of the PR to me? Thanks!

@@ -243,7 +243,7 @@ function SchemaFieldRender(props) {
let idSchema = props.idSchema;
const schema = retrieveSchema(props.schema, rootSchema, formData);
idSchema = mergeObjects(
toIdSchema(schema, null, rootSchema, formData, idPrefix),
toIdSchema(schema, idSchema.$id, rootSchema, formData, idPrefix),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused as to what exactly this change does. Do you mind explaining a bit as to how this change in the logic fixes the issue? @QingqiShi

Depending on how complex it is, it might be good for us to add a comment so future users of this code can more easily understand, as well!

Copy link
Author

@QingqiShi QingqiShi Apr 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gladly.

However I'm not sure if we need much explaining here. The call signature for toIdSchema is toIdSchema(schema, id, rootSchema, formData, idPrefix), so I believe passing null to the id argument is incorrect to begin with.

More specifically, in the ArrayField each item gets an itemIdSchema with the id idSchema.$id + "_" + index, this id then gets passed to each item rendered bySchemaField. The logic after that is a bit fuzzy to me, but I imagine if we don't use it during the generation of new ids, it defaults to 'root' prefix and this wrong id eventually get passed to the MultiSchema component.

🙏 Thanks again for spending the time to maintain this package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for the explanation! I'm still a bit confused as to how this change would have only affected the ids in arrays in MultiSchemaField. I would have expected that such a change to SchemaField.js would have had a significant effect on all / many more ids generated through idSchema. Do you know why that's not the case?

@heath-freenome
Copy link
Member

@QingqiShi So I'd love to get this change into the upcoming v5 beta... do you have the bandwidth to port this to the SchemaField.tsx file in the rjsf-v5 branch in the next week or so?

heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Aug 23, 2022
- Replicated the fix made in rjsf-team#2199 since it is a breaking change and we are doing a bunch of them
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Aug 23, 2022
- Replicated the fix made in rjsf-team#2199 since it is a breaking change and we are doing a bunch of them
- Also updated the `CHANGELOG.md`
heath-freenome added a commit that referenced this pull request Aug 24, 2022
- Replicated the fix made in #2199 since it is a breaking change and we are doing a bunch of them
- Also updated the `CHANGELOG.md`
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Aug 27, 2022
- Replicated the fix made in rjsf-team#2199 since it is a breaking change and we are doing a bunch of them
- Also updated the `CHANGELOG.md`
@heath-freenome
Copy link
Member

Fixed in v5 beta via #3039

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MultiSchemaField getting incorrect id when inside array
4 participants