Skip to content

Commit 0e4432b

Browse files
committed
Add API to remove a resource from the graph (after assembly scanning)
1 parent 325a39c commit 0e4432b

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/JsonApiDotNetCore/Configuration/ResourceGraphBuilder.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,32 @@ private static bool IsImplicitManyToManyJoinEntity(IEntityType entityType)
167167
return entityType is { IsPropertyBag: true, HasSharedClrType: true };
168168
}
169169

170+
/// <summary>
171+
/// Removes a JSON:API resource.
172+
/// </summary>
173+
/// <typeparam name="TResource">
174+
/// The resource CLR type.
175+
/// </typeparam>
176+
public ResourceGraphBuilder Remove<TResource>()
177+
where TResource : class, IIdentifiable
178+
{
179+
return Remove(typeof(TResource));
180+
}
181+
182+
/// <summary>
183+
/// Removes a JSON:API resource.
184+
/// </summary>
185+
/// <param name="resourceClrType">
186+
/// The resource CLR type.
187+
/// </param>
188+
public ResourceGraphBuilder Remove(Type resourceClrType)
189+
{
190+
ArgumentNullException.ThrowIfNull(resourceClrType);
191+
192+
_resourceTypesByClrType.Remove(resourceClrType);
193+
return this;
194+
}
195+
170196
/// <summary>
171197
/// Adds a JSON:API resource.
172198
/// </summary>

test/JsonApiDotNetCoreTests/UnitTests/ResourceGraph/ResourceGraphBuilderTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,55 @@ 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 Can_remove_existing_resource_type()
168+
{
169+
// Arrange
170+
var options = new JsonApiOptions();
171+
var builder = new ResourceGraphBuilder(options, NullLoggerFactory.Instance);
172+
builder.Add<ResourceWithHasOneRelationship, int>();
173+
builder.Add<ResourceWithAttribute, int>();
174+
175+
// Act
176+
builder.Remove<ResourceWithHasOneRelationship>();
177+
178+
// Assert
179+
IResourceGraph resourceGraph = builder.Build();
180+
resourceGraph.GetResourceTypes().Should().ContainSingle().Which.ClrType.Should().Be<ResourceWithAttribute>();
181+
}
182+
183+
[Fact]
184+
public void Can_remove_missing_resource_type()
185+
{
186+
// Arrange
187+
var options = new JsonApiOptions();
188+
var builder = new ResourceGraphBuilder(options, NullLoggerFactory.Instance);
189+
builder.Add<ResourceWithAttribute, int>();
190+
191+
// Act
192+
builder.Remove<ResourceWithHasManyRelationship>();
193+
194+
// Assert
195+
IResourceGraph resourceGraph = builder.Build();
196+
resourceGraph.GetResourceTypes().Should().ContainSingle().Which.ClrType.Should().Be<ResourceWithAttribute>();
197+
}
198+
199+
[Fact]
200+
public void Can_remove_non_resource_type()
201+
{
202+
// Arrange
203+
var options = new JsonApiOptions();
204+
var builder = new ResourceGraphBuilder(options, NullLoggerFactory.Instance);
205+
builder.Add<ResourceWithAttribute, int>();
206+
207+
// Act
208+
builder.Remove(typeof(NonResource));
209+
210+
// Assert
211+
IResourceGraph resourceGraph = builder.Build();
212+
resourceGraph.GetResourceTypes().Should().ContainSingle().Which.ClrType.Should().Be<ResourceWithAttribute>();
213+
}
214+
166215
[Fact]
167216
public void Cannot_build_graph_with_missing_related_HasOne_resource()
168217
{

0 commit comments

Comments
 (0)