Skip to content

Fixes auto-discovery when using multiple assemblies #997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ public void RegisterAssembly(Assembly assembly)
}
}

public IEnumerable<(Assembly assembly, IReadOnlyCollection<ResourceDescriptor> resourceDescriptors)> GetResourceDescriptorsPerAssembly()
public IReadOnlyCollection<ResourceDescriptor> GetResourceDescriptors()
{
EnsureAssembliesScanned();

return _resourceDescriptorsPerAssembly.Select(pair => (pair.Key, pair.Value)).ToArray();
return _resourceDescriptorsPerAssembly.SelectMany(pair => pair.Value).ToArray();
}

public IReadOnlyCollection<Assembly> GetAssemblies()
{
EnsureAssembliesScanned();

return _resourceDescriptorsPerAssembly.Keys;
}

private void EnsureAssembliesScanned()
Expand Down
9 changes: 6 additions & 3 deletions src/JsonApiDotNetCore/Configuration/ServiceDiscoveryFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,21 @@ public ServiceDiscoveryFacade AddAssembly(Assembly assembly)

internal void DiscoverResources()
{
foreach (ResourceDescriptor resourceDescriptor in _assemblyCache.GetResourceDescriptorsPerAssembly().SelectMany(tuple => tuple.resourceDescriptors))
foreach (ResourceDescriptor resourceDescriptor in _assemblyCache.GetResourceDescriptors())
{
AddResource(resourceDescriptor);
}
}

internal void DiscoverInjectables()
{
foreach ((Assembly assembly, IReadOnlyCollection<ResourceDescriptor> resourceDescriptors) in _assemblyCache.GetResourceDescriptorsPerAssembly())
IReadOnlyCollection<ResourceDescriptor> descriptors = _assemblyCache.GetResourceDescriptors();
IReadOnlyCollection<Assembly> assemblies = _assemblyCache.GetAssemblies();

foreach (Assembly assembly in assemblies)
{
AddDbContextResolvers(assembly);
AddInjectables(resourceDescriptors, assembly);
AddInjectables(descriptors, assembly);
}
}

Expand Down
18 changes: 7 additions & 11 deletions test/UnitTests/Graph/ResourceDescriptorAssemblyCacheTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FluentAssertions;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Resources;
using Xunit;
Expand All @@ -20,12 +20,11 @@ public void GetResourceDescriptorsPerAssembly_Locates_Identifiable_Resource()
assemblyCache.RegisterAssembly(resourceType.Assembly);

// Act
IEnumerable<(Assembly assembly, IReadOnlyCollection<ResourceDescriptor> resourceDescriptors)> results =
assemblyCache.GetResourceDescriptorsPerAssembly();
IReadOnlyCollection<ResourceDescriptor> descriptors = assemblyCache.GetResourceDescriptors();

// Assert
Assert.Contains(results,
result => result.resourceDescriptors != null && result.resourceDescriptors.Any(descriptor => descriptor.ResourceType == resourceType));
descriptors.Should().NotBeEmpty();
descriptors.Should().ContainSingle(descriptor => descriptor.ResourceType == resourceType);
}

[Fact]
Expand All @@ -38,14 +37,11 @@ public void GetResourceDescriptorsPerAssembly_Only_Contains_IIdentifiable_Types(
assemblyCache.RegisterAssembly(resourceType.Assembly);

// Act
IEnumerable<(Assembly assembly, IReadOnlyCollection<ResourceDescriptor> resourceDescriptors)> results =
assemblyCache.GetResourceDescriptorsPerAssembly();
IReadOnlyCollection<ResourceDescriptor> descriptors = assemblyCache.GetResourceDescriptors();

// Assert
foreach (ResourceDescriptor resourceDescriptor in results.SelectMany(result => result.resourceDescriptors))
{
Assert.True(typeof(IIdentifiable).IsAssignableFrom(resourceDescriptor.ResourceType));
}
descriptors.Should().NotBeEmpty();
descriptors.Select(descriptor => descriptor.ResourceType).Should().AllBeAssignableTo<IIdentifiable>();
}
}
}