Skip to content

Commit b5032b9

Browse files
RazorComponentResult: Nullability and trim annotations
1 parent 903c44a commit b5032b9

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ Microsoft.AspNetCore.Http.HttpResults.RazorComponentResult.Parameters.get -> Sys
4444
Microsoft.AspNetCore.Http.HttpResults.RazorComponentResult.PreventStreamingRendering.get -> bool
4545
Microsoft.AspNetCore.Http.HttpResults.RazorComponentResult.PreventStreamingRendering.set -> void
4646
Microsoft.AspNetCore.Http.HttpResults.RazorComponentResult.RazorComponentResult(System.Type! componentType) -> void
47-
Microsoft.AspNetCore.Http.HttpResults.RazorComponentResult.RazorComponentResult(System.Type! componentType, object? parameters) -> void
48-
Microsoft.AspNetCore.Http.HttpResults.RazorComponentResult.RazorComponentResult(System.Type! componentType, System.Collections.Generic.IReadOnlyDictionary<string!, object?>? parameters) -> void
47+
Microsoft.AspNetCore.Http.HttpResults.RazorComponentResult.RazorComponentResult(System.Type! componentType, object! parameters) -> void
48+
Microsoft.AspNetCore.Http.HttpResults.RazorComponentResult.RazorComponentResult(System.Type! componentType, System.Collections.Generic.IReadOnlyDictionary<string!, object?>! parameters) -> void
4949
Microsoft.AspNetCore.Http.HttpResults.RazorComponentResult.StatusCode.get -> int?
5050
Microsoft.AspNetCore.Http.HttpResults.RazorComponentResult.StatusCode.set -> void
5151
Microsoft.AspNetCore.Http.HttpResults.RazorComponentResult<TComponent>

src/Components/Endpoints/src/Results/RazorComponentResult.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.ObjectModel;
45
using System.Diagnostics.CodeAnalysis;
56
using Microsoft.AspNetCore.Components;
67
using Microsoft.AspNetCore.Components.Endpoints;
@@ -22,7 +23,7 @@ public class RazorComponentResult : IResult
2223
/// </summary>
2324
/// <param name="componentType">The type of the component to render. This must implement <see cref="IComponent"/>.</param>
2425
public RazorComponentResult([DynamicallyAccessedMembers(Component)] Type componentType)
25-
: this(componentType, null)
26+
: this(componentType, ReadOnlyDictionary<string, object?>.Empty)
2627
{
2728
}
2829

@@ -31,8 +32,10 @@ public RazorComponentResult([DynamicallyAccessedMembers(Component)] Type compone
3132
/// </summary>
3233
/// <param name="componentType">The type of the component to render. This must implement <see cref="IComponent"/>.</param>
3334
/// <param name="parameters">Parameters for the component.</param>
34-
public RazorComponentResult([DynamicallyAccessedMembers(Component)] Type componentType, object? parameters)
35-
: this(componentType, CoerceParametersObjectToDictionary(parameters))
35+
public RazorComponentResult(
36+
[DynamicallyAccessedMembers(Component)] Type componentType,
37+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] object parameters)
38+
: this(componentType, CoerceParametersObjectToDictionary(parameters)!)
3639
{
3740
}
3841

@@ -41,19 +44,20 @@ public RazorComponentResult([DynamicallyAccessedMembers(Component)] Type compone
4144
/// </summary>
4245
/// <param name="componentType">The type of the component to render. This must implement <see cref="IComponent"/>.</param>
4346
/// <param name="parameters">Parameters for the component.</param>
44-
public RazorComponentResult([DynamicallyAccessedMembers(Component)] Type componentType, IReadOnlyDictionary<string, object?>? parameters)
47+
public RazorComponentResult([DynamicallyAccessedMembers(Component)] Type componentType, IReadOnlyDictionary<string, object?> parameters)
4548
{
49+
ArgumentNullException.ThrowIfNull(componentType);
50+
ArgumentNullException.ThrowIfNull(parameters);
51+
4652
// Note that the Blazor renderer will validate that componentType implements IComponent and throws a suitable
4753
// exception if not, so we don't need to duplicate that logic here.
48-
49-
ArgumentNullException.ThrowIfNull(componentType);
5054
ComponentType = componentType;
5155
Parameters = parameters ?? EmptyParameters;
5256
}
5357

5458
private static IReadOnlyDictionary<string, object?>? CoerceParametersObjectToDictionary(object? parameters)
5559
=> parameters is null
56-
? null
60+
? throw new ArgumentNullException(nameof(parameters))
5761
: (IReadOnlyDictionary<string, object?>)PropertyHelper.ObjectToDictionary(parameters);
5862

5963
/// <summary>

src/Components/Endpoints/test/RazorComponentResultTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.ObjectModel;
45
using System.Diagnostics;
56
using System.Text.RegularExpressions;
67
using Microsoft.AspNetCore.Components.Endpoints.Forms;
@@ -23,11 +24,10 @@ namespace Microsoft.AspNetCore.Components.Endpoints;
2324
public class RazorComponentResultTest
2425
{
2526
[Fact]
26-
public void AcceptsNullParameters()
27+
public void RejectsNullParameters()
2728
{
28-
var result = new RazorComponentResult(typeof(SimpleComponent), null);
29-
Assert.NotNull(result.Parameters);
30-
Assert.Empty(result.Parameters);
29+
Assert.Throws<ArgumentNullException>(() => new RazorComponentResult(typeof(SimpleComponent), (object)null));
30+
Assert.Throws<ArgumentNullException>(() => new RazorComponentResult(typeof(SimpleComponent), null));
3131
}
3232

3333
[Fact]

0 commit comments

Comments
 (0)