diff --git a/examples/project/aggregated/Combined/Combined.csproj b/examples/project/aggregated/Combined/Combined.csproj index bb5addb..809c7d5 100644 --- a/examples/project/aggregated/Combined/Combined.csproj +++ b/examples/project/aggregated/Combined/Combined.csproj @@ -3,5 +3,6 @@ + diff --git a/examples/project/aggregated/D/D.csproj b/examples/project/aggregated/D/D.csproj new file mode 100644 index 0000000..6b512ec --- /dev/null +++ b/examples/project/aggregated/D/D.csproj @@ -0,0 +1 @@ + diff --git a/examples/project/aggregated/D/DTO.cs b/examples/project/aggregated/D/DTO.cs new file mode 100644 index 0000000..fb3da6b --- /dev/null +++ b/examples/project/aggregated/D/DTO.cs @@ -0,0 +1,3 @@ +namespace D; + +public class DTO; diff --git a/src/LeanCode.ContractsGenerator.Tests/ExampleBased/Project.cs b/src/LeanCode.ContractsGenerator.Tests/ExampleBased/Project.cs index 705fc6e..392cd2b 100644 --- a/src/LeanCode.ContractsGenerator.Tests/ExampleBased/Project.cs +++ b/src/LeanCode.ContractsGenerator.Tests/ExampleBased/Project.cs @@ -27,7 +27,8 @@ public void Aggregated_project_compiles() .WithTopic("C.Topic") .WithNotification( NotificationTypeRefExtensions.WithTag(TypeRefExtensions.Internal("C.Notification"), "C.Notification") - ); + ) + .WithDto("D.DTO"); } [Fact] @@ -36,14 +37,16 @@ public void Multiple_separate_projects_compile() ProjectsCompile( "project/aggregated/A/A.csproj", "project/aggregated/B/B.csproj", - "project/aggregated/C/C.csproj" + "project/aggregated/C/C.csproj", + "project/aggregated/D/D.csproj" ) .WithCommand("A.Command") .WithQuery("B.Query") .WithTopic("C.Topic") .WithNotification( NotificationTypeRefExtensions.WithTag(TypeRefExtensions.Internal("C.Notification"), "C.Notification") - ); + ) + .WithDto("D.DTO"); } [Fact] @@ -62,6 +65,7 @@ public void Multiple_separate_projects_compile_even_if_some_references_overlap() "project/aggregated/A/A.csproj", "project/aggregated/B/B.csproj", "project/aggregated/C/C.csproj", + "project/aggregated/D/D.csproj", "project/aggregated/Combined/Combined.csproj" ) .WithCommand("A.Command") @@ -69,20 +73,23 @@ public void Multiple_separate_projects_compile_even_if_some_references_overlap() .WithTopic("C.Topic") .WithNotification( NotificationTypeRefExtensions.WithTag(TypeRefExtensions.Internal("C.Notification"), "C.Notification") - ); + ) + .WithDto("D.DTO"); ProjectsCompile( "project/aggregated/Combined/Combined.csproj", "project/aggregated/A/A.csproj", "project/aggregated/B/B.csproj", - "project/aggregated/C/C.csproj" + "project/aggregated/C/C.csproj", + "project/aggregated/D/D.csproj" ) .WithCommand("A.Command") .WithQuery("B.Query") .WithTopic("C.Topic") .WithNotification( NotificationTypeRefExtensions.WithTag(TypeRefExtensions.Internal("C.Notification"), "C.Notification") - ); + ) + .WithDto("D.DTO"); } [Fact] diff --git a/src/LeanCode.ContractsGenerator/Compilation/ContractTypes.cs b/src/LeanCode.ContractsGenerator/Compilation/ContractTypes.cs index da88829..06d7754 100644 --- a/src/LeanCode.ContractsGenerator/Compilation/ContractTypes.cs +++ b/src/LeanCode.ContractsGenerator/Compilation/ContractTypes.cs @@ -55,19 +55,10 @@ public ContractTypes(IReadOnlyCollection compilations) private static HashSet GetTypeSymbols(IReadOnlyCollection compilations) { var name = typeof(T).FullName!; - var result = new HashSet(SymbolEqualityComparer.Default); - foreach (var c in compilations) - { - var type = - c.GetTypeByMetadataName(name) - ?? throw new CompilationFailedException( - $"Cannot locate type {name} in compilation unit `{c.AssemblyName ?? "UNKNOWN"}`." - ); - result.Add(type); - } - - return result; + return compilations + .SelectMany(c => c.GetTypesByMetadataName(name)) + .ToHashSet(SymbolEqualityComparer.Default); } private static HashSet GetUnboundTypeSymbols( @@ -76,19 +67,11 @@ Type type ) { var name = type.FullName!; - var result = new HashSet(SymbolEqualityComparer.Default); - - foreach (var c in compilations) - { - var t = - (c.GetTypeByMetadataName(name)?.ConstructUnboundGenericType()) - ?? throw new CompilationFailedException( - $"Cannot locate generic type {name} in compilation unit `{c.AssemblyName ?? "UNKNOWN"}`." - ); - result.Add(t); - } - return result; + return compilations + .SelectMany(c => c.GetTypesByMetadataName(name)) + .Select(nts => nts.ConstructUnboundGenericType()) + .ToHashSet(SymbolEqualityComparer.Default); } public bool IsQuery(ITypeSymbol symbol) =>