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

Unable to switch between the options in the anyOf when omitExtraData and liveOmit is true #4367

Open
4 tasks done
binhtran04 opened this issue Nov 6, 2024 · 2 comments
Open
4 tasks done
Labels
bug needs triage Initial label given, to be assigned correct labels and assigned

Comments

@binhtran04
Copy link

binhtran04 commented Nov 6, 2024

Prerequisites

What theme are you using?

core

Version

5.x

Current Behavior

I have a schema that has anyOf between two options. The form renders a selector for it, but when I use the selector to select the options, it does not work. I also notice when I select the option 2 for example, the formData is filled with default of both option 1 and option 2.

When turning off omitExtraData and liveOmit, it works normally.

Expected Behavior

When I switch to option 2, the fields for option 2 should be rendered, and the formData contains only the default values for option 2

Steps To Reproduce

Visit playground, and try to use the selector to select option 2. You will notice the fields for option 1 still render and the formData includes the default values for both option 1 and option 2

Environment

Playground: https://rjsf-team.github.io/react-jsonschema-form/#eyJmb3JtRGF0YSI6eyJteVByb3BlcnR5Ijp7InZhbHVlX3Byb3ZpZGVyX3R5cGUiOiJUWVBFXzEiLCJzdGFydCI6NjUwMDAwMDAwMCwic3RvcCI6ODUwMDAwMDAwMCwic3RlcF9zaXplIjoxMDAwMDAsIm51bV9zYW1wbGVzIjoyMDAxLCJzd2VlcF9zaGlmdCI6MH19LCJzY2hlbWEiOnsidGl0bGUiOiJTZXR0aW5ncyIsInR5cGUiOiJvYmplY3QiLCJwcm9wZXJ0aWVzIjp7Im15UHJvcGVydHkiOnsiZGVmYXVsdCI6eyJ2YWx1ZV9wcm92aWRlcl90eXBlIjoiVFlQRV8xIiwic3RhcnQiOjY1MDAwMDAwMDAsInN0b3AiOjg1MDAwMDAwMDAsInN0ZXBfc2l6ZSI6MTAwMDAwLCJudW1fc2FtcGxlcyI6MjAwMX0sImFueU9mIjpbeyIkcmVmIjoiIy9kZWZpbml0aW9ucy9UeXBlMiJ9L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zdCI6InRvcCIsInZhbGlkYXRlIjpmYWxzZSwiZGlzYWJsZWQiOmZhbHNlLCJub0h0bWw1VmFsaWRhdGUiOmZhbHNlLCJyZWFkb25seSI6ZmFsc2UsIm9taXRFeHRyYURhdGEiOnRydWUsImxpdmVPbWl0Ijp0cnVlLCJleHBlcmltZW50YWxfZGVmYXVsdEZvcm1TdGF0ZUJlaGF2aW9yIjp7ImFycmF5TWluSXRlbXMiOiJwb3B1bGF0ZSIsImFsbE9mIjoic2tpcERlZmF1bHRzIiwiZW1wdHlPYmplY3RGaWVsZHMiOiJwb3B1bGF0ZUFsbERlZmF1bHRzIiwibWVyZ2VEZWZhdWx0c0ludG9Gb3JtRGF0YSI6InVzZUZvcm1EYXRhSWZQcmVzZW50In0sImxpdmVWYWxpZGF0ZSI6ZmFsc2UsIm5vVmFsaWRhdGUiOmZhbHNlfX0=

Anything else?

The schema I'm testing with

{
  "title": "Settings",
  "type": "object",
  "properties": {
    "myProperty": {
      "default": {
        "value_provider_type": "TYPE_1",
        "start": 6500000000,
        "stop": 8500000000,
        "step_size": 100000,
        "num_samples": 2001
      },
      "anyOf": [
        {
          "$ref": "#/definitions/Type2"
        },
        {
          "$ref": "#/definitions/Type1"
        }
      ]
    }
  },
  "definitions": {
    "Type2": {
      "title": "Type2",
      "type": "object",
      "properties": {
        "sweep_range": {
          "title": "Sweep range",
          "type": "number"
        },
        "sweep_step_size": {
          "title": "Step size",
          "type": "number"
        },
        "sweep_shift": {
          "title": "Shift",
          "default": 0,
          "type": "number"
        }
      },
      "required": [
        "sweep_range",
        "sweep_step_size"
      ]
    },
    "Type1": {
      "title": "Type1",
      "type": "object",
      "properties": {
        "start": {
          "title": "Start",
          "type": "number"
        },
        "stop": {
          "title": "Stop",
          "type": "number"
        },
        "step_size": {
          "title": "Step size",
          "type": "number"
        },
        "num_samples": {
          "title": "Number of samples",
          "type": "integer",
          "default": -1
        },
        "value_provider_type": {
          "title": "Value Provider Type",
          "default": "TYPE_1",
          "enum": [
            "TYPE_1"
          ],
          "type": "string"
        }
      },
      "required": [
        "start",
        "stop",
        "value_provider_type"
      ]
    }
  }
}
@binhtran04 binhtran04 added bug needs triage Initial label given, to be assigned correct labels and assigned labels Nov 6, 2024
@binhtran04
Copy link
Author

I dug a bit, and it looks like the bug happens like this. Given the schema has a top-level default, which is the value of option 1. Some fields from option 1 and option 2 have default values as well.
When I select option 2:

  1. First onChange happens with the id of the anyOf_select and formData contains the default values for option 2
  2. Then the second change happens without any id and the formData contains the defaults of the top-level defaults and the option 2 defaults. I believe this is because of some internal initialization

Now the formData is mixed between option 1 and option 2 values, and the form thinks option 1 is still selected, hence the bug

I think the expected behaviour should be that either the second onChange does not happen, or it happens in a way that includes only the defaults for the second option.

@quentin-sommer
Copy link
Contributor

👋 I can see the same issue without any default values

{
  "additionalProperties": false,
  "properties": {
    "any_of_array_or_null": {
      "anyOf": [
        {
          "items": {
            "type": "integer"
          },
          "type": "array",
          "title": "Array value selected"
        },
        {
          "type": "null",
          "title": "Null value selected"
        }
      ]
    }
  },
  "required": [
    "any_of_array_or_null"
  ],
  "type": "object"
}
Screen.Recording.2024-11-07.at.17.01.43.mov

My guess is that it's a conflict between a default null value inside the lib and the fact that null is an allowed value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs triage Initial label given, to be assigned correct labels and assigned
Projects
None yet
Development

No branches or pull requests

2 participants