Skip to content
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 @@ -35,7 +35,7 @@ public class Msg1 : ICommand
public string CorrId { get; set; }
}";

return Assert([DiagnosticIds.SagaMappingExpressionCanBeRewritten], source, mustCompile: false);
return Assert([DiagnosticIds.SagaMappingExpressionCanBeSimplified], source, mustCompile: false);
}

[Test]
Expand Down Expand Up @@ -143,6 +143,40 @@ public class Msg1 : ICommand;
return Assert(source);
}

[Test]
public Task NoDiagnosticForConfigureNotFoundHandler()
{
var source =
@"using System;
using System.Threading.Tasks;
using NServiceBus;
public class MySaga : Saga<MyData>, IAmStartedByMessages<Msg1>
{
protected override void ConfigureHowToFindSaga(SagaPropertyMapper<MyData> mapper)
{
mapper.MapSaga(saga => saga.CorrId).ToMessage<Msg1>(msg => msg.CorrId);

mapper.ConfigureNotFoundHandler<MyNotFoundHandler>();
}
public Task Handle(Msg1 message, IMessageHandlerContext context) => throw new NotImplementedException();
}
public class MyData : ContainSagaData
{
public string CorrId { get; set; }
public string OtherId { get; set; }
}
public class Msg1 : ICommand
{
public string CorrId { get; set; }
}
public class MyNotFoundHandler : ISagaNotFoundHandler
{
public Task Handle(object message, IMessageProcessingContext context) => Task.CompletedTask;
}";

return Assert([], source, mustCompile: false);
}

[Test]
public Task SagaDataPropertyHasNonPublicSetter()
{
Expand Down
2 changes: 1 addition & 1 deletion src/NServiceBus.Core.Analyzer/DiagnosticIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class DiagnosticIds
public const string SagaShouldNotImplementNotFoundHandler = "NSB0015";
public const string CorrelationPropertyTypeMustMatchMessageMappingExpressions = "NSB0016";
public const string ToSagaMappingMustBeToAProperty = "NSB0017";
public const string SagaMappingExpressionCanBeRewritten = "NSB0018";
public const string SagaMappingExpressionCanBeRewritten = "NSB0018"; // Unused, merged into NSB0004
public const string DoNotEnableFeaturesInDefaults = "NSB0019";
public const string HandlerInjectsMessageSession = "NSB0020";
public const string AddHandlerOnSagaType = "NSB0021";
Expand Down
16 changes: 2 additions & 14 deletions src/NServiceBus.Core.Analyzer/Sagas/SagaAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public class SagaAnalyzer : DiagnosticAnalyzer
SagaDiagnostics.SagaShouldNotHaveIntermediateBaseClass,
SagaDiagnostics.SagaShouldNotImplementNotFoundHandler,
SagaDiagnostics.ToSagaMappingMustBeToAProperty,
SagaDiagnostics.CorrelationPropertyTypeMustMatchMessageMappingExpressions,
SagaDiagnostics.SagaMappingExpressionCanBeRewritten
SagaDiagnostics.CorrelationPropertyTypeMustMatchMessageMappingExpressions
];

public override void Initialize(AnalysisContext context)
Expand Down Expand Up @@ -153,25 +152,14 @@ static void AnalyzeSagaClass(SyntaxNodeAnalysisContext context, ClassDeclaration
}
}
}
else if (nonCustomFinderMapping.Select(m => m.ToSagaSyntax).Distinct().Count() > 1)
{
Diagnostic diagnostic = CreateMappingRewritingDiagnostic(
fixerTitle: "Simplify saga mapping expression",
descriptor: SagaDiagnostics.SagaMappingExpressionCanBeSimplified,
location: saga.ConfigureHowToFindMethod.Identifier.GetLocation(),
newMappingForLocation: null,
correlationId: assumedCorrelationId,
saga: saga);
context.ReportDiagnostic(diagnostic);
}
else
{
var mappingMethodNames = nonCustomFinderMapping.Select(m => (m.MessageTypeSyntax?.Parent?.Parent as GenericNameSyntax)?.Identifier.ValueText);
if (mappingMethodNames.Any(name => name is "ConfigureMapping" or "ConfigureHeaderMapping"))
{
Diagnostic diagnostic = CreateMappingRewritingDiagnostic(
fixerTitle: "Rewrite saga mapping expression",
descriptor: SagaDiagnostics.SagaMappingExpressionCanBeRewritten,
descriptor: SagaDiagnostics.SagaMappingExpressionCanBeSimplified,
location: saga.ConfigureHowToFindMethod.Identifier.GetLocation(),
newMappingForLocation: null,
correlationId: assumedCorrelationId,
Expand Down
10 changes: 1 addition & 9 deletions src/NServiceBus.Core.Analyzer/Sagas/SagaDiagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static class SagaDiagnostics
public static readonly DiagnosticDescriptor SagaMappingExpressionCanBeSimplified = new(
id: DiagnosticIds.SagaMappingExpressionCanBeSimplified,
title: "Saga mapping expressions must be rewritten",
messageFormat: "The saga mapping contains multiple .ToSaga(…) expressions which must be rewritten using mapper.MapSaga(…).ToMessage<T>(…) syntax. Use the code fix to transition to the new syntax.",
messageFormat: "This saga mapping expression must be rewritten using mapper.MapSaga(…).ToMessage<T>(…) syntax. Use the code fix to transition to the new syntax.",
category: DiagnosticCategory,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
Expand Down Expand Up @@ -129,13 +129,5 @@ static class SagaDiagnostics
category: DiagnosticCategory,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

public static readonly DiagnosticDescriptor SagaMappingExpressionCanBeRewritten = new(
id: DiagnosticIds.SagaMappingExpressionCanBeRewritten,
title: "Saga mapping expressions must be rewritten",
messageFormat: "This saga mapping expression must be rewritten using mapper.MapSaga(…).ToMessage<T>(…) syntax. Use the code fix to transition to the new syntax.",
category: DiagnosticCategory,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
}
}