Skip to content

Commit 8f28a2f

Browse files
committed
feat: #537 emit error for nested data sources whose type doesn't match their containing class
1 parent c153f38 commit 8f28a2f

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

playground/Coalesce.Domain/Person.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ public override IQueryable<Person> GetQuery(IDataSourceParameters parameters)
353353
//=> Db.People.Include(p => p.Company);
354354
=> Db.People.IncludeChildren();
355355

356-
// https://stackoverflow.com/questions/11276964/how-to-slow-down-a-sql-query
357-
//=> Db.People.FromSql("SELECT top 1000000 T1.* FROM Person T1,Person T2, Person T3, Person T4 ORDER BY RAND(T1.PersonId)");
356+
// https://stackoverflow.com/questions/11276964/how-to-slow-down-a-sql-query
357+
//=> Db.People.FromSql("SELECT top 1000000 T1.* FROM Person T1,Person T2, Person T3, Person T4 ORDER BY RAND(T1.PersonId)");
358358
}
359359

360360
public class Behaviors : StandardBehaviors<Person, AppDbContext>

src/IntelliTect.Coalesce.DotnetTool/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using IntelliTect.Coalesce.CodeGeneration.Configuration;
1212
using IntelliTect.Coalesce.CodeGeneration.Generation;
1313
using IntelliTect.Coalesce.CodeGeneration.Utilities;
14+
using IntelliTect.Coalesce.TypeDefinition;
1415
using McMaster.Extensions.CommandLineUtils;
1516
using Microsoft.Extensions.Logging;
1617
using Newtonsoft.Json;
@@ -115,6 +116,12 @@ private async Task<int> OnExecuteAsync(CommandLineApplication app)
115116
{
116117
await executor.GenerateAsync(rootGenerator);
117118
}
119+
catch (CoalesceModelException e)
120+
{
121+
// Only write the message here, not a full exception.ToString() w/ stack trace
122+
executor.Logger.LogError(e.Message);
123+
return -1;
124+
}
118125
catch (ProjectAnalysisException e)
119126
{
120127
// Only write the message here, not a full exception.ToString() w/ stack trace
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IntelliTect.Coalesce.TypeDefinition
8+
{
9+
public sealed class CoalesceModelException : Exception
10+
{
11+
public CoalesceModelException() : base()
12+
{
13+
}
14+
15+
public CoalesceModelException(string? message) : base(message)
16+
{
17+
}
18+
19+
public CoalesceModelException(string? message, Exception? innerException) : base(message, innerException)
20+
{
21+
}
22+
}
23+
}

src/IntelliTect.Coalesce/TypeDefinition/PropertyViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public PropertyViewModel? ManyToManyFarNavigationProperty
165165
string? propName = this.GetAttributeValue<ManyToManyAttribute>(a => a.FarNavigationProperty);
166166
if (propName != null && propName == ManyToManyNearNavigationProperty?.Name)
167167
{
168-
throw new Exception(
168+
throw new CoalesceModelException(
169169
"ManyToManyAtttribute.FarNavigationProperty is referencing the near side of " +
170170
"the many-to-many relationship, which is not allowed. " +
171171
"To configure the near side of the many-to-many relationship, use [InverseProperty].");
@@ -182,7 +182,7 @@ propName is null
182182
{
183183
if (firstCandidate != null)
184184
{
185-
throw new Exception(
185+
throw new CoalesceModelException(
186186
"Found more than one candidate far navigation property for [ManyToMany] collection. To fix this, set ManyToManyAtttribute.FarNavigationProperty to the name of one of these candidate properties: " +
187187
string.Concat(candidates.Select(p => $"\n {Object}.{p.Name}")));
188188
}

src/IntelliTect.Coalesce/TypeDefinition/ReflectionRepository.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,15 @@ private bool AddCrudStrategy(
331331
var servedClass = servedType.ClassViewModel;
332332
if (servedClass == null)
333333
{
334-
throw new InvalidOperationException($"{servedType} is not a valid type argument for a {iface}.");
334+
throw new CoalesceModelException($"{servedType} is not a valid type argument for a {iface}.");
335335
}
336336

337337
// See if we were expecting that the strategy be declared for a particular type
338338
// by virtue of its nesting. If this type has been overridden to something else by an attribute, then that's wrong.
339339
var explicitlyDeclaredFor = strategyType.GetAttributeValue<DeclaredForAttribute>(a => a.DeclaredFor)?.ClassViewModel;
340340
if (explicitlyDeclaredFor != null && declaredFor != null && !explicitlyDeclaredFor.Equals(declaredFor))
341341
{
342-
throw new InvalidOperationException(
342+
throw new CoalesceModelException(
343343
$"Expected that {strategyType} is declared for {declaredFor}, but it was explicitly declared for {explicitlyDeclaredFor} instead.");
344344
}
345345

@@ -350,10 +350,16 @@ private bool AddCrudStrategy(
350350

351351
if (declaredFor.IsCustomDto && !servedClass.Equals(declaredFor.DtoBaseViewModel))
352352
{
353-
throw new InvalidOperationException($"{strategyType} is not a valid {iface} for {declaredFor} - " +
353+
throw new CoalesceModelException($"{strategyType} is not a valid {iface.Name} for {declaredFor}. " +
354354
$"{strategyType} must satisfy {iface} with type parameter <{declaredFor.DtoBaseViewModel}>.");
355355
}
356356

357+
if (!declaredFor.IsCustomDto && !servedClass.Equals(declaredFor))
358+
{
359+
throw new CoalesceModelException($"{strategyType} is not a valid {iface.Name} for {declaredFor}. " +
360+
$"{strategyType} must satisfy {iface} with type parameter <{declaredFor}>.");
361+
}
362+
357363
set.Add(new CrudStrategyTypeUsage(strategyType.ClassViewModel, servedClass, declaredFor));
358364

359365
if (strategyType.IsA(typeof(IDataSource<>)))

0 commit comments

Comments
 (0)