laravel icon indicating copy to clipboard operation
laravel copied to clipboard

Sparse fieldsets not working with included relationships

Open hayespdx opened this issue 4 years ago • 3 comments

Sparse fieldset requests don't seem to be working with included relationships... /api/v1/images?include=places&fields[places]=name,slug returns all attributes for places, instead of just name and slug, as JSON:API specifies here.

hayespdx avatar Nov 15 '21 20:11 hayespdx

@hayespdx I'm going to need more info about this, as I cannot reproduce. Today I've written the sparse fieldsets section for the tutorial I've just added to the docs, and it was working for me in the tutorial application. There's also a sparse fieldsets PHP Unit test, which is passing.

Can you double check? Not sure what else to suggest without more detail or debugging as to what the problem might be.

lindyhopchris avatar Nov 21 '21 17:11 lindyhopchris

Hey, I've just encountered a similar thing today. Instead of returning all fields, I get no included models:

GET /api/v2/patients/80?include=person&fields[patients]=status,careDegree

yields a single patient record, with no other records included:

{
    "jsonapi": {
        "version": "1.0"
    },
    "links": {
        "self": "http://localhost:8080/api/v2/patients/80"
    },
    "data": {
        "type": "patients",
        "id": "80",
        "attributes": {
            "status": "active",
            "careDegree": "pg2"
        },
        "links": {
            "self": "http://localhost:8080/api/v2/patients/80"
        }
    }
}

If I remove the fields parameter this is what I get instead:

{
    "jsonapi": {
        "version": "1.0"
    },
    "links": {
        "self": "http://localhost:8080/api/v2/patients/80"
    },
    "data": {
        "type": "patients",
        "id": "80",
        "attributes": {
            "status": "active",
            "careDegree": "pg2",
            "careDegreeSince": null,
            "insuranceNumber": "R286336113",
            "initialContact": "other",
            "createdAt": "2013-05-21T00:00:00.000000Z",
            "updatedAt": "2022-01-31T16:37:11.000000Z",
            "deletedAt": null
        },
        "relationships": {
            "person": {
                "links": {
                    "related": "http://localhost:8080/api/v2/patients/80/person",
                    "self": "http://localhost:8080/api/v2/patients/80/relationships/person"
                },
                "data": {
                    "type": "people",
                    "id": "80"
                }
            }
        },
        "links": {
            "self": "http://localhost:8080/api/v2/patients/80"
        }
    },
    "included": [
        {
            "type": "people",
            "id": "80",
            "attributes": {
                "firstName": "John",
                "lastName": "Doe",
                "listName": "Doe, John",
                "bornAt": "2013-06-01T00:00:00.000000Z",
                "iban": null,
                "bic": null,
                "createdAt": "2013-05-21T00:00:00.000000Z",
                "updatedAt": "2022-01-31T16:36:53.000000Z",
                "deletedAt": null
            },
            "relationships": {
                "address": {
                    "links": {
                        "related": "http://localhost:8080/api/v2/people/80/address",
                        "self": "http://localhost:8080/api/v2/people/80/relationships/address"
                    }
                }
            },
            "links": {
                "self": "http://localhost:8080/api/v2/people/80"
            }
        }
    ]
}

Edit: I just figured out that I have to append the relationship to my sparse fieldset, like so:

- /api/v2/patients/80?include=person&fields[patients]=status,careDegree
+ /api/v2/patients/80?include=person&fields[patients]=status,careDegree,person

Is this the desired/intended behavior?

spaceemotion avatar Jan 31 '22 21:01 spaceemotion

@spaceemotion yes your edited query is correct. If you want to include the person relation, you need the linkage from patients to be shown, which means you need to request the person relationship field in the patients resource.

lindyhopchris avatar Feb 01 '22 13:02 lindyhopchris

Closing this due to no further follow up needed on it afaik.

lindyhopchris avatar Apr 22 '23 16:04 lindyhopchris