Skip to content

Commit fe932b8

Browse files
author
Bart Koelman
committed
Fail at startup when resource implements IVersionedIdentifiable without IVersionedIdentifiable<TId, TVersion>
1 parent c154330 commit fe932b8

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Diff for: src/JsonApiDotNetCore/Configuration/ResourceGraphBuilder.cs

+7
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ public ResourceGraphBuilder Add(Type resourceClrType, Type? idClrType = null, st
127127
return this;
128128
}
129129

130+
if (resourceClrType.IsOrImplementsInterface<IVersionedIdentifiable>() &&
131+
!resourceClrType.IsOrImplementsInterface(typeof(IVersionedIdentifiable<,>)))
132+
{
133+
throw new InvalidConfigurationException(
134+
$"Resource type '{resourceClrType}' implements 'IVersionedIdentifiable', but not 'IVersionedIdentifiable<TId, TVersion>'.");
135+
}
136+
130137
if (resourceClrType.IsOrImplementsInterface<IIdentifiable>())
131138
{
132139
string effectivePublicName = publicName ?? FormatResourceName(resourceClrType);

Diff for: test/JsonApiDotNetCoreTests/UnitTests/ResourceGraph/ResourceGraphBuilderTests.cs

+22
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ public void Cannot_add_resource_that_implements_only_non_generic_IIdentifiable()
167167
.WithMessage($"Resource type '{typeof(ResourceWithoutId)}' implements 'IIdentifiable', but not 'IIdentifiable<TId>'.");
168168
}
169169

170+
[Fact]
171+
public void Cannot_add_versioned_resource_that_implements_only_non_generic_IVersionedIdentifiable()
172+
{
173+
// Arrange
174+
var options = new JsonApiOptions();
175+
var builder = new ResourceGraphBuilder(options, NullLoggerFactory.Instance);
176+
177+
// Act
178+
Action action = () => builder.Add(typeof(VersionedResourceWithoutToken));
179+
180+
// Assert
181+
action.Should().ThrowExactly<InvalidConfigurationException>().WithMessage(
182+
$"Resource type '{typeof(VersionedResourceWithoutToken)}' implements 'IVersionedIdentifiable', but not 'IVersionedIdentifiable<TId, TVersion>'.");
183+
}
184+
170185
[Fact]
171186
public void Cannot_build_graph_with_missing_related_HasOne_resource()
172187
{
@@ -341,6 +356,13 @@ private sealed class ResourceWithoutId : IIdentifiable
341356
public string? LocalId { get; set; }
342357
}
343358

359+
private sealed class VersionedResourceWithoutToken : IVersionedIdentifiable
360+
{
361+
public string? StringId { get; set; }
362+
public string? LocalId { get; set; }
363+
public string? Version { get; set; }
364+
}
365+
344366
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
345367
private sealed class NonResource
346368
{

0 commit comments

Comments
 (0)