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

default values for each of the oneOf options are not displayed #1133

Open
fangq opened this issue Mar 5, 2022 · 6 comments
Open

default values for each of the oneOf options are not displayed #1133

fangq opened this issue Mar 5, 2022 · 6 comments
Assignees
Labels

Comments

@fangq
Copy link

fangq commented Mar 5, 2022

General information

When a property is set as oneOf and each of the value option sets a default value, json-editor shows empty values either for the default option or switching to alternative options. Basically the default value settings are not used at all when oneOf is set.

Additionally, the title of the item is also fixed as the JSON key name, rather than using the title field of the property.

See below direct link to example

  • json-editor version:
    2.6.1

Expected behavior

for the default option ("string"), display the string-type default value "DP", when switching to "integer", sets to 5 instead

Actual behavior

choosing either option, the default value stays at empty

Steps to reproduce the behavior

Direct link to example

{
  "title": "MCX JSON Input",
  "type": "object",
  "required": [
    "SaveDataMask"
  ],
  "format": "categories",
  "properties": {
    "SaveDataMask": {
      "oneOf": [
        {
          "title": "Save detected photon flags",
          "type": "string",
          "default": "DP"
        },
        {
          "title": "Save detected photon flags",
          "type": "integer",
          "default": 5,
          "minimum": 0
        }
      ]
    }
  }
}
@fangq
Copy link
Author

fangq commented Mar 5, 2022

on a side note - we wrote a web-GUI for a cloud-computing based simulator, where JSON editor was a key component in the frontend. This work was was recently published at https://doi.org/10.1117/1.JBO.27.8.083008, where we referenced the json-editor project. here is our web GUI: http://mcx.space/cloud/

json-editor really made our development much simpler, we want to thank the developers for your great works!

@schmunk42 schmunk42 added the bug label Mar 7, 2022
@schmunk42
Copy link
Collaborator

Does this playground example work like intended? https://is.gd/nBRgjo

But I also had issues with our default example?!

@fangq
Copy link
Author

fangq commented Mar 7, 2022

@schmunk42, the playground example behaves as expected when choosing the integer option (the default value was set to 5), but when choosing the string option, the default value "DP" was not displayed.

@schmunk42
Copy link
Collaborator

We're looking into that and will try to fix it, but it's a complex issue.
If you can come up with a PR, we'd be happy.

There are "fitChecks" to see which value fits best to the given anyOf.
In your case the first type is not really selected but merely it's just the first value in the dropdown and looks selected, but it's not a "conscious decision".

Here are two hacky workarounds, but I do not really advise to use them, they are merely for understanding the issue better:

  1. add a null type to make an interactive decision about the type.
{
  "title": "MCX JSON Input",
  "type": "object",
  "required": [
    "SaveDataMask"
  ],
  "format": "categories",
  "properties": {
    "SaveDataMask": {
      "oneOf": [
        {
          "title": "Save detected photon flags",
          "type": "null"
        },
        {
          "title": "Save detected photon flags",
          "type": "string",
          "default": "DP"
        },
        {
          "title": "Save detected photon flags",
          "type": "integer",
          "default": 5,
          "minimum": 0
        }
      ]
    }
  }
}

Or 2. you can move the default to the property itself (also not nice)

{
  "title": "MCX JSON Input",
  "type": "object",
  "required": [
    "SaveDataMask"
  ],
  "format": "categories",
  "properties": {
    "SaveDataMask": {
      "default": "DP",
      "oneOf": [
        {
          "title": "Save detected photon flags",
          "type": "string"
        },
        {
          "title": "Save detected photon flags",
          "type": "integer",
          "default": 5,
          "minimum": 0
        }
      ]
    }
  }
}

@fangq
Copy link
Author

fangq commented Mar 7, 2022

thanks for the above comments and workarounds. the 2nd workaround works for me - although I am not sure if this is a valid from json-schema's perspective - just to test, I changed "default" to a floating-point value (1.5), and it is displayed when the string option is chosen - which seems inappropriate

In your case the first type is not really selected but merely it's just the first value in the dropdown and looks selected, but it's not a "conscious decision".

regardless whether the first type was consciously chosen by the user, or automatically assigned by the editor, to the user, it is the same - that the string value ends up in the JSON output, so I felt that it is more appropriate to use its respective default value - unless the default option is "please choose below" and force users to make a conscious choice. let me know if this makes sense.

@tobiaseisenschenk
Copy link

tobiaseisenschenk commented Sep 29, 2022

Thank you for the detailed response. I am using workaround no 1, because of a related issue. I choose a subschema in a anyOf and would like the defaults to be overwritten. As this does not work (it only overwrites changing properties) I can use your hack to force an initial selection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants