Skip to content

Commit a5c164b

Browse files
Bart Koelmanbkoelman
Bart Koelman
authored andcommitted
Fail at startup when resource implements IVersionedIdentifiable without IVersionedIdentifiable<TId, TVersion>
1 parent 8542ee7 commit a5c164b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/JsonApiDotNetCore/Configuration/ResourceGraphBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ public ResourceGraphBuilder Add(Type resourceClrType, Type? idClrType = null, st
207207
return this;
208208
}
209209

210+
if (resourceClrType.IsOrImplementsInterface<IVersionedIdentifiable>() && !resourceClrType.IsOrImplementsInterface(typeof(IVersionedIdentifiable<,>)))
211+
{
212+
throw new InvalidConfigurationException(
213+
$"Resource type '{resourceClrType}' implements 'IVersionedIdentifiable', but not 'IVersionedIdentifiable<TId, TVersion>'.");
214+
}
215+
210216
if (resourceClrType.IsOrImplementsInterface<IIdentifiable>())
211217
{
212218
string effectivePublicName = publicName ?? FormatResourceName(resourceClrType);

test/JsonApiDotNetCoreTests/UnitTests/ResourceGraph/ResourceGraphBuilderTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ public void Cannot_add_resource_that_implements_only_non_generic_IIdentifiable()
163163
.WithMessage($"Resource type '{typeof(ResourceWithoutId)}' implements 'IIdentifiable', but not 'IIdentifiable<TId>'.");
164164
}
165165

166+
[Fact]
167+
public void Cannot_add_versioned_resource_that_implements_only_non_generic_IVersionedIdentifiable()
168+
{
169+
// Arrange
170+
var options = new JsonApiOptions();
171+
var builder = new ResourceGraphBuilder(options, NullLoggerFactory.Instance);
172+
173+
// Act
174+
Action action = () => builder.Add(typeof(VersionedResourceWithoutToken));
175+
176+
// Assert
177+
action.Should().ThrowExactly<InvalidConfigurationException>().WithMessage(
178+
$"Resource type '{typeof(VersionedResourceWithoutToken)}' implements 'IVersionedIdentifiable', but not 'IVersionedIdentifiable<TId, TVersion>'.");
179+
}
180+
166181
[Fact]
167182
public void Cannot_build_graph_with_missing_related_HasOne_resource()
168183
{
@@ -428,6 +443,13 @@ private sealed class ResourceWithoutId : IIdentifiable
428443
public string? LocalId { get; set; }
429444
}
430445

446+
private sealed class VersionedResourceWithoutToken : IVersionedIdentifiable
447+
{
448+
public string? StringId { get; set; }
449+
public string? LocalId { get; set; }
450+
public string? Version { get; set; }
451+
}
452+
431453
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
432454
private sealed class NonResource
433455
{

0 commit comments

Comments
 (0)