laravel-json-api / laravel

JSON:API for Laravel applications
MIT License
541 stars 41 forks source link

Allow null as data in relationship PATCH request #197

Closed JaZo closed 2 years ago

JaZo commented 2 years ago

This allows you to remove a to-one relationship by sending null in a PATCH request, as described in the spec.

Request

PATCH {{baseUrl}}/model/:model/relationships/related

{
  "data": null
}

Result

Before

{
    "jsonapi": {
        "version": "1.0"
    },
    "errors": [
        {
            "detail": "Expecting JSON API specification compliance to have been run.",
            "meta": {
                "exception": "LogicException",
                "file": "vendor/laravel-json-api/laravel/src/Http/Requests/ResourceRequest.php",
                "line": 209,
                "trace": []
            },
            "status": "500",
            "title": "Internal Server Error"
        }
    ]
}

After

{
    "jsonapi": {
        "version": "1.0"
    },
    "links": {
        "related": "{{baseUrl}}/model/1/related",
        "self": "{{baseUrl}}/model/1/relationships/related"
    },
    "data": null
}
lindyhopchris commented 2 years ago

@JaZo thanks for submitting the PR, can't believe no one has encountered this error before now!