Default Value not displaying

Hello @sdirix ,

First of all, thank you so much for building such an awesome library. I’m using jsonforms 3.0.0 in one of my project and it seems like there is a bug. Please have a look at following JSON schema and it’s output from jsonforms:

{
    "title": "oneOf Default Value",
    "type": "object",
    "oneOf": [
        {
            "title": "Tab 1",
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "enum": [
                        "Tab_1"
                    ],
                    "default": "Tab_1",
                    "readOnly": true
                }
            },
            "required": [
                "type"
            ]
        },
        {
            "title": "Tab 2",
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "enum": [
                        "Tab_2"
                    ],
                    "default": "Tab_2",
                    "readOnly": true
                }
            },
            "required": [
                "type"
            ]
        },
        {
            "title": "Tab 3",
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "enum": [
                        "Tab_3"
                    ],
                    "default": "Tab_3",
                    "readOnly": true
                }
            },
            "required": [
                "type"
            ]
        }
    ]
};

In above schmea, there are three tabs and each tab has type as required field and a default value. But for some reason only the first tab is displaying the default value. Rest two tabs are empty. And since it is readonly it is problematic that use cannot even change it by their own will.

Here is how tab 2 renders:
11 -Tab 2 - correct

(Here, the type field is empty - despite defining default value in the schema)

Can you please confirm if there is anything wrong in the schema of it’s a but from jsonforms.

Thank you so much again for all the good work and help :smiley:

Hi @Dhruw,

Thanks for the kind words.

The “problem” with the default keyword in JSON Schema is that there is no associated semantic meaning to it, so any framework/library is free to handle it how they see it fit, e.g. when and how to set the value.

In JSON Forms itself we don’t have any special functionality implemented regarding the default properties. However internally we use AJV which offers some (limited) default functionality, i.e. it will set default values during the validation process in case they are undefined. See here for how to enable this functionality in JSON Forms.
Having said that, I think defaults within a oneOf construct like you defined are exactly one of the use cases which is not supported by AJV. The reason is because it is impossible for AJV to know which oneOf actually applies to your data.

However there is a way to accomplish your goal: You can implement a custom oneOf renderer in JSON Forms and then on tab change (or however the renderer visualizes the oneOf selection) you can just set the default value in the object.

Unrelated to the question at hand: Your JSON Schema is rather unconvential, in the sense of that all oneOf entries are exactly the same and just differ in the default value. Was this done to illustrate the problem or is that an actual schema you’re using? If it’s the latter then I would like to suggest to replace the whole oneOf with a simple enum containing three options.

1 Like