-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
At the moment, the return value of a controller method/router handler is converted to the response using the following hard-coded algorithm:
- Object types (including arrays, but also Date, RegExp, Buffer) are converted to JSON and sent with "application/json" content-type.
- String results are sent as-is with "text/plain" content-type.
- Anything else is passed to
response.write()as-is and no content-type is set.
We need a more flexible setup, we should at least honor "produces" configuration from the API specification.
Acceptance Criteria
-
A controller method should be able to specify
media typefor theresponsesobject with@operation. See https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#response-objectNote by @bajtos: I believe this is already supported, see e.g. the TodoController in our examples: https://github.com/strongloop/loopback-next/blob/cb308add60bbe938c8e85812676eb817e9e0efc9/examples/todo/src/controllers/todo.controller.ts#L32
-
A controller method should be able to return JS representation of the response body or a wrapper corresponding to the response objects. The goal: allow controller methods to control response status code and headers. E.g.
Locationfor201 Createdresponses to POST requests creating new model instances,Content-TypeandContent-Dispositionfor file downloads. -
LoopBack should be able to match the response media types to the
Acceptrequest header to determine what content type to be produced for a given request. It will setContent-Typeresponse header. -
Have built in support for
application/jsonandtext/plainmedia-types.
EDIT: Move to another task
- LoopBack runtime should define an extension point to register http response serializers that can be used to serialize the JS plain/wrapper object into a http response. A serializer should be able to handle certain media types, for example, json, xml, or protocol buffer. The serializer just has to deal with the http response object as a canonical representation.