rjsf-team / react-jsonschema-form

A React component for building Web forms from JSON Schema.
https://rjsf-team.github.io/react-jsonschema-form/
Apache License 2.0
14.3k stars 2.19k forks source link

oneOf not selecting correct default value for complex objects with the same property keys #4208

Closed Becaan closed 3 weeks ago

Becaan commented 4 months ago

Prerequisites

What theme are you using?

core

Version

5.x

Current Behavior

As you can see in this example: https://rjsf-team.github.io/react-jsonschema-form/#eyJmb3JtRGF0YSI6eyJQbGFuSWQiOiIxMjMiLCJTdW0gSW5zdXJlZCI6IjkwMDAiLCJIZWFydCBhbmQgVmFzY3VsYXIgU3lzdGVtIjp7IlN1bSBJbnN1cmVkIjoiMTIwMCJ9fSwic2NoZW1hIjp7InR5cGUiOiJvYmplY3QiLCJvbmVPZiI6W3sidGl0bGUiOiJQbGFuIiwicHJvcGVydGllcyI6eyJQbGFuSWQiOnsidHlwZSI6InN0cmluZyIsImRlZmF1bHQiOiI2MSIsInJlYWRPbmx5Ijp0cnVlfSwiU3VtIEluc3VyZWQiOnsidHlwZSI6InN0cmluZyIsImRlZmF1bHQiOiIxMjAwIiwicGF0dGVybiI6Il5bMC05XXsxLDIwMn0oXFwuWzAtOV17MSwyM30pPyQifSwiSGVhcnQgYW5kIFZhc2N1bGFyIFN5c3RlbSI6eyJwcm9wZXJ0aWVzIjp7IlN1bSBJbnN1cmVkIjp7InR5cGUiOiJzdHJpbmciLCJkZWZhdWx0IjoiMTIwMCIsInBhdHRlcm4iOiJeWzAtOV17MSwyMDJ9KFxcLlswLTldezEsMjN9KT8kIn19fX19LHsidGl0bGUiOiJOZXcgUGxhbiIsInByb3BlcnRpZXMiOnsiUGxhbklkIjp7InR5cGUiOiJzdHJpbmciLCJkZWZhdWx0IjoiMTIzIiwicmVhZE9ubHkiOnRydWV9LCJTdW0gSW5zdXJlZCI6eyJ0eXBlIjoic3RyaW5nIiwiZGVmYXVsdCI6IjkwMDAiLCJwYXR0ZXJuIjoiXlswLTldezEsMjAyfShcXC5bMC05XXsxLDIzfSk/JCJ9LCJIZWFydCBhbmQgVmFzY3VsYXIgU3lzdGVtIjp7InByb3BlcnRpZXMiOnsiU3VtIEluc3VyZWQiOnsidHlwZSI6InN0cmluZyIsImRlZmF1bHQiOiI5MDAwIiwicGF0dGVybiI6Il5bMC05XXsxLDIwMn0oXFwuWzAtOV17MSwyM30pPyQifX19fX1dfSwidWlTY2hlbWEiOnt9LCJ0aGVtZSI6Im1hdGVyaWFsLXVpLTUiLCJsaXZlU2V0dGluZ3MiOnt9fQ==

The default value for complex object is not taken into account at all as well as pattern for example which i tried also. So basically. "Sum Insured" that is a part of "Heart and Vascular System" always has the same default value of 1200 even though in jsonSchema default value for "New Plan" is set to 9000. The behaviour that it should follow should be like the "Sum Insured" that is outside of "Heart and Vascular System".

TLDR: "Sum Insured" inside the "Heart and Vascular System" should have default value 9000 previewed when selecting "New Plan"

Expected Behavior

"Sum Insured" inside the "Heart and Vascular System" should have default value 9000 previewed when selecting "New Plan"

Steps To Reproduce

