-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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: oneOf anyOf default value always using first option #2198
Conversation
Whatever happened to this fix? This is still happening in the playground |
Still waiting for a review and merge. |
+1 here. Would be great to merge this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test that specifically covers this playground example (from #2183)? Meaning, the case in which neither option has a default, and when you switch from option 1 to option 2.
11a9101
to
b2eb250
Compare
b2eb250
to
8d8c915
Compare
I think @QingqiShi addressed the comment, could we pls review and merge this? thanks |
Yes indeed. I added a test to cover the example from #2183 |
Just been tracking down some issues with an upgrade and found this fix. I noticed that for any schemas that make use of refs, this fix may also depend on #2272 |
Need some help with this one. Build is failing after merging with |
@QingqiShi looks like this is the line that is failing. Maybe you could try debugging the test by printing out |
I realised that after the fix it no longer generate To make this fix backwards compatible, I've added a check to see if @epicfaace tests fixed, please instruct on how to proceed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your changes! A few more comments.
packages/core/src/utils.js
Outdated
schema.anyOf[getMatchingOption(undefined, schema.anyOf, rootSchema)]; | ||
schema.anyOf[ | ||
getMatchingOption( | ||
JSON.stringify(formData) !== "{}" ? formData : undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, should we move this logic out of this file and directly inside the getMatchingOption
function instead?
Finally, getMatchingOption
is also used a few times in MultiSchemaField.js -- should we apply this fix there as well, and if we do, perhaps we could add a test that covers that change as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit scared to widen this change.
I know that getMatchingOption
is used for a couple of different purposes, such as generating defaults (as we're doing here), and for loading the initial selected multi-schema options (as you mentioned in MultiSchemaField
), we also use it in resolveDependencies
.
These use cases have subtle differences - in the computeDefault
function, formData
gets defaulted to {}
(for reasons I don't understand), and that's why we need this check here so it goes back to generating defaults. Whereas in other places it may be necessary to keep the empty object as the object type may be matched to a particular sub-schema.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I may, I will restrain myself from potentially breaking the world. I'll leave it to your judgement as you are the expert on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@epicfaace is this a blocking comment? It would be great to get this pr merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @nickgros (I see you on an active pr)
Hello, any news on the merge ? |
Hello, any updates on the merge? |
Can we get this merged, please? |
Hi, are they plans to merge this? |
@QingqiShi Do you have the bandwidth to refactor this code into the new |
- The fix in rjsf-team#2198 needed to be refactored into the new `@rjsf/utils` package - Replicated the fix into the `getDefaultFormState.ts` file - Replicated the additional tests into `getDefaultFormStateTest.ts` and `oneOf_test.js` - Updated the `CHANGELOG.md` with the note for this fix along with the notes for PR rjsf-team#3094
- The fix in rjsf-team#2198 needed to be refactored into the new `@rjsf/utils` package - Replicated the fix into the `getDefaultFormState.ts` file - Replicated the additional tests into `getDefaultFormStateTest.ts` and `oneOf_test.js` - Updated the `CHANGELOG.md` with the note for this fix along with the notes for PR rjsf-team#3094
- The fix in rjsf-team#2198 needed to be refactored into the new `@rjsf/utils` package - Replicated the fix into the `getDefaultFormState.ts` file - Replicated the additional tests into `getDefaultFormStateTest.ts` and `oneOf_test.js` - Updated the `CHANGELOG.md` with the note for this fix along with the notes for PR rjsf-team#3094
- The fix in rjsf-team#2198 needed to be refactored into the new `@rjsf/utils` package - Replicated the fix into the `getDefaultFormState.ts` file - Replicated the additional tests into `getDefaultFormStateTest.ts` and `oneOf_test.js` - Updated the `CHANGELOG.md` with the note for this fix along with the notes for PR rjsf-team#3094
- The fix in rjsf-team#2198 needed to be refactored into the new `@rjsf/utils` package - Replicated the fix into the `getDefaultFormState.ts` file - Replicated the additional tests into `getDefaultFormStateTest.ts` and `oneOf_test.js` - Updated the `CHANGELOG.md` with the note for this fix along with the notes for PR rjsf-team#3094
* Reimplemented fix in #2198 in @rjsf/utils package - The fix in #2198 needed to be refactored into the new `@rjsf/utils` package - Replicated the fix into the `getDefaultFormState.ts` file - Replicated the additional tests into `getDefaultFormStateTest.ts` and `oneOf_test.js` - Updated the `CHANGELOG.md` with the note for this fix along with the notes for PR #3094 * - Updated `CHANGELOG.md` for PR #2925 * - Fixed up `CHANGELOG.md` to move note from #3049 into the proper location
@QingqiShi closing as I took your excellent work and refactored it into the new |
Thank you very much ❤️ @heath-freenome |
Fixes #2183
Reasons for making this change
When generating default values, currently we're always assuming the first option is matched. This default value will then be merged with existing data - so if you have the second option selected, you can end up with both values from the first and the second options.
#2183
Checklist