Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Title is not populated for oneOf when referenced through $ref #164

Closed
sshishov opened this issue Mar 27, 2024 · 6 comments
Closed

Title is not populated for oneOf when referenced through $ref #164

sshishov opened this issue Mar 27, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@sshishov
Copy link

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

Screenshot 2024-03-28 at 00 04 13Screenshot 2024-03-28 at 00 04 27

NOTE: tested locally, working like a charm.

@sshishov sshishov changed the title Do not reproduce title for oneOf when referenced through $ref Title is not populated for oneOf when referenced through $ref Mar 27, 2024
@bhch bhch added the enhancement New feature or request label Mar 28, 2024
@bhch
Copy link
Owner

bhch commented Mar 28, 2024

Thanks for taking the time to go through the source code and providing a fix.

I'll ship it in the next release.

@bhch
Copy link
Owner

bhch commented Apr 1, 2024

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.

@abe-101
Copy link

abe-101 commented May 15, 2024

Any Update?
I'd really appreciate this enhancment

abe-101 added a commit to abe-101/react-json-form that referenced this issue May 15, 2024
Following the suggestion from bhch/django-jsonform#164
@bhch bhch closed this as completed in 812888c Jul 6, 2024
@sshishov
Copy link
Author

sshishov commented Jul 7, 2024

@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...

@bhch
Copy link
Owner

bhch commented Sep 20, 2024

Released in v2.23.0.

@abe-101
Copy link

abe-101 commented Sep 25, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants