Change MapGet return type for better OpenApi support

This commit is contained in:
2025-05-23 15:41:59 +02:00
parent 3e517ae425
commit fdf9579846
2 changed files with 51 additions and 46 deletions
@@ -6,7 +6,7 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<IsPackable>true</IsPackable> <IsPackable>true</IsPackable>
<Version>0.8.8</Version> <Version>0.8.9</Version>
<Authors>Alexandru Macocian</Authors> <Authors>Alexandru Macocian</Authors>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
@@ -34,52 +34,57 @@ public static class WebApplicationExtensions
where TWebSocketRoute : WebSocketRouteBase where TWebSocketRoute : WebSocketRouteBase
{ {
app.ThrowIfNull(); app.ThrowIfNull();
return app.MapGet(route, static async (HttpContext context, ILogger<TWebSocketRoute> logger, TWebSocketRoute route) => return app.MapGet(route, HandleWebSocketRoute<TWebSocketRoute>);
{ }
if (context.WebSockets.IsWebSocketRequest)
{
var routeFilters = GetRouteFilters<TWebSocketRoute>(context).ToList();
var actionContext = new ActionContext( private static async Task<IResult> HandleWebSocketRoute<TWebSocketRoute>(HttpContext context, TWebSocketRoute route, ILogger<TWebSocketRoute> logger)
context, where TWebSocketRoute : WebSocketRouteBase
new RouteData(), {
new ActionDescriptor()); if (!context.WebSockets.IsWebSocketRequest)
var actionExecutingContext = new ActionExecutingContext( {
actionContext, logger.LogError("WebSocket request expected");
routeFilters, return Results.BadRequest("WebSocket request expected");
new Dictionary<string, object?>(), }
route);
var actionExecutedContext = new ActionExecutedContext( var routeFilters = GetRouteFilters<TWebSocketRoute>(context).ToList();
actionContext, var actionContext = new ActionContext(
routeFilters, context,
route); new RouteData(),
try new ActionDescriptor());
{ var actionExecutingContext = new ActionExecutingContext(
var processingTask = new Func<Task>(() => ProcessWebSocketRequest(route, context)); actionContext,
await BeginProcessingPipeline(actionExecutingContext, actionExecutedContext, processingTask); routeFilters,
} new Dictionary<string, object?>(),
catch(WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) route);
{ var actionExecutedContext = new ActionExecutedContext(
logger.LogInformation("Websocket closed prematurely. Marking as closed"); actionContext,
} routeFilters,
catch(OperationCanceledException) route);
{ try
logger.LogInformation("Websocket closed prematurely. Marking as closed"); {
} var processingTask = new Func<Task>(() => ProcessWebSocketRequest(route, context));
catch(Exception ex) await BeginProcessingPipeline(actionExecutingContext, actionExecutedContext, processingTask);
{ return Results.Empty;
logger.LogError(ex, "Encountered exception while handling websocket. Closing"); }
} catch (WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely)
finally {
{ logger.LogInformation("Websocket closed prematurely. Marking as closed");
await route.SocketClosed(); return Results.Empty;
} }
} catch (OperationCanceledException)
else {
{ logger.LogInformation("Websocket closed prematurely. Marking as closed");
context.Response.StatusCode = StatusCodes.Status400BadRequest; return Results.Empty;
} }
}); catch (Exception ex)
{
logger.LogError(ex, "Encountered exception while handling websocket. Closing");
return Results.Empty;
}
finally
{
await route.SocketClosed();
}
} }
private static async Task ProcessWebSocketRequest(WebSocketRouteBase route, HttpContext httpContext) private static async Task ProcessWebSocketRequest(WebSocketRouteBase route, HttpContext httpContext)