cloudcreativity / laravel-json-api

JSON API (jsonapi.org) package for Laravel applications.
http://laravel-json-api.readthedocs.io/en/latest/
Apache License 2.0
778 stars 109 forks source link

Unable to create json api resource with attribute name "type" #621

Closed sunnydesign closed 3 years ago

sunnydesign commented 3 years ago

When I create a resource with attribute name "type" and try to send a POST request with payload:

{
    "data": {
        "type": "files",
        "attributes": {
            "name": "file.pdf",
            "type": "img_product",
            "mime": "pdf",
            "size": 123456
        }
    }
}

I get an error:

{
    "jsonapi": {
        "version": "1.0"
    },
    "errors": [
        {
            "detail": "The member attributes cannot have a type field.",
            "source": {
                "pointer": "/data/attributes"
            },
            "status": "400",
            "title": "Non-Compliant JSON:API Document"
        }
    ]
}
ben221199 commented 3 years ago

Yes, following the spec on https://jsonapi.org/ you are not allowed to use a type attribute inside your attributes object. This, because there is already a type element in your resource object.

ben221199 commented 3 years ago

https://jsonapi.org/format/#document-resource-object-fields

A resource object’s attributes and its relationships are collectively called its “fields”.

Fields for a resource object MUST share a common namespace with each other and with type and id. In other words, a resource can not have an attribute and relationship with the same name, nor can it have an attribute or relationship named type or id.

sunnydesign commented 3 years ago

Thanks, I read it.