Repository navigation
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.
Activity
raymondfeng commentedon Jul 13, 2017
We probably should start to refactor https://github.com/strongloop/loopback-next/tree/master/packages/repository/src/types into its own package to form the LB.next typing system and leverage it for parameter parsing/conversion/serialization.
bajtos commentedon Nov 2, 2017
Labelling as non-MVP, because we have a simple workaround available - users can use their own implementation of
sendsequence action.dustinbarnes commentedon Dec 4, 2017
@bajtos can you give an example of that? I'm currently fighting the content-type header (it really needs to send application/json when we return a single string), and using the sugar for @get(path, spec), but it seems to ignore whatever is put into the "produces" section of the spec object.
bajtos commentedon Dec 5, 2017
@dustinbarnes Take a look at the default implementation of
send()here and here.Here is what you need to do:
writeResultToResponsefunction.kjdelisle commentedon Dec 5, 2017
@bajtos @dustinbarnes
Registering your custom send action involves injecting on the
RestBindings.SequenceActions.SENDkey with a custom Provider for Send.application.ts
custom-send-provider.ts
If you're implementing multiple servers in your application, binding this key at the Application-level context will make that binding the default for all of the servers connected to it. This means that if you wanted different custom providers for different RestServer instances, you'd want to perform those bindings elsewhere (like in an async override of the
startmethod).In this example, PrivateSendProvider might allow stack traces to be returned via the response, whereas PublicSendProvider will not.
in application.ts
bajtos commentedon Jan 15, 2018
I created a follow-up issue to capture the instructions from Kevin in our docs - see #863.
dhmlau commentedon Mar 22, 2018
Does it fall under the "Validation and type conversion" epic #755 ?
bajtos commentedon Mar 23, 2018
I don't think so. As I understand #755, it deals with input parameters. This story deals with outputs.
I think the best epic to assign this story to is an epic for implementing a local in-process API Explorer and/or a capability to server static files.
42 remaining items
mdbetancourt commentedon Feb 27, 2020
What about this, I think it can work
because writeResultToResponse bypass response if controller return the response
stale commentedon Jul 14, 2021
This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the
CODEOWNERSfile at the top-level of this repository. This issue will be closed within 30 days of being stale.stale commentedon Aug 13, 2021
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the
CODEOWNERSfile at the top-level of this repository.