luraproject / lura

Ultra performant API Gateway with middlewares. A project hosted at The Linux Foundation
https://luraproject.org
Other
6.35k stars 563 forks source link

Accept non-objects in the backends response #21

Closed alombarte closed 7 years ago

alombarte commented 7 years ago

Currently KrakenD is only processing responses from backends that return objects. Even returning objects is the usual case, it would be nice to support non-object responses such as lists. E.g:

[ "some", "thing" ]
kpacha commented 7 years ago

This is a nice feature we can add to the framework with the following considerations:

  1. KrakenD must be as stable as possible, so we can not change the interfaces.
  2. KrakenD will accept lists as a valid response from the backend, but they will be embedded under a constant key (let's say collection) before passing it to the filters. Keep in mind the name of the key can be modified in that filtering stage, just by adding the proper mapping for that backend in the config file (so you can change it to whatever)

The implementation would transform the response from ["a",1] to {"collection":["a",1]}.

Here you have an example of how to change the name of the property:

                {
                    "host": [
                        "https://jsonplaceholder.typicode.com"
                    ],
                    "url_pattern": "/users",
                    "mapping": {
                        "collection": "users"
                    }
                }

There are several reasons for returning an object:

  1. KrakenD is not a load balancer, a WAF or an edge-service. It's an api-gateway, so it's intended to manipulate, aggregate and mix several responses in order to fulfill a use-case
  2. There is a trade-off between performance, code readability and flexibility for the users. We’ve chosen that all the filtering, merging and data manipulations are intended to work over some kind of map. Having this minimal restriction gives us better performance and reduces a lot the complexity of the entire system

By the way, using this new feature would have an small impact on the performance of the use-case (aka, throughput of the endpoint that aggregates a collection instead of a resource). We'll add a benchmark so everyone will know how small (or not) it is.

kpacha commented 7 years ago

Feature added.

Feedback is welcome

github-actions[bot] commented 2 years ago

This issue was marked as resolved a long time ago and now has been automatically locked as there has not been any recent activity after it. You can still open a new issue and reference this link.