zalando / restful-api-guidelines

A model set of guidelines for RESTful APIs and Events, created by Zalando
https://opensource.zalando.com/restful-api-guidelines/
Creative Commons Attribution 4.0 International
2.61k stars 383 forks source link

Hints on tooling to implement 'partial responses via filtering' #806

Closed sareykaj closed 1 month ago

sareykaj commented 4 months ago

Hi, I would like to implement 'partial responses via filtering' (using the fields query parameter) in my Spring Boot REST services but I could not find any Spring / Jackson build in functionality or add-on libraries to do it. As you refer to Spring/Java in your API guidelines in some places, I was wondering how you have implemented it. Best regards, Julian

tkrop commented 4 months ago

We actually have a Java/JavaScript library for this, that was retracted already 5 to 6 years ago from Zalando's Open Source GH repository. It was using simple filtering on top of the response. However, we do not think that this is a proper approach to provide this feature, since it resolves much more information from the database than it needs to and is inherently inefficient.

Internally, we advise our developers - if possible - to filter the data at the source instead of applying a post filter at the response for efficiency. I also advise in talks and reviews to focus the usage of filter-sets and embed-combination for distinct high value use cases, where the effect compared to the effort and costs creates the maximum benefit.

sareykaj commented 4 months ago

Thanks a lot for you answer! So an example of "filter the data at the source" would be to transform the fields query into an sql projection and then use e.g. Java Optional fields to filter out uninitialized fields in the response payload? You mentioned filter-sets. What do you mean with "sets"? Something predefined?

tkrop commented 3 months ago

@sareykaj Yes, I used the term filter-sets and embed-combinations to describe that I suggest to provide a list of valid filter and embed keyword combinations that are supported instead of allowing the full might of the expression language defined. This is especially simple, if the properties that are disabled or enabled by the embed or filter are already structured into objects in the response.