1- using System . Runtime . CompilerServices ;
21using Microsoft . CodeAnalysis ;
32using static GeneratedEndpoints . Common . Constants ;
43
54namespace GeneratedEndpoints . Common ;
65
76internal static class EndpointConfigurationFactory
87{
9- private static readonly ConditionalWeakTable < INamedTypeSymbol , GeneratedAttributeKindCacheEntry > GeneratedAttributeKindCache = new ( ) ;
10-
118 public static EndpointConfiguration Create ( ISymbol symbol )
129 {
1310 var attributes = symbol . GetAttributes ( ) ;
@@ -48,7 +45,7 @@ public static EndpointConfiguration Create(ISymbol symbol)
4845 if ( attributeClass is null )
4946 continue ;
5047
51- var attributeKind = GetGeneratedAttributeKind ( attributeClass ) ;
48+ var attributeKind = attributeClass . OriginalDefinition . GetRequestHandlerAttributeKind ( ) ;
5249 switch ( attributeKind )
5350 {
5451 case RequestHandlerAttributeKind . ShortCircuit :
@@ -172,9 +169,14 @@ public static EndpointConfiguration Create(ISymbol symbol)
172169 WithRequestTimeout = withRequestTimeout ?? false ,
173170 RequestTimeoutPolicyName = requestTimeoutPolicyName ,
174171 Order = order ,
175- GroupIdentifier = groupIdentifier ,
176- GroupPattern = groupPattern ,
177- GroupName = groupName ,
172+ Group = groupIdentifier is not null && groupPattern is not null
173+ ? new EndpointGroup
174+ {
175+ Identifier = groupIdentifier ,
176+ Pattern = groupPattern ,
177+ Name = groupName ,
178+ }
179+ : null ,
178180 } ;
179181 }
180182
@@ -198,16 +200,6 @@ public static EndpointConfiguration Create(ISymbol symbol)
198200 return StringBuilderPool . ToStringAndReturn ( builder ) ;
199201 }
200202
201- private static RequestHandlerAttributeKind GetGeneratedAttributeKind ( INamedTypeSymbol attributeClass )
202- {
203- var definition = attributeClass . OriginalDefinition ;
204- var cacheEntry = GeneratedAttributeKindCache . GetValue (
205- definition , static def => new GeneratedAttributeKindCacheEntry ( def . GetRequestHandlerAttributeKind ( ) )
206- ) ;
207-
208- return cacheEntry . Kind ;
209- }
210-
211203 private static EquatableImmutableArray < T > ? ToEquatableOrNull < T > ( List < T > ? values )
212204 {
213205 return values is { Count : > 0 } ? values . ToEquatableImmutableArray ( ) : null ;
@@ -219,9 +211,11 @@ private static void TryAddAcceptsMetadata(AttributeData attribute, INamedTypeSym
219211 if ( attributeClass is { IsGenericType : true , TypeArguments . Length : 1 } )
220212 requestType = attributeClass . TypeArguments [ 0 ]
221213 . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ;
222- else if ( attribute . GetNamedTypeSymbol ( RequestTypeAttributeNamedParameter ) is { } requestTypeSymbol )
223- requestType = requestTypeSymbol . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ;
224214 else
215+ requestType = attribute . GetConstructorTypeSymbol ( )
216+ ? . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ;
217+
218+ if ( requestType is null )
225219 return ;
226220
227221 var contentType = attribute . GetConstructorStringValue ( ) ?? ApplicationJsonContentType ;
@@ -236,23 +230,29 @@ private static void TryAddAcceptsMetadata(AttributeData attribute, INamedTypeSym
236230
237231 private static void TryAddProducesMetadata ( AttributeData attribute , INamedTypeSymbol attributeClass , ref List < ProducesMetadata > ? produces )
238232 {
239- string ? responseType ;
233+ ProducesMetadata ? producesMetadata ;
240234 if ( attributeClass is { IsGenericType : true , TypeArguments . Length : 1 } )
241- responseType = attributeClass . TypeArguments [ 0 ]
235+ {
236+ var responseType = attributeClass . TypeArguments [ 0 ]
242237 . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ;
243- else if ( attribute . GetNamedTypeSymbol ( ResponseTypeAttributeNamedParameter ) is { } responseTypeSymbol )
244- responseType = responseTypeSymbol . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ;
238+ var statusCode = attribute . GetConstructorIntValue ( 0 ) ?? 200 ;
239+ var contentType = attribute . GetConstructorStringValue ( 1 ) ;
240+ var additionalContentTypes = attribute . GetConstructorStringArray ( 2 ) ;
241+ producesMetadata = new ProducesMetadata ( responseType , statusCode , contentType , additionalContentTypes ) ;
242+ }
245243 else
246- return ;
247-
248- var statusCode = attribute . GetConstructorIntValue ( ) ?? 200 ;
249- var contentType = attribute . GetConstructorStringValue ( 1 ) ;
250- var additionalContentTypes = attribute . GetConstructorStringArray ( 2 ) ;
251-
252- var producesMetadata = new ProducesMetadata ( responseType , statusCode , contentType , additionalContentTypes ) ;
244+ {
245+ var responseType = attribute . GetConstructorTypeSymbol ( )
246+ ? . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat )
247+ ?? "" ;
248+ var statusCode = attribute . GetConstructorIntValue ( 1 ) ?? 200 ;
249+ var contentType = attribute . GetConstructorStringValue ( 2 ) ;
250+ var additionalContentTypes = attribute . GetConstructorStringArray ( 3 ) ;
251+ producesMetadata = new ProducesMetadata ( responseType , statusCode , contentType , additionalContentTypes ) ;
252+ }
253253
254254 var producesList = produces ??= [ ] ;
255- producesList . Add ( producesMetadata ) ;
255+ producesList . Add ( producesMetadata . Value ) ;
256256 }
257257
258258 private static void TryAddEndpointFilter (
@@ -291,9 +291,4 @@ private static void TryAddEndpointFilterType(ITypeSymbol? typeSymbol, ref List<s
291291 endpointFilters ??= [ ] ;
292292 endpointFilters . Add ( displayString ) ;
293293 }
294-
295- private sealed class GeneratedAttributeKindCacheEntry ( RequestHandlerAttributeKind kind )
296- {
297- public RequestHandlerAttributeKind Kind { get ; } = kind ;
298- }
299294}
0 commit comments