Skip to content

Commit 0dfb728

Browse files
committed
produces metadata improvements #422
1 parent 8024cb1 commit 0dfb728

File tree

9 files changed

+337
-14
lines changed

9 files changed

+337
-14
lines changed

Src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
33

4-
<Version>5.8.1.10-beta</Version>
4+
<Version>5.8.1.11-beta</Version>
55

66
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
77
<LangVersion>latest</LangVersion>

Src/Library/Endpoint/Auxiliary/EndpointExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ internal static class EndpointExtensions
1212
internal static string ActualTypeName(this Type type)
1313
=> (Nullable.GetUnderlyingType(type) ?? type).Name;
1414

15+
internal static bool RequiresAuthorization(this EndpointDefinition ep)
16+
{
17+
return ep.AllowedPermissions?.Any() is true ||
18+
ep.AllowedClaimTypes?.Any() is true ||
19+
ep.AllowedRoles?.Any() is true ||
20+
ep.AuthSchemeNames?.Any() is true ||
21+
ep.PolicyBuilder is not null;
22+
}
23+
1524
internal static void Initialize(this EndpointDefinition def, BaseEndpoint instance, HttpContext? ctx)
1625
{
1726
instance.Definition = def;

Src/Library/Endpoint/Endpoint.Setup.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,12 @@ public sealed override void Verbs(params string[] methods)
423423
else
424424
b.Produces<TResponse>(200, "application/json");
425425

426+
if (Definition.AnonymousVerbs?.Any() is not true)
427+
b.Produces(401);
428+
429+
if (Definition.RequiresAuthorization())
430+
b.Produces(403);
431+
426432
var opts = FastEndpoints.Config.ErrOpts;
427433
if (opts.ProducesMetadataType is not null && Definition.ValidatorType is not null)
428434
b.Produces(opts.StatusCode, opts.ProducesMetadataType, "application/problem+json");

Src/Library/Main/MainExtensions.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ namespace FastEndpoints;
1919
/// </summary>
2020
public static class MainExtensions
2121
{
22-
///// <summary>
23-
///// WARNING: this data is only available for a few minutes after app startup. It is automatically cleared to release unneeded memory.
24-
///// <para>DO NOT ACCESS AFTER APP STARTUP!!!</para>
25-
///// </summary>
26-
//internal static EndpointData Endpoints { get; private set; }
27-
2822
/// <summary>
2923
/// adds the FastEndpoints services to the ASP.Net middleware pipeline
3024
/// </summary>
@@ -234,14 +228,8 @@ private static IAuthorizeData[] BuildAuthorizeAttributes(EndpointDefinition ep)
234228
if (ep.PreBuiltUserPolicies?.Any() is true)
235229
policiesToAdd.AddRange(ep.PreBuiltUserPolicies);
236230

237-
if (ep.AllowedPermissions?.Any() is true ||
238-
ep.AllowedClaimTypes?.Any() is true ||
239-
ep.AllowedRoles?.Any() is true ||
240-
ep.AuthSchemeNames?.Any() is true ||
241-
ep.PolicyBuilder is not null)
242-
{
231+
if (ep.RequiresAuthorization())
243232
policiesToAdd.Add(ep.SecurityPolicyName);
244-
}
245233

246234
return policiesToAdd.Select(p =>
247235
{

Src/Library/Main/RouteHandlerBuilderExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Http.Metadata;
34

45
namespace FastEndpoints;
56

@@ -13,4 +14,21 @@ public static RouteHandlerBuilder ProducesProblemFE(this RouteHandlerBuilder hb,
1314

1415
public static RouteHandlerBuilder ProducesProblemDetails(this RouteHandlerBuilder hb, int statusCode = 400, string contentType = "application/problem+json")
1516
=> hb.ProducesProblemFE<ProblemDetails>(statusCode, contentType);
17+
18+
/// <summary>
19+
/// clears any number of given produces response type metadata from the endpoint by supplying the status codes of the responses to remove
20+
/// </summary>
21+
/// <param name="statusCodes">one or more status codes of the defaults to remove</param>
22+
public static RouteHandlerBuilder ClearDefaultProduces(this RouteHandlerBuilder hb, params int[] statusCodes)
23+
{
24+
hb.Add(epBuilder =>
25+
{
26+
foreach (var m in epBuilder.Metadata.ToArray())
27+
{
28+
if (m is IProducesResponseTypeMetadata meta && statusCodes.Contains(meta.StatusCode))
29+
epBuilder.Metadata.Remove(m);
30+
}
31+
});
32+
return hb;
33+
}
1634
}

Src/Library/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ here's an [example](https://github.com/FastEndpoints/FastEndpoints/blob/4831acea
99
- `ValidationContext<T>` class for manipulating the validation failures list of the current endpoint [#info](https://discord.com/channels/933662816458645504/1090551226598432828)
1010
- `RFC8707` compatible problem detail (error response) builder [#info](https://discord.com/channels/933662816458645504/1093917953528971344)
1111
- `JsonExceptionTransformer` func to enable customization of error messages when STJ throws due to invalid json input [#info](https://discord.com/channels/933662816458645504/1095670893113528370/1095923891605622884)
12+
- `ClearDefaultProduces(200,401,401)` extension method to clear chosen produces metadata added by default #432
1213

1314
### IMPROVEMENTS
1415
- add overload to `AddError()`, `ThrowError()`, `ThrowIfAnyErrors()` methods to accept a `ValidationFailure` [#info](https://discord.com/channels/933662816458645504/1090551226598432828/1090934715952926740)
1516
- populate inner exception of `InvalidOperationException` thrown by the testing extensions #422
1617
- modify source generator for incremental generation #426
18+
- automatically add `401 - Unauthorized` and `403 - Forbidden` produces metadata to endpoints by default #422
1719
- upgrade dependencies to latest
1820

1921
### FIXES

0 commit comments

Comments
 (0)