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>
<OutputType>Library</OutputType>
<IsPackable>true</IsPackable>
<Version>0.8.8</Version>
<Version>0.8.9</Version>
<Authors>Alexandru Macocian</Authors>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
@@ -34,52 +34,57 @@ public static class WebApplicationExtensions
where TWebSocketRoute : WebSocketRouteBase
{
app.ThrowIfNull();
return app.MapGet(route, static async (HttpContext context, ILogger<TWebSocketRoute> logger, TWebSocketRoute route) =>
{
if (context.WebSockets.IsWebSocketRequest)
{
var routeFilters = GetRouteFilters<TWebSocketRoute>(context).ToList();
return app.MapGet(route, HandleWebSocketRoute<TWebSocketRoute>);
}
var actionContext = new ActionContext(
context,
new RouteData(),
new ActionDescriptor());
var actionExecutingContext = new ActionExecutingContext(
actionContext,
routeFilters,
new Dictionary<string, object?>(),
route);
var actionExecutedContext = new ActionExecutedContext(
actionContext,
routeFilters,
route);
try
{
var processingTask = new Func<Task>(() => ProcessWebSocketRequest(route, context));
await BeginProcessingPipeline(actionExecutingContext, actionExecutedContext, processingTask);
}
catch(WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely)
{
logger.LogInformation("Websocket closed prematurely. Marking as closed");
}
catch(OperationCanceledException)
{
logger.LogInformation("Websocket closed prematurely. Marking as closed");
}
catch(Exception ex)
{
logger.LogError(ex, "Encountered exception while handling websocket. Closing");
}
finally
{
await route.SocketClosed();
}
}
else
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
}
});
private static async Task<IResult> HandleWebSocketRoute<TWebSocketRoute>(HttpContext context, TWebSocketRoute route, ILogger<TWebSocketRoute> logger)
where TWebSocketRoute : WebSocketRouteBase
{
if (!context.WebSockets.IsWebSocketRequest)
{
logger.LogError("WebSocket request expected");
return Results.BadRequest("WebSocket request expected");
}
var routeFilters = GetRouteFilters<TWebSocketRoute>(context).ToList();
var actionContext = new ActionContext(
context,
new RouteData(),
new ActionDescriptor());
var actionExecutingContext = new ActionExecutingContext(
actionContext,
routeFilters,
new Dictionary<string, object?>(),
route);
var actionExecutedContext = new ActionExecutedContext(
actionContext,
routeFilters,
route);
try
{
var processingTask = new Func<Task>(() => ProcessWebSocketRequest(route, context));
await BeginProcessingPipeline(actionExecutingContext, actionExecutedContext, processingTask);
return Results.Empty;
}
catch (WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely)
{
logger.LogInformation("Websocket closed prematurely. Marking as closed");
return Results.Empty;
}
catch (OperationCanceledException)
{
logger.LogInformation("Websocket closed prematurely. Marking as closed");
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)