Title is not populated for `oneOf` when referenced through `$ref`
In the processing of oneOf options we rely that the title should be inside the components, but sometimes the oneOf is the array of just references, therefore title can be unavailable there.
We can replace the current implementation
getOptions = () => {
return this.props.args.schema[this.schemaName].map((option, index) => {
return {label: option.title || 'Option ' + (index + 1), value: index};
});
}
with this one
getOptions = () => {
return Array.from(this.props.args.schema[this.schemaName].keys(), (index) => ({label: this.getSchema(index).title || 'Option ' + (index + 1), value: index}));
}
or similar. The main point - we should receive the full schema first to make sure that we properly retrieve options.
I would recommend to store the schemas of objects in the internal state of the Container (same list with index acces). In this case we should not retrieve and parse it every time.
Example of the broken schema
{
"$defs": {
"ChoiceModel": {
"additionalProperties": false,
"properties": {
"value": {
"title": "Value",
"type": "string"
},
"label": {
"title": "Label",
"type": "string"
},
},
"required": [
"label"
],
"title": "ChoiceModel",
"type": "object"
},
"CustomFieldNumberMetadataModel": {
"additionalProperties": false,
"properties": {},
"title": "Metadata",
"type": "object"
},
"CustomFieldNumberModel": {
"additionalProperties": false,
"properties": {
"field_type": {
"const": "NUMBER",
"title": "Field Type"
},
"metadata": {
"$ref": "#/$defs/CustomFieldNumberMetadataModel"
}
},
"required": [
"field_type"
],
"title": "Number",
"type": "object"
},
"CustomFieldSelectMetadataModel": {
"additionalProperties": false,
"properties": {
"choices": {
"items": {
"$ref": "#/$defs/ChoiceModel"
},
"title": "Choices",
"type": "array"
}
},
"required": [
"choices"
],
"title": "Metadata",
"type": "object"
},
"CustomFieldSelectModel": {
"additionalProperties": false,
"properties": {
"field_type": {
"const": "SELECT",
"title": "Field Type"
},
"metadata": {
"$ref": "#/$defs/CustomFieldSelectMetadataModel"
}
},
"required": [
"field_type",
"metadata"
],
"title": "Select",
"type": "object"
},
"CustomFieldTextMetadataModel": {
"additionalProperties": false,
"properties": {},
"title": "Metadata",
"type": "object"
},
"CustomFieldTextModel": {
"additionalProperties": false,
"properties": {
"field_type": {
"const": "TEXT",
"title": "Field Type"
},
"metadata": {
"$ref": "#/$defs/CustomFieldTextMetadataModel"
}
},
"required": [
"field_type"
],
"title": "Text",
"type": "object"
}
},
"discriminator": {
"mapping": {
"NUMBER": "#/$defs/CustomFieldNumberModel",
"SELECT": "#/$defs/CustomFieldSelectModel",
"TEXT": "#/$defs/CustomFieldTextModel"
},
"propertyName": "field_type"
},
"final": true,
"oneOf": [
{
"$ref": "#/$defs/CustomFieldSelectModel"
},
{
"$ref": "#/$defs/CustomFieldNumberModel"
},
{
"$ref": "#/$defs/CustomFieldTextModel"
}
],
"title": "Discriminator"
}
Actual and Expected screenshots on playground
NOTE: tested locally, working like a charm.
Thanks for taking the time to go through the source code and providing a fix.
I'll ship it in the next release.
Hi @sshishov, would you like to contribute and open a Pull Request at bhch/react-json-form?
If not, it's still okay and I can make these changes myself. Let me know.
Thanks again.
Any Update? I'd really appreciate this enhancment
@abe-101 and @bhch thanks for taking care, guys! Lately was slightly busy and missed this opportunity!
Change looks good, I guess. Need to test more myself to make sure that all cases are covered...
Released in v2.23.0.
Thanks!