Skip to content
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 @@ -238,9 +238,15 @@ public WritersForType(Type targetType)
continue;
}

var propertyName = propertyInfo.Name;
if (parameterAttribute != null && (propertyInfo.SetMethod == null || !propertyInfo.SetMethod.IsPublic))
{
throw new InvalidOperationException(
$"The type '{targetType.FullName}' declares a parameter matching the name '{propertyName}' that is not public. Parameters must be public.");
}

var propertySetter = MemberAssignment.CreatePropertySetter(targetType, propertyInfo, cascading: cascadingParameterAttribute != null);

var propertyName = propertyInfo.Name;
if (WritersByName.ContainsKey(propertyName))
{
throw new InvalidOperationException(
Expand Down
19 changes: 18 additions & 1 deletion src/Components/Components/src/Reflection/MemberAssignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,34 @@ internal class MemberAssignment
public static IEnumerable<PropertyInfo> GetPropertiesIncludingInherited(
Type type, BindingFlags bindingFlags)
{
var dictionary = new Dictionary<string, List<PropertyInfo>>();

while (type != null)
{
var properties = type.GetProperties(bindingFlags)
.Where(prop => prop.DeclaringType == type);
foreach (var property in properties)
{
yield return property;
if (!dictionary.TryGetValue(property.Name, out var others))
{
others = new List<PropertyInfo>();
dictionary.Add(property.Name, others);
}

if (others.Any(other => other.GetMethod?.GetBaseDefinition() == property.GetMethod?.GetBaseDefinition()))
{
// This is an inheritance case. We can safely ignore the value of property since
// we have seen a more derived value.
continue;
}

others.Add(property);
}

type = type.BaseType;
}

return dictionary.Values.SelectMany(p => p);
}

public static IPropertySetter CreatePropertySetter(Type targetType, PropertyInfo property, bool cascading)
Expand Down
83 changes: 78 additions & 5 deletions src/Components/Components/test/ParameterViewTest.Assignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.RenderTree;
using Xunit;

namespace Microsoft.AspNetCore.Components
Expand Down Expand Up @@ -71,6 +70,24 @@ public void IncomingParameterMatchesInheritedDeclaredParameter_SetsValue()
Assert.Equal(456, target.DerivedClassIntProp);
}

[Fact]
public void IncomingParameterMatchesOverridenParameter_ThatDoesNotHasAttribute()
{
// Test for https://github.com/aspnet/AspNetCore/issues/13162
// Arrange
var parameters = new ParameterViewBuilder
{
{ nameof(DerivedType.VirtualProp), 123 },
}.Build();
var target = new DerivedType();

// Act
parameters.SetParameterProperties(target);

// Assert
Assert.Equal(123, target.VirtualProp);
}

[Fact]
public void NoIncomingParameterMatchesDeclaredParameter_LeavesValueUnchanged()
{
Expand Down Expand Up @@ -172,6 +189,43 @@ public void IncomingParameterMatchesPropertyNotDeclaredAsParameter_Throws()
ex.Message);
}

[Fact]
public void IncomingParameterMatchesPropertyNotPublic_Throws()
{
// Arrange
var target = new HasNonPublicPropertyWithParameterAttribute();
var parameters = new ParameterViewBuilder
{
{ nameof(HasNonPublicPropertyWithParameterAttribute.IntProp), 123 },
}.Build();

// Act
var ex = Assert.Throws<InvalidOperationException>(
() => parameters.SetParameterProperties(target));

// Assert
Assert.Equal(default, target.IntProp);
Assert.Equal(
$"The type '{typeof(HasNonPublicPropertyWithParameterAttribute).FullName}' declares a parameter matching the name '{nameof(HasNonPublicPropertyWithParameterAttribute.IntProp)}' that is not public. Parameters must be public.",
ex.Message);
}

[Fact]
public void IncomingCascadingParameterMatchesPropertyNotPublic_Works()
{
// Arrange
var target = new HasNonPublicCascadingParameter();
var builder = new ParameterViewBuilder();
builder.Add("Cascading", "Test", cascading: true);
var parameters = builder.Build();

// Act
parameters.SetParameterProperties(target);

// Assert
Assert.Equal("Test", target.GetCascadingValue());
}

[Fact]
public void IncomingNonCascadingValueMatchesCascadingParameter_Throws()
{
Expand Down Expand Up @@ -497,13 +551,9 @@ public void SupplyingNullWritesDefaultForType()

class HasInstanceProperties
{
// "internal" to show we're not requiring public accessors, but also
// to keep the assertions simple in the tests

[Parameter] public int IntProp { get; set; }
[Parameter] public string StringProp { get; set; }

// Also a truly private one to show there's nothing special about 'internal'
[Parameter] public object ObjectProp { get; set; }

public static string ObjectPropName => nameof(ObjectProp);
Expand All @@ -521,6 +571,12 @@ class HasCascadingParameter

class HasPropertyWithoutParameterAttribute
{
public int IntProp { get; set; }
}

class HasNonPublicPropertyWithParameterAttribute
{
[Parameter]
internal int IntProp { get; set; }
}

Expand All @@ -539,6 +595,16 @@ class HasInheritedProperties : HasInstanceProperties
[Parameter] public int DerivedClassIntProp { get; set; }
}

class BaseType
{
[Parameter] public virtual int VirtualProp { get; set; }
}

class DerivedType : BaseType
{
public override int VirtualProp { get; set; }
}

class HasParametersVaryingOnlyByCase
{
[Parameter] public object MyValue { get; set; }
Expand Down Expand Up @@ -575,6 +641,13 @@ class HasWrongTypeCaptureUnmatchedValuesProperty
[Parameter(CaptureUnmatchedValues = true)] public KeyValuePair<string, object>[] CaptureUnmatchedValuesProp { get; set; }
}

class HasNonPublicCascadingParameter
{
[CascadingParameter] private string Cascading { get; set; }

public string GetCascadingValue() => Cascading;
}

class ParameterViewBuilder : IEnumerable
{
private readonly List<(string Name, object Value, bool Cascading)> _keyValuePairs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2231,7 +2231,7 @@ private class FakeComponent : IComponent
public object ObjectProperty { get; set; }

[Parameter]
public string ReadonlyProperty { get; private set; }
public string ReadonlyProperty { get; set; }

[Parameter]
public string PrivateProperty { get; set; }
Expand Down
8 changes: 4 additions & 4 deletions src/Components/Components/test/RendererTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3791,10 +3791,10 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
private class FakeComponent : IComponent
{
[Parameter]
public int IntProperty { get; private set; }
public int IntProperty { get; set; }

[Parameter]
public string StringProperty { get; private set; }
public string StringProperty { get; set; }

[Parameter]
public object ObjectProperty { get; set; }
Expand Down Expand Up @@ -3927,7 +3927,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
private class ReRendersParentComponent : AutoRenderComponent
{
[Parameter]
public TestComponent Parent { get; private set; }
public TestComponent Parent { get; set; }

private bool _isFirstTime = true;

Expand Down Expand Up @@ -4057,7 +4057,7 @@ private class DisposableComponent : AutoRenderComponent, IDisposable
public bool Disposed { get; private set; }

[Parameter]
public Action DisposeAction { get; private set; }
public Action DisposeAction { get; set; }

public void Dispose()
{
Expand Down