Path: redb-route/redb.Route.Http/Rest/RestVerbDefinition.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/RestVerbDefinition.cs
----
using redb.Route.Abstractions;
namespace redb.Route.Http.Rest;
///
/// One operation of a : method + path template, media types, binding
/// types and description. Finished with (send to an endpoint) or
/// (write the steps inline); both create the underlying HTTP route.
///
public sealed class RestVerbDefinition
{
private readonly RestDefinition _rest;
internal RestVerbDefinition(RestDefinition rest, string method, string path)
{
_rest = rest;
Method = method;
Path = path;
}
/// HTTP method (GET, POST, …).
public string Method { get; }
/// Path template relative to the (/{id}, /{id}/status, or empty).
public string Path { get; }
/// Full path template (/api/orders/{id}).
public string FullPath => RestDefinition.JoinPath(_rest.BasePath, Path);
/// Expected request media type; a request with another Content-Type is answered with 415.
public string? ConsumesType { get; private set; }
/// Response media type written to the reply.
public string? ProducesType { get; private set; }
/// Binding mode override for this verb.
public RestBindingMode? Binding { get; private set; }
/// CLR type of the request body (JSON binding and OpenAPI).
public Type? RequestType { get; private set; }
/// CLR type of the response body (OpenAPI).
public Type? ResponseType { get; private set; }
/// Human description → OpenAPI summary.
public string? Summary { get; private set; }
///
/// Route id; default rest-METHOD-full-path-1a2b3c4d (no '/' and no space, stable across restarts). Also the
/// OpenAPI operationId.
///
public string? RouteId { get; private set; }
/// Target endpoint set by ; null for inline routes.
public string? Target { get; private set; }
/// Path parameter names in declaration order ({id} → id).
public IReadOnlyList PathParameters => RestDefinition.PathParameterNames(FullPath);
/// Parameters declared with , in declaration order.
public IReadOnlyList Parameters => _parameters;
/// Client request validation override for this verb; null = the declaration's option.
public bool? Validation { get; private set; }
private readonly List _parameters = [];
///
/// Declares a parameter: described in OpenAPI, and when client request validation is on, a missing
/// required one or a value that does not convert to is answered with 400.
/// A path parameter must be a segment of the template and is always required, so
/// left null means required for a path parameter and optional
/// for the others.
///
public RestVerbDefinition Param(string name, RestParamType type = RestParamType.Query, bool? required = null,
RestParamDataType dataType = RestParamDataType.String, string? description = null)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
if (type == RestParamType.Path)
{
if (!PathParameters.Contains(name, StringComparer.Ordinal))
throw new ArgumentException(
$"Path parameter '{name}' is not a segment of '{FullPath}'.", nameof(name));
if (required == false)
throw new ArgumentException(
$"Path parameter '{name}' cannot be optional: a path parameter is always required.", nameof(required));
}
var comparer = type == RestParamType.Header ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal;
if (_parameters.Any(p => p.Type == type && comparer.Equals(p.Name, name)))
throw new ArgumentException($"{type} parameter '{name}' is declared twice on {Method} {FullPath}.", nameof(name));
_parameters.Add(new RestParamDefinition(name, type, required ?? type == RestParamType.Path, dataType, description));
return this;
}
/// Switches client request validation on or off for this verb (see ).
public RestVerbDefinition ClientRequestValidation(bool enabled = true) { Validation = enabled; return this; }
/// Request media type this operation accepts.
public RestVerbDefinition Consumes(string contentType) { ConsumesType = Required(contentType); return this; }
/// Response media type this operation produces.
public RestVerbDefinition Produces(string contentType) { ProducesType = Required(contentType); return this; }
/// Binding mode for this operation.
public RestVerbDefinition BindingMode(RestBindingMode mode) { Binding = mode; return this; }
/// Request body type: unmarshalled under JSON binding, described in OpenAPI.
public RestVerbDefinition Type() { RequestType = typeof(T); return this; }
/// Response body type, described in OpenAPI.
public RestVerbDefinition OutType() { ResponseType = typeof(T); return this; }
/// Human description (OpenAPI summary).
public RestVerbDefinition Description(string summary) { Summary = Required(summary); return this; }
/// Route id / OpenAPI operationId.
public RestVerbDefinition Id(string routeId) { RouteId = Required(routeId); return this; }
/// Sends the request to (usually direct:...) and returns to the REST declaration for the next verb.
public RestDefinition To(string uri)
{
Target = Required(uri);
_rest.Complete(this);
return _rest;
}
/// Handles the request with steps written inline; returns the route to add them to.
public IRouteDefinition Route() => _rest.Complete(this);
internal RestBindingMode EffectiveBinding(RestOptions options) => Binding ?? options.BindingMode;
internal string? EffectiveConsumes(RestOptions options) => ConsumesType ?? options.Consumes;
internal string? EffectiveProduces(RestOptions options) => ProducesType ?? options.Produces;
internal bool EffectiveValidation(RestOptions options) => Validation ?? options.ClientRequestValidation;
private static string Required(string value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(value);
return value;
}
}