Skip to content

Commit cfbeda2

Browse files
committed
Move SupplyParameterFromFormAttribute to Microsoft.AspNetCore.Components.Web
1 parent a5b3b53 commit cfbeda2

7 files changed

+58
-15
lines changed

src/Components/Components/src/CascadingParameterState.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Concurrent;
55
using System.Diagnostics.CodeAnalysis;
6+
using System.Linq;
67
using System.Reflection;
78
using Microsoft.AspNetCore.Components.Reflection;
89
using Microsoft.AspNetCore.Components.Rendering;
@@ -103,15 +104,16 @@ private static ReflectedCascadingParameterInfo[] CreateReflectedCascadingParamet
103104
attribute.Name));
104105
}
105106

106-
var fromForm = prop.GetCustomAttribute<SupplyParameterFromFormAttribute>();
107-
if (fromForm != null)
107+
var hostParameterAttribute = prop.GetCustomAttributes()
108+
.OfType<IHostEnvironmentCascadingParameter>().SingleOrDefault();
109+
if (hostParameterAttribute != null)
108110
{
109111
result ??= new List<ReflectedCascadingParameterInfo>();
110112

111113
result.Add(new ReflectedCascadingParameterInfo(
112114
prop.Name,
113115
prop.PropertyType,
114-
fromForm.Name));
116+
hostParameterAttribute.Name));
115117
}
116118
}
117119

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Components;
5+
6+
// Marks a cascading parameter that can be offered via an attribute that is not
7+
// directly defined in the Components assembly. For example [SupplyParameterFromForm].
8+
internal interface IHostEnvironmentCascadingParameter
9+
{
10+
public string? Name { get; set; }
11+
}

src/Components/Components/src/PublicAPI.Unshipped.txt

-4
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddComponentParamete
5959
Microsoft.AspNetCore.Components.StreamRenderingAttribute
6060
Microsoft.AspNetCore.Components.StreamRenderingAttribute.Enabled.get -> bool
6161
Microsoft.AspNetCore.Components.StreamRenderingAttribute.StreamRenderingAttribute(bool enabled) -> void
62-
Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute
63-
Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute.Name.get -> string?
64-
Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute.Name.set -> void
65-
Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute.SupplyParameterFromFormAttribute() -> void
6662
override Microsoft.AspNetCore.Components.EventCallback.GetHashCode() -> int
6763
override Microsoft.AspNetCore.Components.EventCallback.Equals(object? obj) -> bool
6864
override Microsoft.AspNetCore.Components.EventCallback<TValue>.GetHashCode() -> int

src/Components/Components/src/Reflection/ComponentProperties.cs

+27-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Concurrent;
55
using System.Diagnostics.CodeAnalysis;
6+
using System.Linq;
67
using System.Reflection;
78
using static Microsoft.AspNetCore.Internal.LinkerFlags;
89

@@ -170,12 +171,12 @@ private static void ThrowForUnknownIncomingParameterName([DynamicallyAccessedMem
170171
{
171172
if (!propertyInfo.IsDefined(typeof(ParameterAttribute)) &&
172173
!propertyInfo.IsDefined(typeof(CascadingParameterAttribute)) &&
173-
!propertyInfo.IsDefined(typeof(SupplyParameterFromFormAttribute)))
174+
!propertyInfo.GetCustomAttributes().OfType<IHostEnvironmentCascadingParameter>().Any())
174175
{
175176
throw new InvalidOperationException(
176177
$"Object of type '{targetType.FullName}' has a property matching the name '{parameterName}', " +
177178
$"but it does not have [{nameof(ParameterAttribute)}], [{nameof(CascadingParameterAttribute)}] or " +
178-
$"[{nameof(SupplyParameterFromFormAttribute)}] applied.");
179+
$"[SupplyParameterFromFormAttribute] applied.");
179180
}
180181
else
181182
{
@@ -260,11 +261,30 @@ public WritersForType([DynamicallyAccessedMembers(Component)] Type targetType)
260261

261262
foreach (var propertyInfo in GetCandidateBindableProperties(targetType))
262263
{
263-
var parameterAttribute = propertyInfo.GetCustomAttribute<ParameterAttribute>();
264-
var cascadingParameterAttribute = propertyInfo.GetCustomAttribute<CascadingParameterAttribute>();
265-
var supplyParameterFromFormAttribute = propertyInfo.GetCustomAttribute<SupplyParameterFromFormAttribute>();
264+
ParameterAttribute? parameterAttribute = null;
265+
CascadingParameterAttribute? cascadingParameterAttribute = null;
266+
IHostEnvironmentCascadingParameter? hostEnvironmentCascadingParameter = null;
266267

267-
var isParameter = parameterAttribute != null || cascadingParameterAttribute != null || supplyParameterFromFormAttribute != null;
268+
var attributes = propertyInfo.GetCustomAttributes();
269+
foreach (var attribute in attributes)
270+
{
271+
switch (attribute)
272+
{
273+
case ParameterAttribute parameter:
274+
parameterAttribute = parameter;
275+
break;
276+
case CascadingParameterAttribute cascadingParameter:
277+
cascadingParameterAttribute = cascadingParameter;
278+
break;
279+
case IHostEnvironmentCascadingParameter hostEnvironmentAttribute:
280+
hostEnvironmentCascadingParameter = hostEnvironmentAttribute;
281+
break;
282+
default:
283+
break;
284+
}
285+
}
286+
287+
var isParameter = parameterAttribute != null || cascadingParameterAttribute != null || hostEnvironmentCascadingParameter != null;
268288
if (!isParameter)
269289
{
270290
continue;
@@ -279,7 +299,7 @@ public WritersForType([DynamicallyAccessedMembers(Component)] Type targetType)
279299

280300
var propertySetter = new PropertySetter(targetType, propertyInfo)
281301
{
282-
Cascading = cascadingParameterAttribute != null || supplyParameterFromFormAttribute != null,
302+
Cascading = cascadingParameterAttribute != null || hostEnvironmentCascadingParameter != null,
283303
};
284304

285305
if (_underlyingWriters.ContainsKey(propertyName))

src/Components/Components/test/CascadingParameterStateTest.cs

+10
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,13 @@ public TestNavigationManager()
550550
}
551551
}
552552
}
553+
554+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
555+
public sealed class SupplyParameterFromFormAttribute : Attribute, IHostEnvironmentCascadingParameter
556+
{
557+
/// <summary>
558+
/// Gets or sets the name for the parameter. The name is used to match
559+
/// the form data and decide whether or not the value needs to be bound.
560+
/// </summary>
561+
public string Name { get; set; }
562+
}

src/Components/Web/src/PublicAPI.Unshipped.txt

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ Microsoft.AspNetCore.Components.HtmlRendering.Infrastructure.StaticHtmlRenderer.
1717
Microsoft.AspNetCore.Components.HtmlRendering.Infrastructure.StaticHtmlRenderer.BeginRenderingComponent(System.Type! componentType, Microsoft.AspNetCore.Components.ParameterView initialParameters) -> Microsoft.AspNetCore.Components.Web.HtmlRendering.HtmlRootComponent
1818
Microsoft.AspNetCore.Components.HtmlRendering.Infrastructure.StaticHtmlRenderer.StaticHtmlRenderer(System.IServiceProvider! serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory! loggerFactory) -> void
1919
Microsoft.AspNetCore.Components.RenderTree.WebRenderer.WaitUntilAttachedAsync() -> System.Threading.Tasks.Task!
20+
Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute
21+
Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute.Name.get -> string?
22+
Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute.Name.set -> void
23+
Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute.SupplyParameterFromFormAttribute() -> void
2024
Microsoft.AspNetCore.Components.Web.AutoRenderMode
2125
Microsoft.AspNetCore.Components.Web.AutoRenderMode.AutoRenderMode() -> void
2226
Microsoft.AspNetCore.Components.Web.AutoRenderMode.AutoRenderMode(bool prerender) -> void

src/Components/Components/src/SupplyParameterFromFormAttribute.cs renamed to src/Components/Web/src/SupplyParameterFromFormAttribute.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Components;
88
/// the form data for the form with the specified name.
99
/// </summary>
1010
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
11-
public sealed class SupplyParameterFromFormAttribute : Attribute
11+
public sealed class SupplyParameterFromFormAttribute : Attribute, IHostEnvironmentCascadingParameter
1212
{
1313
/// <summary>
1414
/// Gets or sets the name for the parameter. The name is used to match

0 commit comments

Comments
 (0)