Skip to content

Commit 001e108

Browse files
committed
Fix shared code references in analyzers and address feedback
1 parent 26d174a commit 001e108

File tree

6 files changed

+22
-17
lines changed

6 files changed

+22
-17
lines changed

src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteHandlers/AtMostOneFromBodyAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private static void AtMostOneFromBodyAttribute(
2727
foreach (var asParameterDecoratedParameter in asParametersDecoratedParameters)
2828
{
2929
var fromBodyMetadataInterfaceMembers = asParameterDecoratedParameter.Type.GetMembers().Where(
30-
m => m.TryGetAttributeImplementingInterface(fromBodyMetadataInterfaceType)
30+
m => m.HasAttributeImplementingInterface(fromBodyMetadataInterfaceType)
3131
);
3232

3333
if (fromBodyMetadataInterfaceMembers.Count() >= 2)
@@ -36,7 +36,7 @@ private static void AtMostOneFromBodyAttribute(
3636
}
3737
}
3838

39-
var fromBodyMetadataInterfaceParameters = methodSymbol.Parameters.Where(p => p.TryGetAttributeImplementingInterface(fromBodyMetadataInterfaceType));
39+
var fromBodyMetadataInterfaceParameters = methodSymbol.Parameters.Where(p => p.HasAttributeImplementingInterface(fromBodyMetadataInterfaceType));
4040

4141
if (fromBodyMetadataInterfaceParameters.Count() >= 2)
4242
{

src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteHandlers/DisallowNonParsableComplexTypesOnParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static bool ReportFromAttributeDiagnostic(OperationAnalysisContext context, Well
9898
{
9999
var fromMetadataInterfaceTypeSymbol = wellKnownTypes.Get(fromMetadataInterfaceType);
100100
var parsability = ParsabilityHelper.GetParsability(parameterTypeSymbol, wellKnownTypes);
101-
if (parameter.TryGetAttributeImplementingInterface(fromMetadataInterfaceTypeSymbol) && parsability != Parsability.Parsable)
101+
if (parameter.HasAttributeImplementingInterface(fromMetadataInterfaceTypeSymbol) && parsability != Parsability.Parsable)
102102
{
103103
context.ReportDiagnostic(Diagnostic.Create(
104104
DiagnosticDescriptors.RouteParameterComplexTypeIsNotParsable,

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,16 @@ private void ProcessEndpointParameterSource(Endpoint endpoint, ISymbol symbol, I
142142
}
143143
if (symbol is IPropertySymbol ||
144144
Type is not INamedTypeSymbol namedTypeSymbol ||
145-
!TryGetAsParametersConstructor(endpoint, namedTypeSymbol, out var isParameterlessConstructor, out var matchedParameters))
145+
!TryGetAsParametersConstructor(endpoint, namedTypeSymbol, out var isDefaultConstructor, out var matchedProperties))
146146
{
147147
if (symbol is IPropertySymbol)
148148
{
149149
endpoint.Diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.InvalidAsParametersNested, location));
150150
}
151151
return;
152152
}
153-
EndpointParameters = matchedParameters.Select(matchedParameter => new EndpointParameter(endpoint, matchedParameter.Property, matchedParameter.Parameter, wellKnownTypes));
154-
if (isParameterlessConstructor == true)
153+
EndpointParameters = matchedProperties.Select(matchedParameter => new EndpointParameter(endpoint, matchedParameter.Property, matchedParameter.Parameter, wellKnownTypes));
154+
if (isDefaultConstructor == true)
155155
{
156156
var parameterList = string.Join(", ", EndpointParameters.Select(p => $"{p.LookupName} = {p.EmitHandlerArgument()}"));
157157
AssigningCode = $"new {namedTypeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} {{ {parameterList} }}";
@@ -471,10 +471,10 @@ private static string GetEscapedParameterName(AttributeData attribute, string pa
471471
}
472472
}
473473

474-
private static bool TryGetAsParametersConstructor(Endpoint endpoint, INamedTypeSymbol type, out bool? isParameterlessConstructor, [NotNullWhen(true)] out IEnumerable<ConstructorParameter>? matchedParameters)
474+
private static bool TryGetAsParametersConstructor(Endpoint endpoint, INamedTypeSymbol type, out bool? isDefaultConstructor, [NotNullWhen(true)] out IEnumerable<ConstructorParameter>? matchedProperties)
475475
{
476-
isParameterlessConstructor = null;
477-
matchedParameters = null;
476+
isDefaultConstructor = null;
477+
matchedProperties = null;
478478
var parameterTypeString = type.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat);
479479
var location = endpoint.Operation.Syntax.GetLocation();
480480
if (type.IsAbstract)
@@ -492,7 +492,7 @@ private static bool TryGetAsParametersConstructor(Endpoint endpoint, INamedTypeS
492492

493493
if (numOfConstructors == 1)
494494
{
495-
var targetConstructor = constructors.SingleOrDefault();
495+
var targetConstructor = constructors.Single();
496496
var lookupTable = new Dictionary<ParameterLookupKey, IPropertySymbol>();
497497
foreach (var property in properties)
498498
{
@@ -504,8 +504,8 @@ private static bool TryGetAsParametersConstructor(Endpoint endpoint, INamedTypeS
504504

505505
if (parameters.Length == 0)
506506
{
507-
isParameterlessConstructor = true;
508-
matchedParameters = writableProperties.Select(property => new ConstructorParameter(property, null));
507+
isDefaultConstructor = true;
508+
matchedProperties = writableProperties.Select(property => new ConstructorParameter(property, null));
509509
return true;
510510
}
511511

@@ -523,15 +523,15 @@ private static bool TryGetAsParametersConstructor(Endpoint endpoint, INamedTypeS
523523
}
524524
}
525525

526-
isParameterlessConstructor = false;
527-
matchedParameters = propertiesWithParameterInfo;
526+
isDefaultConstructor = false;
527+
matchedProperties = propertiesWithParameterInfo;
528528
return true;
529529
}
530530

531531
if (type.IsValueType)
532532
{
533-
isParameterlessConstructor = true;
534-
matchedParameters = writableProperties.Select(property => new ConstructorParameter(property, null));
533+
isDefaultConstructor = true;
534+
matchedProperties = writableProperties.Select(property => new ConstructorParameter(property, null));
535535
return true;
536536
}
537537

src/Http/Http.Extensions/src/RequestDelegateFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ private static Expression BindParameterFromProperties(ParameterInfo parameter, R
14951495
for (var i = 0; i < properties.Length; i++)
14961496
{
14971497
// For parameterless ctor we will init only writable properties.
1498-
if (properties[i].CanWrite)
1498+
if (properties[i].CanWrite && properties[i].GetSetMethod(nonPublic: false) != null)
14991499
{
15001500
var parameterInfo = new PropertyAsParameterInfo(properties[i], factoryContext.NullabilityContext);
15011501
bindings.Add(Expression.Bind(properties[i], CreateArgument(parameterInfo, factoryContext)));

src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTests.AsParameters.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,15 @@ void TestAction(HttpContext context, [AsParameters] ParameterListWithReadOnlyPro
290290
httpContext.Request.RouteValues[nameof(ParameterListWithReadOnlyProperties.Value)] = routeParamValue.ToString(NumberFormatInfo.InvariantInfo);
291291
httpContext.Request.RouteValues[nameof(ParameterListWithReadOnlyProperties.ConstantValue)] = routeParamValue.ToString(NumberFormatInfo.InvariantInfo);
292292
httpContext.Request.RouteValues[nameof(ParameterListWithReadOnlyProperties.ReadOnlyValue)] = routeParamValue.ToString(NumberFormatInfo.InvariantInfo);
293+
httpContext.Request.RouteValues[nameof(ParameterListWithReadOnlyProperties.PrivateSetValue)] = routeParamValue.ToString(NumberFormatInfo.InvariantInfo);
293294

294295
await endpoint.RequestDelegate(httpContext);
295296

296297
var input = Assert.IsType<ParameterListWithReadOnlyProperties>(httpContext.Items["input"]);
297298
Assert.Equal(expectedInput.Value, input.Value);
298299
Assert.Equal(expectedInput.ConstantValue, input.ConstantValue);
299300
Assert.Equal(expectedInput.ReadOnlyValue, input.ReadOnlyValue);
301+
Assert.Equal(expectedInput.PrivateSetValue, input.PrivateSetValue);
300302
}
301303

302304
[Fact]

src/Http/Http.Extensions/test/RequestDelegateGenerator/SharedTypes.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,13 +799,16 @@ public class ParameterListWithReadOnlyProperties
799799
public ParameterListWithReadOnlyProperties()
800800
{
801801
ReadOnlyValue = 1;
802+
PrivateSetValue = 2;
802803
}
803804

804805
public int Value { get; set; }
805806

806807
public int ConstantValue => 1;
807808

808809
public int ReadOnlyValue { get; }
810+
811+
public int PrivateSetValue { get; private set; }
809812
}
810813

811814
public record struct SampleParameterList(int Foo);

0 commit comments

Comments
 (0)