Skip to content

Commit 11bdfe1

Browse files
committedNov 2, 2024
Instead of throwing there, we emit diagnostics when the type is invalid.
1 parent d8acb6d commit 11bdfe1

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed
 

‎src/Http/Http.Extensions/gen/DiagnosticDescriptors.cs

+9
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,13 @@ internal static class DiagnosticDescriptors
125125
DiagnosticSeverity.Warning,
126126
isEnabledByDefault: true,
127127
helpLinkUri: GetHelpLinkUrl("RDG013"));
128+
129+
public static DiagnosticDescriptor InvalidAsParametersEnumerableType { get; } = new(
130+
"RDG014",
131+
new LocalizableResourceString(nameof(Resources.InvalidAsParametersEnumerableType_Title), Resources.ResourceManager, typeof(Resources)),
132+
new LocalizableResourceString(nameof(Resources.InvalidAsParametersEnumerableType_Message), Resources.ResourceManager, typeof(Resources)),
133+
"Usage",
134+
DiagnosticSeverity.Warning,
135+
isEnabledByDefault: true,
136+
helpLinkUri: GetHelpLinkUrl("RDG014"));
128137
}

‎src/Http/Http.Extensions/gen/Resources.resx

+6
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,10 @@
195195
<data name="KeyedAndNotKeyedServiceAttributesNotSupported_Message" xml:space="preserve">
196196
<value>The [FromKeyedServices] attribute is not supported on parameters that are also annotated with IFromServiceMetadata. For more information, please see https://aka.ms/aspnet/rdg-known-issues</value>
197197
</data>
198+
<data name="InvalidAsParametersEnumerableType_Title" xml:space="preserve">
199+
<value>Invalid enumerable type</value>
200+
</data>
201+
<data name="InvalidAsParametersEnumerableType_Message" xml:space="preserve">
202+
<value>The enumerable type '{0}' is not supported. For more information, please see https://aka.ms/aspnet/rdg-known-issues</value>
203+
</data>
198204
</root>

‎src/Http/Http.Extensions/gen/StaticRouteHandlerModel/EndpointParameter.cs

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Collections;
56
using System.Collections.Generic;
67
using System.Collections.Immutable;
78
using System.Diagnostics.CodeAnalysis;
@@ -466,6 +467,12 @@ private static bool TryGetAsParametersConstructor(Endpoint endpoint, INamedTypeS
466467
return false;
467468
}
468469

470+
if (type.AllInterfaces.Any(x => x.ToString() == typeof(IEnumerable).FullName))
471+
{
472+
endpoint.Diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.InvalidAsParametersEnumerableType, location, parameterTypeString));
473+
return false;
474+
}
475+
469476
var constructors = type.Constructors.Where(constructor => constructor.DeclaredAccessibility == Accessibility.Public && !constructor.IsStatic);
470477
var numOfConstructors = constructors.Count();
471478
// When leveraging parameterless constructors, we want to ensure we only emit for writable

‎src/Http/Http.Extensions/test/RequestDelegateGenerator/CompileTimeCreationTests.AsParameters.cs

+6
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,19 @@ static string GetNoContructorsError(Type type)
5050
static string GetInvalidConstructorError(Type type)
5151
=> $"The public parameterized constructor must contain only parameters that match the declared public properties for type '{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}'. For more information, please see https://aka.ms/aspnet/rdg-known-issues";
5252

53+
static string GetEnumerableTypeError(Type type)
54+
=> $"The enumerable type '{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}' is not supported. For more information, please see https://aka.ms/aspnet/rdg-known-issues";
55+
5356
return new []
5457
{
5558
new object[] { "BadArgumentListRecord", DiagnosticDescriptors.InvalidAsParametersSingleConstructorOnly.Id, GetMultipleContructorsError(typeof(BadArgumentListRecord)) },
5659
new object[] { "BadArgumentListClass", DiagnosticDescriptors.InvalidAsParametersSignature.Id, GetInvalidConstructorError(typeof(BadArgumentListClass)) },
5760
new object[] { "BadArgumentListClassMultipleCtors", DiagnosticDescriptors.InvalidAsParametersSingleConstructorOnly.Id, GetMultipleContructorsError(typeof(BadArgumentListClassMultipleCtors)) },
5861
new object[] { "BadAbstractArgumentListClass", DiagnosticDescriptors.InvalidAsParametersAbstractType.Id, GetAbstractTypeError(typeof(BadAbstractArgumentListClass)) },
5962
new object[] { "BadNoPublicConstructorArgumentListClass", DiagnosticDescriptors.InvalidAsParametersNoConstructorFound.Id, GetNoContructorsError(typeof(BadNoPublicConstructorArgumentListClass)) },
63+
new object[] { "List<string>", DiagnosticDescriptors.InvalidAsParametersEnumerableType.Id, GetEnumerableTypeError(typeof(List<string>)) },
64+
new object[] { "List<IFormFile>", DiagnosticDescriptors.InvalidAsParametersEnumerableType.Id, GetEnumerableTypeError(typeof(List<IFormFile>)) },
65+
new object[] { "Dictionary<string, string>", DiagnosticDescriptors.InvalidAsParametersEnumerableType.Id, GetEnumerableTypeError(typeof(Dictionary<string, string>)) }
6066
};
6167
}
6268
}

0 commit comments

Comments
 (0)