Skip to content

Relationships validation fails when sending an empty object #320

@veneliniliev

Description

@veneliniliev

When sending an empty relationships object ({}), the API responds with an error saying "The member relationships must be an object."

According to the JSON:API specification, this should be valid — an empty object for relationships is allowed when there are simply no relationships to define.

The issue appears to originate from the following check in RelationshipsValidator.php:

if (!is_object($relationships)) {
    return $errors->push(
        $this->translator->memberNotObject('/data', 'relationships')
    );
}

Even though {} is an object in JSON, by the time the validation runs, $relationships may already have been converted to an array, triggering this error.


✅ Expected Behavior

The validator should accept an empty object {} as a valid relationships value and not return an error.


🛠 Suggested Fix

Ensure that $relationships remains an object or adjust the validation logic to correctly handle empty objects.
For example, confirm the type before validation or modify the check to allow empty arrays/objects when appropriate.

if (!is_object($relationships) && !empty($relationships)) {
    // only push error if it's not an object AND not empty
    return $errors->push(
        $this->translator->memberNotObject('/data', 'relationships')
    );
}

Request example:

{
    "data": {
        "type": "xxxx",
        "id": "5",
        "attributes": {
            "deleted_at": null
        },
        "relationships": {}
    }
}

Response:

{
    "jsonapi": {
        "version": "1.0"
    },
    "errors": [
        {
            "detail": "The member relationships must be an object.",
            "source": {
                "pointer": "/data/relationships"
            },
            "status": "400",
            "title": "Non-Compliant JSON:API Document"
        }
    ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions