thephpleague / fractal

Output complex, flexible, AJAX/RESTful data structures.
fractal.thephpleague.com
MIT License
3.52k stars 352 forks source link

[JsonApiSerializer] A way to return requested realtionship without attributes and links #519

Open Insolita opened 3 years ago

Insolita commented 3 years ago

Even if we transform data as

   transform($data){
       return ['id' => $data['id']];
  }

the response will contain empty attributes object and link object with self-link like

 "data" : {
            "type":       "users" ,
            "id":         "55" ,
            "attributes": { } ,
            "links":      {
                "self": "http://127.0.0.1:80/users/55"
            }
        }

There is no way to receive a simple response for request like /articles/1/relationships/author r like in examples https://jsonapi.org/format/#fetching-relationships

 "data" : {"type":  "users" , "id":   "55" }
willishq commented 3 years ago

How are you creating the serializer? If you create the serializer without a base URL, it shouldn't add the links.

Regarding the attributes, that's a tricky one which comes from the current implementation not being able to difrentiate between an empty object and an empty array, there are issues raised about this problem already and the changes required would essentially mean rebuilding the serializer from scratch. In theory, an empty object should not be there, but an empty array should be, so the API consumer knows that there is potentially an amount of items to be iterated over.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 4 weeks if no further activity occurs. Thank you for your contributions.