Path: redb-route/redb.Route.Http/Rest/RestResponseDefinition.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Http/Rest/RestResponseDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Definitions;
using redb.Route.Processors;
namespace redb.Route.Http.Rest;
///
/// Last step of a REST route: writes the Produces media type, marshals a CLR object body to
/// JSON under JSON binding, and answers 204 when the route left no body and set no status.
/// The status code itself is the HTTP consumer's contract: header redbHttp.ResponseCode.
///
public sealed class RestResponseDefinition : ProcessorDefinition
{
private readonly RestVerbDefinition _verb;
private readonly RestOptions _options;
internal RestResponseDefinition(RestVerbDefinition verb, RestOptions options)
{
_verb = verb;
_options = options;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
{
var produces = _verb.EffectiveProduces(_options);
var json = _verb.EffectiveBinding(_options) == RestBindingMode.Json
? context.GetService()?.GetSerializer(produces ?? "application/json")
: null;
return new DelegateProcessor(exchange =>
{
var message = exchange.Out ?? exchange.In;
if (exchange.IsStopped) return;
if (json is not null && message.Body is not (null or string or byte[] or Stream))
{
message.Body = json.Serialize(message.Body);
message.ContentType = json.ContentType;
}
if (produces is not null && !message.Headers.ContainsKey(HttpHeaders.ResponseContentType))
message.Headers[HttpHeaders.ResponseContentType] = produces;
if (message.Body is null && !message.Headers.ContainsKey(HttpHeaders.ResponseCode))
message.Headers[HttpHeaders.ResponseCode] = 204;
});
}
}