8000 [question] How to implement ?fields=name,email filter? · Issue #193 · api-platform/api-platform · GitHub
[go: up one dir, main page]

Skip to content

[question] How to implement ?fields=name,email filter? #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rogamoore opened this issue Dec 16, 2016 · 9 comments
Closed

[question] How to implement ?fields=name,email filter? #193

rogamoore opened this issue Dec 16, 2016 · 9 comments
Labels

Comments

@rogamoore
Copy link

Hello everyone and thanks for this great bundle!

I'm currently trying to find a way to limit the returned fields on a GET request dynamically by using a fields list on the query string.

Standard:
GET /books

[
  {
    "id": 19,
    "isbn": null,
    "title": "test",
    "description": "test",
    "author": "test",
    "publicationDate": "2016-11-28T14:47:44+00:00",
  },
  {
    "id": 25,
    "isbn": "9788811810148",
    "title": "Odissea",
    "description": "Grande classico ellenico",
    "author": "Omero",
    "publicationDate": "2016-11-11T18:14:58+00:00",
  }
]

With filter:
GET /books?fields=id,title

[
  {
    "id": 19,
    "title": "test",
  },
  {
    "id": 25,
    "title": "Odissea",
  }
]

What would be the recommended way to solve this?

@teohhanhui
Copy link
Contributor

I don't think this can be implemented as a custom filter. And serializer groups seem ill-suited for this...

@soyuka
Copy link
Member
soyuka commented Dec 19, 2016

Indeed, you can do this by using serialization groups.

@dunglas
Copy link
Member
dunglas commented Dec 19, 2016

I've started to add this feature in the Symfony Serializer but it's not finished yet (see comments): symfony/symfony#18834

Any help welcome :)

@dunglas
Copy link
Member
dunglas commented Dec 28, 2016

By the way, it can be also be easily added by decorating the api_platform.jsonld.normalizer.item service and unsetting fields you don't want.

You can directly inject the request_stack service in this decorator to access the fields query parameter ; but it's not clean, I recommend to set a new key in the serialization context to do that: https://api-platform.com/docs/core/serialization-groups-and-relations#changing-the-serialization-context-dynamically.

@rogamoore
Copy link
Author

Thanks Kévin, for the feedback. I've used dynamic serialization groups for now (not as dynamic as I wanted above) but I will try your suggestion asap. :)
I'll also have a look if I can provide any help with the Symfony Serializer as this would be probably the cleanest solution :)

@alborq
Copy link
alborq commented Jan 26, 2017

It's may impleaments into DataProvider too. It's not really usefull to fetch unused data.
@dunglas some news for symfony/symfony#18834 ?

@teohhanhui
Copy link
Contributor

symfony/symfony#18834 has been merged. 🎉

@BallisticPain
Copy link

Looking forward to this.

@Simperfit
Copy link
Contributor

This has been implemented in core !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants
0