Skip to content

Descriptive Error for Improper Use of [AsParameters] #58218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 10, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,13 @@ internal static class DiagnosticDescriptors
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
helpLinkUri: GetHelpLinkUrl("RDG013"));

public static DiagnosticDescriptor InvalidAsParametersEnumerableType { get; } = new(
"RDG014",
new LocalizableResourceString(nameof(Resources.InvalidAsParametersEnumerableType_Title), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.InvalidAsParametersEnumerableType_Message), Resources.ResourceManager, typeof(Resources)),
"Usage",
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
helpLinkUri: GetHelpLinkUrl("RDG014"));
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,10 @@
<data name="KeyedAndNotKeyedServiceAttributesNotSupported_Message" xml:space="preserve">
<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>
</data>
<data name="InvalidAsParametersEnumerableType_Title" xml:space="preserve">
<value>Invalid enumerable type</value>
</data>
<data name="InvalidAsParametersEnumerableType_Message" xml:space="preserve">
<value>The enumerable type '{0}' is not supported. For more information, please see https://aka.ms/aspnet/rdg-known-issues</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -168,7 +169,7 @@ private void ProcessEndpointParameterSource(Endpoint endpoint, ISymbol symbol, I
}
if (symbol is IPropertySymbol ||
Type is not INamedTypeSymbol namedTypeSymbol ||
!TryGetAsParametersConstructor(endpoint, namedTypeSymbol, out var isDefaultConstructor, out var matchedProperties))
!TryGetAsParametersConstructor(endpoint, namedTypeSymbol, wellKnownTypes, out var isDefaultConstructor, out var matchedProperties))
{
if (symbol is IPropertySymbol)
{
Expand Down Expand Up @@ -454,7 +455,7 @@ private static string GetEscapedParameterName(AttributeData attribute, string pa
}
}

private static bool TryGetAsParametersConstructor(Endpoint endpoint, INamedTypeSymbol type, out bool? isDefaultConstructor, [NotNullWhen(true)] out IEnumerable<ConstructorParameter>? matchedProperties)
private static bool TryGetAsParametersConstructor(Endpoint endpoint, INamedTypeSymbol type, WellKnownTypes wellKnownTypes, out bool? isDefaultConstructor, [NotNullWhen(true)] out IEnumerable<ConstructorParameter>? matchedProperties)
{
isDefaultConstructor = null;
matchedProperties = null;
Expand All @@ -466,6 +467,12 @@ private static bool TryGetAsParametersConstructor(Endpoint endpoint, INamedTypeS
return false;
}

if (type.Implements(wellKnownTypes.Get(WellKnownType.System_Collections_IEnumerable)))
{
endpoint.Diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.InvalidAsParametersEnumerableType, location, parameterTypeString));
return false;
}

var constructors = type.Constructors.Where(constructor => constructor.DeclaredAccessibility == Accessibility.Public && !constructor.IsStatic);
var numOfConstructors = constructors.Count();
// When leveraging parameterless constructors, we want to ensure we only emit for writable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,19 @@ static string GetNoContructorsError(Type type)
static string GetInvalidConstructorError(Type type)
=> $"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";

static string GetEnumerableTypeError(Type type)
=> $"The enumerable type '{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}' is not supported. For more information, please see https://aka.ms/aspnet/rdg-known-issues";

return new []
{
new object[] { "BadArgumentListRecord", DiagnosticDescriptors.InvalidAsParametersSingleConstructorOnly.Id, GetMultipleContructorsError(typeof(BadArgumentListRecord)) },
new object[] { "BadArgumentListClass", DiagnosticDescriptors.InvalidAsParametersSignature.Id, GetInvalidConstructorError(typeof(BadArgumentListClass)) },
new object[] { "BadArgumentListClassMultipleCtors", DiagnosticDescriptors.InvalidAsParametersSingleConstructorOnly.Id, GetMultipleContructorsError(typeof(BadArgumentListClassMultipleCtors)) },
new object[] { "BadAbstractArgumentListClass", DiagnosticDescriptors.InvalidAsParametersAbstractType.Id, GetAbstractTypeError(typeof(BadAbstractArgumentListClass)) },
new object[] { "BadNoPublicConstructorArgumentListClass", DiagnosticDescriptors.InvalidAsParametersNoConstructorFound.Id, GetNoContructorsError(typeof(BadNoPublicConstructorArgumentListClass)) },
new object[] { "List<string>", DiagnosticDescriptors.InvalidAsParametersEnumerableType.Id, GetEnumerableTypeError(typeof(List<string>)) },
new object[] { "List<IFormFile>", DiagnosticDescriptors.InvalidAsParametersEnumerableType.Id, GetEnumerableTypeError(typeof(List<IFormFile>)) },
new object[] { "Dictionary<string, string>", DiagnosticDescriptors.InvalidAsParametersEnumerableType.Id, GetEnumerableTypeError(typeof(Dictionary<string, string>)) }
};
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Shared/RoslynUtils/WellKnownTypeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public enum WellKnownType
Microsoft_AspNetCore_Http_IFormCollection,
Microsoft_AspNetCore_Http_IFormFileCollection,
Microsoft_AspNetCore_Http_IFormFile,
System_Collections_IEnumerable,
System_DateOnly,
System_DateTimeOffset,
System_IO_Stream,
Expand Down Expand Up @@ -146,6 +147,7 @@ public enum WellKnownType
"Microsoft.AspNetCore.Http.IFormCollection",
"Microsoft.AspNetCore.Http.IFormFileCollection",
"Microsoft.AspNetCore.Http.IFormFile",
"System.Collections.IEnumerable",
"System.DateOnly",
"System.DateTimeOffset",
"System.IO.Stream",
Expand Down
Loading