laravel
laravel copied to clipboard
Sparse fieldsets not working with included relationships
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 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.
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 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.
Closing this due to no further follow up needed on it afaik.