Been trying to get JSON-API responses to work but cannot seem to find any way to get included data returned.
The API returns the normal JSON-API format so i assume its working but there is no included data
JSON-API Example (http://jsonapi.org/examples/):
{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!",
"body": "The shortest article. Ever.",
"created": "2015-05-22T14:56:29.000Z",
"updated": "2015-05-22T14:56:28.000Z"
},
"relationships": {
"author": {
"data": {"id": "42", "type": "people"}
}
}
}],
"included": [
{
"type": "people",
"id": "42",
"attributes": {
"name": "John",
"age": 80,
"gender": "male"
}
}
]
}
Response from my API:
/api/stats/22.jsonapi?include=vehicle
{
data: {
id: "/api/stats/22",
type: "Stat",
attributes: {
_id: 22,
engineSize: "1",
drivetrain: "rwd",
horsepower: "250"
},
relationships: {
vehicle: {
data: {
type: "Vehicle",
id: "/api/vehicles/1"
}
}
}
}
}
GraphQL request/response from my api:
Request:
{
stat(id: "/api/stats/22") {
id
_id
horsepower
vehicle {
id
_id
make
model
}
}
}
Response:
{
"data": {
"stat": {
"id": "/api/stats/22",
"_id": 22,
"horsepower": "250",
"vehicle": {
"id": "/api/vehicles/1",
"_id": 1,
"make": "Toyota",
"model": "Supra"
}
}
}
}
The API works perfectly in GraphQL which makes me assume my entities are setup correctly but I don't know why JSON-API isnt working.
I briefly looked through the normaliser and couldn't find any reference to "included"
Perhaps i'm doing something wrong?
You likely need to use normalization groups to get related object attributes.
I suggest you read https://api-platform.com/docs/core/serialization/#embedding-relations
You can check in the core tests entity to get hint if you're struggling.
For example usnig existing entities I got this test to run which looks similar to what you're expecting
@createSchema
Scenario: Correctly serialize a collection
Given there are people having pets
When I send a "GET" request to "/people/1"
Then the response status code should be 200
And the response should be in JSON
And the JSON should be valid according to the JSON API schema
And the JSON should be equal to:
"""
{
"data": {
"id": "\/people\/1",
"type": "Person",
"attributes": {
"name": "foo",
"pets": [
{
"pet": {
"data": {
"id": "\/pets\/1",
"type": "Pet",
"attributes": {
"name": "bar"
}
}
}
}
]
}
}
}
"""
my bad I didn't understood correctly
@antograssiot Thank you for your reply, these responses still don't have the "included" object which is a pretty standard part of JSON-API.
Surely there is a way to have that in the response if JSON-API is supported by Api-Platform?
Same issue +1
Any answer for this? None of the filters seem to work either. Do we need to replace the normaliser that comes with Api platform for JSON-API?
I'm working on a PR to provide this feature but you can give it a try and propose one.
Indeed enhancing the JSON-API normalizer seems the way to go.
Not related but filters seems ok from my experience (at least searchfilter, orderfilter, datefilter - and propertyfilter but in a tricky way for now) and the are some tests arround them. Would you please try to elaborate and maybe open a new issue with a reproducer, how did you declare them on your entitites ? (Maybe related https://github.com/api-platform/core/issues/1940)
@antograssiot perfect, yes enhancing the normalizer is a solid fix.
When i mentioned filters I meant JSON-API ones, sorry i wasnt particularly clear.
The filtering i mentioned is something in JSON-API where you pass "fields" as a query parameter (http://jsonapi.org/format/#fetching-filtering).
an example in my case being:
?include=stats&fields[vehicles]=stats&fields[stats]=engineSize
This would limit the response to only the fields specified in their respective objects.
This should also be included in the normalizer as it is standard in JSON-API
Ok so I've been working on those filters too. It is almost done I'll try to submit a PR this weekend or next week
That would be amazing. Thank you
The current JSON-API support is minimalist. Many non-mandatory features are not yet implemented. Feel free to contribute these ones! 馃槈
Super happy with what was merged, great work and big thanks to @antograssiot
Most helpful comment
Ok so I've been working on those filters too. It is almost done I'll try to submit a PR this weekend or next week