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: oneOf anyOf default value always using first option #2198

Closed

Conversation

QingqiShi
Copy link

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

  • 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

@Canas
Copy link

Canas commented Feb 11, 2021

Whatever happened to this fix? This is still happening in the playground

@QingqiShi
Copy link
Author

Still waiting for a review and merge.

@exevil
Copy link

exevil commented Mar 23, 2021

+1 here. Would be great to merge this.

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.

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.

@epicfaace epicfaace self-assigned this Mar 23, 2021
@QingqiShi QingqiShi force-pushed the fix/multi-schema-default branch from 11a9101 to b2eb250 Compare March 23, 2021 17:10
@QingqiShi QingqiShi force-pushed the fix/multi-schema-default branch from b2eb250 to 8d8c915 Compare March 23, 2021 17:12
@QingqiShi QingqiShi requested a review from epicfaace March 23, 2021 17:13
@chenghu3
Copy link

chenghu3 commented Apr 1, 2021

I think @QingqiShi addressed the comment, could we pls review and merge this? thanks

@QingqiShi
Copy link
Author

Yes indeed. I added a test to cover the example from #2183

@tappleby
Copy link

tappleby commented Apr 6, 2021

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

@QingqiShi
Copy link
Author

Need some help with this one. Build is failing after merging with master. It seems to be an unrelated test that's failing.

@epicfaace
Copy link
Member

@QingqiShi looks like this is the line that is failing. Maybe you could try debugging the test by printing out onChange and onChange.lastCall? I'm wondering if perhaps onChange isn't being called properly with the changes in this PR.

@epicfaace epicfaace assigned QingqiShi and unassigned epicfaace Apr 21, 2021
@QingqiShi
Copy link
Author

I realised that after the fix it no longer generate formData if you haven't selected any of the options in a oneOf or anyOf yet.

To make this fix backwards compatible, I've added a check to see if formData is an empty object, if it is then continue to pass undefined to getMatchingOption when generating defaults. This maintains the current behaviour before the user selects an option, and will start using formData after the user selects an option.

@epicfaace tests fixed, please instruct on how to proceed.

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.

Thanks for your changes! A few more comments.

packages/core/src/utils.js Outdated Show resolved Hide resolved
schema.anyOf[getMatchingOption(undefined, schema.anyOf, rootSchema)];
schema.anyOf[
getMatchingOption(
JSON.stringify(formData) !== "{}" ? formData : undefined,
Copy link
Member

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?

Copy link
Author

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.

Copy link
Author

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.

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.

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)

@Stankk
Copy link

Stankk commented Dec 16, 2021

Hello, any news on the merge ?

@Akifhan
Copy link

Akifhan commented Mar 24, 2022

Hello, any updates on the merge?

ubik2 added a commit to ubik2/react-jsonschema-form that referenced this pull request Jun 5, 2022
@davidpett
Copy link

Can we get this merged, please?

davidpett added a commit to davidpett/react-jsonschema-form that referenced this pull request Jun 17, 2022
@eliyaoo32
Copy link

Hi, are they plans to merge this?

@heath-freenome heath-freenome added the p1 Important to fix soon label Aug 26, 2022
@heath-freenome heath-freenome added awaiting changes v5 refactor Needs refactor due to v5 breaking changes labels Sep 7, 2022
@heath-freenome
Copy link
Member

@QingqiShi Do you have the bandwidth to refactor this code into the new @rjsf/utils package in the v5 beta?

heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Sep 8, 2022
- 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
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Sep 8, 2022
- 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
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Sep 8, 2022
- 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
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Sep 8, 2022
- 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
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Sep 8, 2022
- 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
heath-freenome added a commit that referenced this pull request Sep 8, 2022
* 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
@heath-freenome
Copy link
Member

@QingqiShi closing as I took your excellent work and refactored it into the new @rjsf/utils library code in #3099

@QingqiShi
Copy link
Author

Thank you very much ❤️ @heath-freenome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting changes p1 Important to fix soon v5 refactor Needs refactor due to v5 breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

oneOf with "object" types - impossible to replace Option 1