json-forms icon indicating copy to clipboard operation
json-forms copied to clipboard

selects render "undefined" when bound to a oneOf definition

Open Vaevictus opened this issue 9 years ago • 3 comments

for the following schema:

    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "typeName": "IncomingFileIODto",
    "additionalProperties": false,
    "required": [
        "EndPoint",
        "Title",
        "Comment",
        "Status",
        "OwnerBoxId"
    ],
    "properties": {
        "ChannelTypeName": {
            "type": "string",
            "readOnly": true
        },
        "ChannelTypeInfo": {
            "type": "string",
            "readOnly": true
        },
        "Title": {
            "type": "string"
        },
        "Comment": {
            "type": "string",
            "format": "textarea"
        },
        "Status": {
            "default": "Enabled",
            "oneOf": [
                {
                    "$ref": "#/definitions/ChannelState"
                }
            ]
        },
        "EndPoint": {
            "type": "string"
        },
        "Interval": {
            "type": "integer"
        },
        "IntervalUnit": {
            "oneOf": [
                {
                    "$ref": "#/definitions/TimeUnit"
                }
            ]
        },
        "DateTimeStart": {
            "type": "string",
            "format": "datetime-local"
        },
        "DateTimeStop": {
            "type": "string",
            "format": "datetime-local"
        },
        "DateTimeCreated": {
            "type": "string",
            "format": "datetime-local",
            "readOnly": true
        },
        "DateTimeLastChanged": {
            "type": "string",
            "format": "datetime-local",
            "readOnly": true
        },
        "TemporaryFolderName": {
            "type": "string",
            "readOnly": true
        },
        "ID": {
            "type": "string",
            "readOnly": true
        },
        "OwnerBoxId": {
            "type": "integer",
            "readOnly": true
        }
    },
    "definitions": {
        "ChannelState": {
            "type": "string",
            "typeName": "ChannelState",
            "enumNames": [
                "Disabled",
                "Enabled",
                "Paused",
                "Errored"
            ],
            "enum": [
                "Disabled",
                "Enabled",
                "Paused",
                "Errored"
            ],
            "description": ""
        },
        "TimeUnit": {
            "type": "string",
            "typeName": "TimeUnit",
            "enumNames": [
                "Seconds",
                "Minutes",
                "Hours",
                "Days",
                "Weeks",
                "Months",
                "Years"
            ],
            "enum": [
                "Seconds",
                "Minutes",
                "Hours",
                "Days",
                "Weeks",
                "Months",
                "Years"
            ],
            "description": ""
        }
    }
}

the select boxes rendered for the TimeUnit and Status types render a select box with only "undefined" as the available choice. When selecting this undefined value, a new Select is immediately rendered beneath it which contains the correct options as defined in the schema.

When the bootstrap-select renderer is used, the behaviour is different. The select box renders no options and no drop down is displayed on click.

Vaevictus avatar Jun 07 '16 22:06 Vaevictus

This is the expected behaviour. Please see http://brutusin.org/json-forms/#7. For "one-of" definitions, a combo is rendered showing the title property of the different choices. So you can add a title property to your definitions, or if there is only one "one-of" choice don't use the "one-of" construct at all.

idelvall avatar Jun 08 '16 15:06 idelvall

very clear - thanks. I will have to modify my schemas to work with your framework.

Just so you know this is different behaviour to some other form generators. Also, C# json schema generators such as JSON.NET or NJSONSchema will generate JSON as I have shown above for non-array-of enums.

 [EnumDataType(typeof(ChannelState))]
        [Required]
        public string Status { get; set; }

will result in a jsonschema being generated which looks like

 "Status": {
            "default": "Enabled",
            "oneOf": [
                {
                    "$ref": "#/definitions/ChannelState"
                }
            ]
        },

Vaevictus avatar Jun 08 '16 20:06 Vaevictus

Thanks for the info, we can change the implementation in such a way that when only there is one option, not rendering the combo.

On the other hand, when there is no title in the definition, is preferable to use an alternative name for the select option (for example the node name). This way no "undefined" option would be rendered.

Do you feel capable of implementing these? I so, please fork and send us a pull request.

idelvall avatar Jun 10 '16 07:06 idelvall