django-rest-framework-json-api icon indicating copy to clipboard operation
django-rest-framework-json-api copied to clipboard

Sparse fields nulling meta fields instead of removing them

Open approxit opened this issue 8 years ago • 0 comments

Hi,

JSON API specification doesn't say anything about meta fields while using sparse fields, but with drf-json-api 2.1.1 implementation we actually have control over meta files presence in response. The thing is - we can't get rid of meta fields totally. Values of meta fields are nulled, and this is a quite impact for the actual information.

Let's take example:

GET /resource1/1/
{
    "data": {
        "type": "resource1",
        "id": "1",
        "attributes": {
            "some_field": 1,
            "some_other_field": 2
        },
        "meta": {
            "some_meta_field": 3,
            "some_other_meta_field": 4
        }
    }
}
GET /resource1/1/?fields[resource1]=some_field
{
    "data": {
        "type": "resource1",
        "id": "1",
        "attributes": {
            "some_field": 1
        },
        "meta": {
            "some_meta_field": null,
            "some_other_meta_field": null
        }
    }
}
GET /resource1/1/?fields[resource1]=some_meta_field
{
    "data": {
        "type": "resource1",
        "id": "1",
        "attributes": {},
        "meta": {
            "some_meta_field": 3,
            "some_other_meta_field": null
        }
    }
}

Take note that null value is a totally natural and legal value in application logic. Here we receives wrong information. It's a bug for me. drf-json-api must decide to drop limiting meta fields by sparse fields or to remove meta fields totally by sparse fields.

approxit avatar Mar 09 '17 08:03 approxit