jdorn / json-editor

JSON Schema Based Editor
MIT License
5.81k stars 1.08k forks source link

Problem with "oneOf" #398

Open skrichten opened 9 years ago

skrichten commented 9 years ago

Here is a scheme I am trying to use...

{
  "title": "Configuration",
  "type": "object",
  "properties": {
    "triggers": {
      "type": "array",
      "format": "tabs",
      "title": "Triggers",
      "items": {
        "title": "Trigger",
        "oneOf": [
          {
            "title": "Twitter",
            "type": "object",
            "properties": {
              "track": {
                "type": "string"
              }
            }
          },
          {
            "title": "Instagram",
            "type": "object",
            "properties": {
              "tags": {
                "title": "Tags",
                "type": "array",
                "format": "table",
                "items": {
                  "title": "tag",
                  "type": "string"
                }
              }
            }
          }
        ]
      }
    }
  }
}

In the editor, If I switch an "Trigger" from "Twitter" to "Instagram", I still see a property from the Twitter schema ("track"). I don't think that should be displaying at all, but it is also not displaying correctly. Is this a bug? In any case is there any way to get around this problem?

You can see what I mean here... http://jeremydorn.com/json-editor/?schema=N4IgLglmA2CmIC4QGECGZXQJ4GcwAJkB7AOwDMIBzAVwCd0JSQAacLAB3iSICMArWAGMwLEO1pFOtSLByJQYWlUqxachAo5cQqWvSyiyRWgFt0icKh5zWkGNoAqSyirWiosE+oVQ4Fp8qqoqSwAPJkiADaPvb+AO5QYEG2Wha8AsKi4pKqMt7g9IIA1vJsnBZ4SiSUogAmsoJK7JBMSMQmZvj10BAmHrX4PXj4RGT4SaY440TjhUUAdCAAvivMMX5IAJIkeKiU9CaiYKnc/EIirNlSeaUYlPl2GyAOezZl2rr6hsZmIkgYPD8rHqOEaEGajBIFgAMhBhmAABawfAI1A4BF3KZgGaKVDFRasagkCAAR2osE2SS8iEU5NYHmpGnAvm0dyOJxAlQg1WWKyWAF0+UsgAAA==&value=N4IgLgTglg5jCmEDOIBcBtUkCGBjA1miCAL4C6JQAAA=&theme=bootstrap3&iconlib=bootstrap3&object_layout=normal&show_errors=interaction

In the dropdown, change twitter to instagram.

jdorn commented 9 years ago

It's not a bug, but it is really unintuitive behavior. By default, JSON Editor tries to keep the value of oneOf fields when you switch between schemas if possible.

You can fix this in one of two ways:

  1. Make the track property invalid for your Instagram oneOf schema by setting additionalProperties to false.

    {
     "title": "Instagram",
     "type": "object",
     "additionalProperties": false,
     "properties": {
       "tags": {
         "title": "Tags",
         "type": "array",
         "format": "table",
         "items": {
           "title": "tag",
           "type": "string"
         }
       }
     }
    }
  2. Turn off this default JSON Editor behavior by setting the option keep_oneof_values to false during initialization.

    new JSONEditor(element, {
     schema: {...},
     keep_oneof_values: false
    });
skrichten commented 9 years ago

Gotcha! Thank you very much!

mbrodala commented 9 years ago

@jdorn Thanks, the keep_oneof_values option did the trick since the other way only yields an error message No additional properties allowed, but property {property} is set.

This option should be documented since switching form views depending on a select field is a very frequent usecase.

stevage commented 9 years ago

Agreed, @mbrodala - I ran into this as well.

(Btw if I haven't said it already, thanks @jdorn for a really awesome tool!)

akshitjain commented 9 years ago

Please provide any updation on this way of solution:-Make the track property invalid for your Instagram oneOf schema by setting additionalProperties to false. There is an error in this method .

mbrodala commented 9 years ago

@akshitjain Wut?

akshitjain commented 9 years ago

@mbrodala I am saying that on putting "additionalProperties": false in the object as mentioned by @jdorn It's giving error as mentioned by you only in your comment above : "way only yields an error message No additional properties allowed, but property {property} is set." So I was asking whether there is some updation on this or not i.e will it be corrected in the editor code ?

mbrodala commented 9 years ago

@akshitjain From what I can see the behavior is correct. By default (keep_oneof_values: true) the editor does not silently drop properties to make them valid for the selected schema. If you are fine with that, you can set keep_oneof_values: false.

@jdorn Please correct me if I'm wrong and this is indeed a bug in the additionalProperties processing.

eric-schleicher commented 8 years ago

I also ran into this.. perhaps having a section in the docs that addresses this would be helpful.. @jdorn will you take a pull request for that?