django-jsonform icon indicating copy to clipboard operation
django-jsonform copied to clipboard

Unexpected behavior during oneOf/anyOf option select

Open perrotuerto opened this issue 11 months ago • 3 comments

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!

perrotuerto avatar Jan 31 '25 15:01 perrotuerto

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"},
    ]
  }
}

bhch avatar Jan 31 '25 17:01 bhch

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)

perrotuerto avatar Jan 31 '25 17:01 perrotuerto

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.

bhch avatar Feb 01 '25 05:02 bhch