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

Unexpected behavior during oneOf/anyOf option select #181

Open
perrotuerto opened this issue Jan 31, 2025 · 3 comments
Open

Unexpected behavior during oneOf/anyOf option select #181

perrotuerto opened this issue Jan 31, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@perrotuerto
Copy link
Contributor

Hi, I am wondering if I am doing something wrong. I have this valid JSON Schema:

{
  "2": {
    "type": "object",
    "properties": {
      "corpus": {
        "type": "string",
        "default": "Corpus Test",
        "readonly": true
      },
      "Tipos textuales": {
        "type": "object",
        "systype": {
          "const": "object",
          "widget": "hidden"
        },
        "title": "Tipos textuales",
        "keys": {
          "items": {
            "title": "Ítems",
            "type": "list",
            "items": {
              "anyOf": [
                {
                  "type": "string",
                  "systype": {
                    "const": "string_choices",
                    "widget": "hidden"
                  },
                  "title": "Lengua escrita",
                  "default": "",
                  "required": null,
                  "choices": [
                    "A",
                    "B"
                  ]
                },
                {
                  "type": "string",
                  "systype": {
                    "const": "string_choices",
                    "widget": "hidden"
                  },
                  "title": "Lengua tecleada",
                  "default": "",
                  "required": null,
                  "choices": [
                    "C",
                    "D"
                  ]
                }
              ]
            }
          }
        }
      }
    }
  }
}

But when I try to select an item, I get this behavior:

Image

I check the documentation and I couldn't find where I am missing something. Thanks in advance!

@bhch
Copy link
Owner

bhch commented Jan 31, 2025

It's a known issue, but it's not a bug, per se.

To generate the UI, we first look at the data and then try to find the matching anyOf subschema, and then generate the form.

The problem here is that both the subschemas in anyOf have the same data type. So when you select the second option, the data type is still a string.

So the UI generator matches the string data with the first subschema and generates the form for that.

This probably won't be fixed.

However, there's a workaround. Instead of using string type for subschema, you will need to use an object type and then you can set some hidden unique id field on the subschemas so that the UI generator can differentiate between them.

Example:

{
  "anyOf": [
    {
      "type": "object",
      "title": "Lengua escrita",
      "keys": {
        "id": {"const": "option_1", "widget": "hidden"},
        "systype": {"const": "string_choices", "widget": "hidden"},
        "value": {
          "type": "string",
          "title": "Lengua escrita",
          "default": "",
          "required": null,
          "choices": [
            "A",
            "B"
          ]
        }
      }
    },
    {
      "type": "object",
      "title": "Lengua tecleada",
      "keys": {
        "id": {"const": "option_2", "widget": "hidden"},
        "systype": {"const": "string_choices", "widget": "hidden"},
        "value": {
          "type": "string",
          "title": "Lengua tecleada",
          "default": "",
          "required": null,
          "choices": [
            "C",
            "D"
          ]
        }
      }
    }
  ]
}

Resulting data:

{
  "corpus": "Corpus Test",
  "Tipos textuales": {
    "items": [
      {"id": "option_1", "systype": "string_choices", "value": "A"},
      {"id": "option_1", "systype": "string_choices", "value": "C"},
    ]
  }
}

@perrotuerto
Copy link
Contributor Author

perrotuerto commented Jan 31, 2025

Now it is working! It is not the first time I open an issue here and you always reply asap. Thanks for your attention and your work.

Seeing that this is a known issue, would be a good idea to mentioned it in the troubleshooting or to add some validator to check this problem? I can help you with any of that.

(It was not the first time I run into this problem with anyof, but I don't remember what I did that I was able to fix it)

@bhch bhch changed the title Unexpected behavior during select Unexpected behavior during oneOf/anyOf option select Feb 1, 2025
@bhch bhch added the bug Something isn't working label Feb 1, 2025
@bhch
Copy link
Owner

bhch commented Feb 1, 2025

Thanks for the kind words.

Yeah, some sort of check/validation sounds a good idea but I'm putting off any major changes for the time being.

Right now I'm doing a complete rewrite of the javascript library. We have some ideas to fix this issue (e.g.: this comment on #160).

But I agree, this should be documented. I'll try to update the documentation soon. Until then I'll mark this issue as a bug and leave it open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants