Skip to content

Commit 0fa988e

Browse files
Copilotjaviercn
andcommitted
Add IComponent validation and improve documentation for NotAuthorizedPage
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
1 parent 1a6bcee commit 0fa988e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Components/Authorization/src/AuthorizeRouteView.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public AuthorizeRouteView()
5353

5454
/// <summary>
5555
/// The page type that will be displayed if the user is not authorized.
56-
/// The page type must implement <see cref="IComponent"/> and have a <see cref="RouteAttribute"/>.
56+
/// The page type must implement <see cref="IComponent"/>.
5757
/// </summary>
5858
[Parameter]
5959
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
@@ -121,6 +121,12 @@ private void RenderNotAuthorizedInDefaultLayout(RenderTreeBuilder builder, Authe
121121
{
122122
if (NotAuthorizedPage is not null)
123123
{
124+
if (!typeof(IComponent).IsAssignableFrom(NotAuthorizedPage))
125+
{
126+
throw new InvalidOperationException($"The type {NotAuthorizedPage.FullName} " +
127+
$"does not implement {typeof(IComponent).FullName}.");
128+
}
129+
124130
RenderPageInDefaultLayout(builder, NotAuthorizedPage);
125131
}
126132
else

src/Components/Authorization/test/AuthorizeRouteViewTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,28 @@ public void WhenNotAuthorized_NotAuthorizedPageTakesPriorityOverNotAuthorizedCon
434434
edit => AssertPrependText(batch, edit, "Layout ends here"));
435435
}
436436

437+
[Fact]
438+
public void WhenNotAuthorized_ThrowsForInvalidNotAuthorizedPageType()
439+
{
440+
// Arrange: set NotAuthorizedPage to a type that doesn't implement IComponent
441+
var routeData = new RouteData(typeof(TestPageRequiringAuthorization), EmptyParametersDictionary);
442+
_testAuthorizationService.NextResult = AuthorizationResult.Failed();
443+
444+
// Act & Assert
445+
var exception = Assert.Throws<InvalidOperationException>(() =>
446+
{
447+
_renderer.RenderRootComponent(_authorizeRouteViewComponentId, ParameterView.FromDictionary(new Dictionary<string, object>
448+
{
449+
{ nameof(AuthorizeRouteView.RouteData), routeData },
450+
{ nameof(AuthorizeRouteView.DefaultLayout), typeof(TestLayout) },
451+
{ nameof(AuthorizeRouteView.NotAuthorizedPage), typeof(string) }, // string doesn't implement IComponent
452+
}));
453+
});
454+
455+
Assert.Contains("does not implement", exception.Message);
456+
Assert.Contains("IComponent", exception.Message);
457+
}
458+
437459
private static void AssertPrependText(CapturedBatch batch, RenderTreeEdit edit, string text)
438460
{
439461
Assert.Equal(RenderTreeEditType.PrependFrame, edit.Type);

0 commit comments

Comments
 (0)