Skip to content

Commit fec8a29

Browse files
committed
Cleanup.
1 parent 5375c9b commit fec8a29

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/GeneratedEndpoints/MinimalApiGenerator.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public sealed class MinimalApiGenerator : IIncrementalGenerator
9999
private const string UseEndpointHandlersMethodName = "MapEndpointHandlers";
100100
private const string UseEndpointHandlersMethodHint = $"{RoutingNamespace}.{UseEndpointHandlersMethodName}.g.cs";
101101

102+
private const string ConfigureMethodName = "Configure";
102103
private const string AsyncSuffix = "Async";
103104

104105
private static readonly string FileHeader = $"""
@@ -852,7 +853,7 @@ ref List<ProducesValidationProblemMetadata>? producesValidationProblem
852853
? GetStringArrayValues(attribute.ConstructorArguments[2])
853854
: null;
854855

855-
var producesProblemList = producesProblem ??= new List<ProducesProblemMetadata>();
856+
var producesProblemList = producesProblem ??= [];
856857
producesProblemList.Add(new ProducesProblemMetadata(statusCode, contentType, additionalContentTypes));
857858
break;
858859
}
@@ -868,7 +869,7 @@ ref List<ProducesValidationProblemMetadata>? producesValidationProblem
868869
? GetStringArrayValues(attribute.ConstructorArguments[2])
869870
: null;
870871

871-
var producesValidationProblemList = producesValidationProblem ??= new List<ProducesValidationProblemMetadata>();
872+
var producesValidationProblemList = producesValidationProblem ??= [];
872873
producesValidationProblemList.Add(new ProducesValidationProblemMetadata(statusCode, contentType, additionalContentTypes));
873874
break;
874875
}
@@ -923,12 +924,12 @@ private static void TryAddAcceptsMetadata(
923924
INamedTypeSymbol attributeClass,
924925
ref List<AcceptsMetadata>? accepts)
925926
{
926-
string? requestType = null;
927+
string? requestType;
927928
string contentType;
928929
EquatableImmutableArray<string>? additionalContentTypes;
929930
var isOptional = GetNamedBoolValue(attribute, IsOptionalAttributeNamedParameter);
930931

931-
if (attributeClass.IsGenericType && attributeClass.TypeArguments.Length == 1)
932+
if (attributeClass is { IsGenericType: true, TypeArguments.Length: 1 })
932933
{
933934
requestType = attributeClass.TypeArguments[0].ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
934935
contentType = attribute.ConstructorArguments.Length > 0
@@ -953,7 +954,7 @@ private static void TryAddAcceptsMetadata(
953954
return;
954955
}
955956

956-
var acceptsList = accepts ??= new List<AcceptsMetadata>();
957+
var acceptsList = accepts ??= [];
957958
acceptsList.Add(new AcceptsMetadata(requestType, contentType, additionalContentTypes, isOptional));
958959
}
959960

@@ -962,12 +963,12 @@ private static void TryAddProducesMetadata(
962963
INamedTypeSymbol attributeClass,
963964
ref List<ProducesMetadata>? produces)
964965
{
965-
string? responseType = null;
966+
string? responseType;
966967
int statusCode;
967968
string? contentType;
968969
EquatableImmutableArray<string>? additionalContentTypes;
969970

970-
if (attributeClass.IsGenericType && attributeClass.TypeArguments.Length == 1)
971+
if (attributeClass is { IsGenericType: true, TypeArguments.Length: 1 })
971972
{
972973
responseType = attributeClass.TypeArguments[0].ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
973974
statusCode = attribute.ConstructorArguments.Length > 0 && attribute.ConstructorArguments[0].Value is int producesStatusCode
@@ -998,7 +999,7 @@ private static void TryAddProducesMetadata(
998999
return;
9991000
}
10001001

1001-
var producesList = produces ??= new List<ProducesMetadata>();
1002+
var producesList = produces ??= [];
10021003
producesList.Add(new ProducesMetadata(responseType, statusCode, contentType, additionalContentTypes));
10031004
}
10041005

@@ -1102,7 +1103,7 @@ CancellationToken cancellationToken
11021103
var hasConfigureMethod = false;
11031104
var acceptsServiceProvider = false;
11041105

1105-
foreach (var member in classSymbol.GetMembers("Configure"))
1106+
foreach (var member in classSymbol.GetMembers(ConfigureMethodName))
11061107
{
11071108
cancellationToken.ThrowIfCancellationRequested();
11081109

@@ -1426,7 +1427,9 @@ private static void GenerateMapRequestHandler(StringBuilder source, RequestHandl
14261427
{
14271428
source.Append(" ");
14281429
source.Append(requestHandler.Class.Name);
1429-
source.AppendLine(".Configure(");
1430+
source.Append('.');
1431+
source.Append(ConfigureMethodName);
1432+
source.AppendLine("(");
14301433
}
14311434

14321435
source.Append(indent);

tests/GeneratedEndpoints.Tests.Lab/GetUserEndpoint.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNetCore.Generated.Attributes;
55
using Microsoft.AspNetCore.Http;
66
using Microsoft.AspNetCore.Http.HttpResults;
7+
using Microsoft.AspNetCore.Mvc;
78

89
namespace GeneratedEndpoints.Tests.Lab;
910

@@ -15,13 +16,13 @@ internal static class GetUserEndpoint
1516
[Tags("Featured")]
1617
[AllowAnonymous]
1718
[Accepts("application/json", "application/xml", RequestType = typeof(GetUserRequest))]
18-
[Accepts<GetUserMetadata>("application/json", "application/xml")]
19+
[Accepts<GetUserMetadata>("application/json", "application/xml", IsOptional = true)]
1920
[ProducesResponse( StatusCodes.Status200OK, "application/json", ResponseType = typeof(UserProfile))]
2021
[ProducesResponse<UserProfile>(StatusCodes.Status202Accepted, "application/json")]
2122
[ProducesProblem(StatusCodes.Status500InternalServerError, "application/problem+json")]
2223
[ProducesValidationProblem(StatusCodes.Status400BadRequest, "application/problem+json")]
2324
[MapGet("/users/{id:int}", Name = nameof(GetUser), Summary = "Gets a user by ID.", Description = "Gets a user by ID when the ID is greater than zero.")]
24-
public static Results<Ok<UserProfile>, NotFound, ValidationProblem, ProblemHttpResult> GetUser(int id)
25+
public static Results<Ok<UserProfile>, NotFound, ValidationProblem, ProblemHttpResult> GetUser([FromQuery] int id)
2526
{
2627
if (id <= 0)
2728
{

0 commit comments

Comments
 (0)