https://rjsf-team.github.io/react-jsonschema-form/#eyJmb3JtRGF0YSI6eyJQbGFuSWQiOiI2MSIsIlN1bSBJbnN1cmVkIjoiMTIwMCIsIkhlYXJ0IGFuZCBWYXNjdWxhciBTeXN0ZW0iOnsiU3VtIEluc3VyZWQiOiIxMjAwIn19LCJzY2hlbWEiOnsidHlwZSI6Im9iamVjdCIsIm9uZU9mIjpbeyJ0aXRsZSI6IlBsYW4iLCJwcm9wZXJ0aWVzIjp7IlBsYW5JZCI6eyJ0eXBlIjoic3RyaW5nIiwiZGVmYXVsdCI6IjYxIiwicmVhZE9ubHkiOnRydWV9LCJTdW0gSW5zdXJlZCI6eyJ0eXBlIjoic3RyaW5nIiwiZGVmYXVsdCI6IjEyMDAiLCJwYXR0ZXJuIjoiXlswLTldezEsMjAyfShcXC5bMC05XXsxLDIzfSk/JCJ9LCJIZWFydCBhbmQgVmFzY3VsYXIgU3lzdGVtIjp7InByb3BlcnRpZXMiOnsiU3VtIEluc3VyZWQiOnsidHlwZSI6InN0cmluZyIsImRlZmF1bHQiOiIxMjAwIiwicGF0dGVybiI6Il5bMC05XXsxLDIwMn0oXFwuWzAtOV17MSwyM30pPyQifX19fX0seyJ0aXRsZSI6Ik5ldyBQbGFuIiwicHJvcGVydGllcyI6eyJQbGFuSWQiOnsidHlwZSI6InN0cmluZyIsImRlZmF1bHQiOiIxMjMiLCJyZWFkT25seSI6dHJ1ZX0sIlN1bSBJbnN1cmVkIjp7InR5cGUiOiJzdHJpbmciLCJkZWZhdWx0IjoiOTAwMCIsInBhdHRlcm4iOiJeWzAtOV17MSwyMDJ9KFxcLlswLTldezEsMjN9KT8kIn0sIkhlYXJ0IGFuZCBWYXNjdWxhciBTeXN0ZW0iOnsicHJvcGVydGllcyI6eyJTdW0gSW5zdXJlZCI6eyJ0eXBlIjoic3RyaW5nIiwiZGVmYXVsdCI6IjkwMDAiLCJwYXR0ZXJuIjoiXlswLTldezEsMjAyfShcXC5bMC05XXsxLDIzfSk/JCJ9fX19fV19LCJ1aVNjaGVtYSI6e30sInRoZW1lIjoibWF0ZXJpYWwtdWktNSIsImxpdmVTZXR0aW5ncyI6e319

Environment

- OS:
- Node:
- npm:

Anything else?

No response

nickgros commented 4 months ago

@Becaan thanks for the report, this is definitely a bug. The regular maintainers don't have much time to fix all issues, are you interested to investigate a fix?

This looks similar to #3892, which dealt with allOfs, which was fixed by #3969, so that PR may be a good place to figure out where to start

Writingsinfo commented 3 weeks ago

Hi @nickgros I know you have labeled it as help wanted but is it possible to escalate this to the team and see if anyone can pick it up? We are relying on oneOf and have been facing the same issue.

Writingsinfo commented 3 weeks ago

The issue we are seeing is that when we switch options, the default values are restored only for the fields that are common to both the options. Eg. Link

In this example, when you switch from option-1 to option-2 and then back to option-1, default value for lorem is not restored.

nickgros commented 3 weeks ago

@Becaan We took another look at your schema and realized that the issue is that the "Heart and Vascular System" property is missing"type": "object"`. If you add that, it works (playground link)

nickgros commented 3 weeks ago

@Writingsinfo @heath-freenome is going to have a PR soon that will hopefully fix your issue in #4322

Nickvajani commented 2 weeks ago

Thank you @nickgros and @heath-freenome for jumping on it so quickly.