Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/GeneratedEndpoints/MinimalApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,8 @@ private static bool RequestHandlerFilter(SyntaxNode syntaxNode, CancellationToke

var (httpMethod, pattern, name, summary, description) = GetRequestHandlerAttribute(attribute, cancellationToken);

var (tags, requireAuthorization, authorizationPolicies, disableAntiforgery, allowAnonymous, accepts, produces,
producesProblem, producesValidationProblem)
var (tags, requireAuthorization, authorizationPolicies, disableAntiforgery, allowAnonymous, excludeFromDescription,
accepts, produces, producesProblem, producesValidationProblem)
= GetAdditionalRequestHandlerAttributes(requestHandlerClassSymbol, requestHandlerMethodSymbol, cancellationToken);

name ??= RemoveAsyncSuffix(requestHandlerMethod.Name);
Expand All @@ -608,7 +608,8 @@ private static bool RequestHandlerFilter(SyntaxNode syntaxNode, CancellationToke
accepts,
produces,
producesProblem,
producesValidationProblem
producesValidationProblem,
excludeFromDescription
);

var requestHandler = new RequestHandler(requestHandlerClass, requestHandlerMethod, httpMethod, pattern, metadata, requireAuthorization,
Expand Down Expand Up @@ -689,6 +690,7 @@ private static (
EquatableImmutableArray<string>? authorizationPolicies,
bool disableAntiforgery,
bool allowAnonymous,
bool excludeFromDescription,
EquatableImmutableArray<AcceptsMetadata>? accepts,
EquatableImmutableArray<ProducesMetadata>? produces,
EquatableImmutableArray<ProducesProblemMetadata>? producesProblem,
Expand All @@ -702,6 +704,7 @@ private static (
EquatableImmutableArray<string>? authorizationPolicies = null;
var disableAntiforgery = false;
var allowAnonymous = false;
var excludeFromDescription = false;

List<AcceptsMetadata>? accepts = null;
List<ProducesMetadata>? produces = null;
Expand All @@ -716,6 +719,7 @@ private static (
ref authorizationPolicies,
ref disableAntiforgery,
ref allowAnonymous,
ref excludeFromDescription,
ref accepts,
ref produces,
ref producesProblem,
Expand All @@ -730,6 +734,7 @@ ref producesValidationProblem
ref authorizationPolicies,
ref disableAntiforgery,
ref allowAnonymous,
ref excludeFromDescription,
ref accepts,
ref produces,
ref producesProblem,
Expand All @@ -742,6 +747,7 @@ ref producesValidationProblem
authorizationPolicies,
disableAntiforgery,
allowAnonymous,
excludeFromDescription,
ToEquatableOrNull(accepts),
ToEquatableOrNull(produces),
ToEquatableOrNull(producesProblem),
Expand All @@ -756,6 +762,7 @@ private static void GetAdditionalRequestHandlerAttributeValues(
ref EquatableImmutableArray<string>? authorizationPolicies,
ref bool disableAntiforgery,
ref bool allowAnonymous,
ref bool excludeFromDescription,
ref List<AcceptsMetadata>? accepts,
ref List<ProducesMetadata>? produces,
ref List<ProducesProblemMetadata>? producesProblem,
Expand Down Expand Up @@ -821,6 +828,9 @@ ref List<ProducesValidationProblemMetadata>? producesValidationProblem
case $"global::{AllowAnonymousAttributeFullyQualifiedName}":
allowAnonymous = true;
break;
case "global::Microsoft.AspNetCore.Routing.ExcludeFromDescriptionAttribute":
excludeFromDescription = true;
break;
case $"global::{ProducesProblemAttributeFullyQualifiedName}":
{
var statusCode = attribute.ConstructorArguments.Length > 0 && attribute.ConstructorArguments[0].Value is int producesProblemStatusCode
Expand Down Expand Up @@ -1467,6 +1477,13 @@ private static void GenerateMapRequestHandler(StringBuilder source, RequestHandl
source.Append(')');
}

if (requestHandler.Metadata.ExcludeFromDescription)
{
source.AppendLine();
source.Append(continuationIndent);
source.Append(".ExcludeFromDescription()");
}

if (requestHandler.Metadata.Tags is { Count: > 0 })
{
source.AppendLine();
Expand Down Expand Up @@ -1623,6 +1640,8 @@ private static StringBuilder GetUseEndpointHandlersStringBuilder(ImmutableArray<
cost += 24 + metadata.Summary.Length;
if (metadata.Description is { Length: > 0 })
cost += 28 + metadata.Description.Length;
if (metadata.ExcludeFromDescription)
cost += 32;

if (metadata.Tags is { Count: > 0 })
cost += metadata.Tags.Value.Sum(tag => 6 + tag.Length);
Expand Down Expand Up @@ -1872,7 +1891,8 @@ private readonly record struct RequestHandlerMetadata(
EquatableImmutableArray<AcceptsMetadata>? Accepts,
EquatableImmutableArray<ProducesMetadata>? Produces,
EquatableImmutableArray<ProducesProblemMetadata>? ProducesProblem,
EquatableImmutableArray<ProducesValidationProblemMetadata>? ProducesValidationProblem
EquatableImmutableArray<ProducesValidationProblemMetadata>? ProducesValidationProblem,
bool ExcludeFromDescription
);

private readonly record struct AcceptsMetadata(string RequestType, string ContentType, EquatableImmutableArray<string>? AdditionalContentTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ internal static class EndpointRouteBuilderExtensions
.WithName("GetComplex")
.WithSummary("Gets complex data.")
.WithDescription("Uses every supported attribute.")
.ExcludeFromDescription()
.WithTags("Shared", "ClassLevel", "MethodLevel")
.Accepts<global::GeneratedEndpointsTests.ClassLevelRequest>("application/xml", "text/xml")
.Accepts<global::GeneratedEndpointsTests.GetRequest>("application/custom", "text/custom")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ internal static class EndpointRouteBuilderExtensions
.WithName("GetComplex")
.WithSummary("Gets complex data.")
.WithDescription("Uses every supported attribute.")
.ExcludeFromDescription()
.WithTags("Shared", "ClassLevel", "MethodLevel")
.Accepts<global::ClassLevelRequest>("application/xml", "text/xml")
.Accepts<global::GetRequest>("application/custom", "text/custom")
Expand Down
2 changes: 2 additions & 0 deletions tests/GeneratedEndpoints.Tests/GeneratedEndpointsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public async Task MapAllAttributesAndHttpMethods(bool withNamespace)
[Microsoft.AspNetCore.Generated.Attributes.ProducesResponse(typeof(ClassLevelResponse), 201, "application/json", "text/json")]
[ProducesProblem(503, "application/problem+json")]
[ProducesValidationProblem(409, "application/problem+json", "text/plain")]
[Microsoft.AspNetCore.Routing.ExcludeFromDescription]
internal sealed class ComplexEndpoints
{
private readonly IServiceProvider _serviceProvider;
Expand All @@ -141,6 +142,7 @@ public static void Configure<TBuilder>(TBuilder builder, IServiceProvider servic
[Microsoft.AspNetCore.Generated.Attributes.ProducesResponse<GetResponse>(200, "application/json", "text/json")]
[ProducesProblem(400, "application/problem+json", "text/plain")]
[ProducesValidationProblem(422, "application/problem+json", "text/plain")]
[Microsoft.AspNetCore.Routing.ExcludeFromDescription]
public async Task<Results<Ok<GetResponse>, NotFound>> GetComplex(
[FromRoute] int id,
[FromQuery] string? filter,
Expand Down
Loading