Skip to content

Commit 9fe9794

Browse files
author
Bart Koelman
committed
Fail at startup on controller that derives from BaseJsonApiController, but uses a resource type that is not registered in the resource graph
1 parent 9ffb3bc commit 9fe9794

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

src/JsonApiDotNetCore/Middleware/JsonApiRoutingConvention.cs

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public void Apply(ApplicationModel application)
8484
_resourceTypePerControllerTypeMap.Add(controller.ControllerType, resourceType);
8585
_controllerPerResourceTypeMap.Add(resourceType, controller);
8686
}
87+
else
88+
{
89+
throw new InvalidConfigurationException($"Controller '{controller.ControllerType}' depends on " +
90+
$"resource type '{resourceClrType}', which does not exist in the resource graph.");
91+
}
8792
}
8893
}
8994

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using JetBrains.Annotations;
2+
using JsonApiDotNetCore.Resources;
3+
4+
namespace JsonApiDotNetCoreTests.IntegrationTests.NonJsonApiControllers
5+
{
6+
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
7+
public sealed class UnknownResource : Identifiable<int>
8+
{
9+
public string? Value { get; set; }
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using FluentAssertions;
3+
using JsonApiDotNetCore.Errors;
4+
using TestBuildingBlocks;
5+
using Xunit;
6+
7+
namespace JsonApiDotNetCoreTests.IntegrationTests.NonJsonApiControllers
8+
{
9+
public sealed class UnknownResourceControllerTests : IntegrationTestContext<TestableStartup<NonJsonApiDbContext>, NonJsonApiDbContext>
10+
{
11+
public UnknownResourceControllerTests()
12+
{
13+
UseController<UnknownResourcesController>();
14+
}
15+
16+
[Fact]
17+
public void Fails_at_startup_when_using_controller_for_resource_type_that_is_not_registered_in_resource_graph()
18+
{
19+
// Act
20+
Action action = () => _ = Factory;
21+
22+
// Assert
23+
action.Should().ThrowExactly<InvalidConfigurationException>().WithMessage($"Controller '{typeof(UnknownResourcesController)}' " +
24+
$"depends on resource type '{typeof(UnknownResource)}', which does not exist in the resource graph.");
25+
}
26+
27+
public override void Dispose()
28+
{
29+
// Prevents crash when test cleanup tries to access lazily constructed Factory.
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using JsonApiDotNetCore.Configuration;
2+
using JsonApiDotNetCore.Controllers;
3+
using JsonApiDotNetCore.Services;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace JsonApiDotNetCoreTests.IntegrationTests.NonJsonApiControllers
7+
{
8+
public sealed class UnknownResourcesController : JsonApiController<UnknownResource, int>
9+
{
10+
public UnknownResourcesController(IJsonApiOptions options, IResourceGraph resourceGraph, ILoggerFactory loggerFactory,
11+
IResourceService<UnknownResource, int> resourceService)
12+
: base(options, resourceGraph, loggerFactory, resourceService)
13+
{
14+
}
15+
}
16+
}

test/TestBuildingBlocks/IntegrationTestContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private WebApplicationFactory<TStartup> CreateFactory()
111111
return factoryWithConfiguredContentRoot;
112112
}
113113

114-
public void Dispose()
114+
public virtual void Dispose()
115115
{
116116
RunOnDatabaseAsync(async dbContext => await dbContext.Database.EnsureDeletedAsync()).Wait();
117117

0 commit comments

Comments
 (0)