react-jsonschema-form icon indicating copy to clipboard operation
react-jsonschema-form copied to clipboard

Upgrading from v2 to v3 or v4 breaks preselection in `anyOf` schema

Open michal-kurz opened this issue 2 years ago • 6 comments

Prerequisites

  • [x] I have read the documentation;
  • [x] In the case of a bug report, I understand that providing a SSCCE example is tremendously useful to the maintainers.
  • [x] I'm providing a couple of sample sandboxes demonstrating the issue.

Description

I'm not sure this is technically a bug, as I haven't found the broken behaviour documented anywhere. But it does work in v2, breaks in v3.0.0+ and I consider it an essential feature.

Here is a working v2.5.1. sandbox ... and here is a sandbox with identical code, but broken, because it runs on v3.0.0.

Steps to Reproduce

  1. Create an anyOf schema with two distinct types of anyOf items
  2. Render a <Form> with this schema, and formData exclusively corresponding to the second schema item

Expected (v2.5.1.) behavior

Form renders with the second anyOf item selected - the item matched by formData

Actual (v3.0.0) behavior

Form always renders with the first anyOf item selected - regardless of formData not complying with the UI-selected schema

michal-kurz avatar Mar 21 '22 16:03 michal-kurz

Hmm, do you know which change in v3 might have broken this?

-- Ashwin Ramaswami

On Mon, Mar 21, 2022 at 12:29 PM michal-kurz @.***> wrote:

Prerequisites

Description

I'm not sure this is technically a bug, as I haven't found the broken behaviour documented anywhere. But does work in v2, breaks in v3.0.0+ and I consider it an essential feature.

Here https://codesandbox.io/s/rjsf-anyof-broken-option-preselection-v2-5-1-8h76z?file=/src/Test.tsx is a working v2.5.1. sandbox ... and here https://codesandbox.io/s/rjsf-anyof-broken-option-preselection-v3-0-hbbikm?file=/src/Test.tsx is a sandbox with identical code, but broken, because it runs of v3.0.0. Steps to Reproduce

  1. Create an anyOf schema with two distinct type of anyOf items
  2. Render a <Form> with this schema, and formData exclusively corresponding to the second schema item

Expected (v2.5.1.) behavior

Form renders with the second anyOf item selected - the item matched by formData Actual (v3.0.0) behavior

Form always renderes with the first anyOf item selected - regardless of formData not complying with the UI-selected schema

— Reply to this email directly, view it on GitHub https://github.com/rjsf-team/react-jsonschema-form/issues/2773, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAM4MXYWTA6URDVK6HZIWMLVBCPWDANCNFSM5RIHM2UA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

epicfaace avatar Mar 21 '22 16:03 epicfaace

@epicfaace I have no idea, I haven't compared the code at all. But I suppose this part of migration docs might have something to do with it.

michal-kurz avatar Mar 21 '22 17:03 michal-kurz

bump, also running into this - @michal-kurz did you find any workaround?

ksbrar avatar Jun 06 '22 15:06 ksbrar

@ksbrar Unfortunately not, we're running on v2 for this reason, because this is critical for us.

michal-kurz avatar Jun 15 '22 11:06 michal-kurz

@michal-kurz it would help if -- do you mind making a PR that includes a test that breaks due to this bug?

epicfaace avatar Jun 15 '22 17:06 epicfaace

In this case it works on v2.5.1 because the list of enums has only one value. When we add more than one values in enum, default value also breaks.

@epicfaace I added a PR with test. Is there a way to fix this problem?

andreySimonenko avatar Aug 23 '22 07:08 andreySimonenko

@andreySimonenko In your PR, when you change the default from ["<default value>"] to "<default value>" things work. I'm curious why are you providing an array of values as a default for a type that expects a single value.

heath-freenome avatar Jan 19 '23 21:01 heath-freenome

Closing as not a bug. I'm guessing that the old 2.x code incorrectly allowed defaults of arrays when the type itself wasn't an array

heath-freenome avatar Jan 20 '23 16:01 heath-freenome

@heath-freenome

For the record: I strongly believe this issue was closed incorrectly. The fact that we used an array is not relevant to the bug. If we use a simple string value for the default, the situation is the same:

However on v5.3.1, this works again, and now works as expected!! 🎉 🎉

Not sure what broke v3 and v4, but now we can migrate to v5, I'm happy :)

The API has really progressed a long way since, thank you very much the excellent work you're putting in this project! ❤️ And thanks to whoever fixed this issue.

michal-kurz avatar Mar 22 '23 00:03 michal-kurz