Skip to content
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

Analyzer rule NSB0006 gives false positive when mapping is done with base interface #6714

Closed
oskar opened this issue Mar 30, 2023 · 5 comments
Labels
Milestone

Comments

@oskar
Copy link

oskar commented Mar 30, 2023

Describe the bug

Description

Consider the following saga and message definitions:

public class ReproSaga : Saga<SagaData>,
    IAmStartedByMessages<IFirstMessage>,
    IHandleMessages<ISecondMessage>
{
    protected override void ConfigureHowToFindSaga(SagaPropertyMapper<SagaData> mapper)
    {
        mapper.MapSaga(saga => saga.Correlation)
            .ToMessage<IMessageBase>(msg => msg.Correlation);
    }

    public Task Handle(IFirstMessage message, IMessageHandlerContext context) => Task.CompletedTask;

    public Task Handle(ISecondMessage message, IMessageHandlerContext context) => Task.CompletedTask;
}

public class SagaData : ContainSagaData
{
    public string Correlation { get; set; }
}

public interface IMessageBase
{
    string Correlation { get; set; }
}

public interface IFirstMessage : IMessageBase { }

public interface ISecondMessage : IMessageBase { }

Expected behavior

Since all handled message types are mapped through the base interface, this should be a perfectly fine saga definition. With NSB 7, sagas like this was not a problem (we have them running in production).

Actual behavior

When upgrading to NSB 8, the analyzer starts to throw error NSB0006 (which I believe is a false positive):

ReproSaga.cs(7, 5): [NSB0006] Saga ReproSaga implements IAmStartedByMessages<IFirstMessage> but does not provide a mapping for that message type. In the ConfigureHowToFindSaga method, after calling mapper.MapSaga(saga => saga.CorrelationPropertyName), add .ToMessage<IFirstMessage>(msg => msg.PropertyName) to map a message property to the saga correlation ID, or .ToMessageHeader<IFirstMessage>("HeaderName") to map a header value that will contain the correlation ID.

Versions

NServiceBus 8.0.3

Steps to reproduce

  1. Reference NServiceBus 8
  2. Take the code block above and build
  3. Analyzer generates error

Relevant log output

No response

Additional Information

Workarounds

Using explicit mapping of all message types seems to satisfy the analyzer.

Possible solutions

Additional information

Since the analyzer rule is an error and not a warning, we cannot suppress it with for example <NoWarn>.

@oskar oskar added the Bug label Mar 30, 2023
@DavidBoike
Copy link
Member

Hi @oskar, yep this is probably a bug in the analyzer from an unconsidered edge case.

However you should be able to disable or downgrade the analyzer in one of the following ways:

Using a pragma in the source:

#pragma warning disable NSB0006
// Saga code
#pragma warning restore NSB0006

Or using an .editorconfig file:

[*.cs]
dotnet_diagnostic.NSB0006.severity = suggestion

or

dotnet_diagnostic.NSB0006.severity = none

Can you confirm that one of these will work for you for the time being?

@oskar
Copy link
Author

oskar commented Mar 31, 2023

You're absolutely right. I was convinced that only warnings could be suppressed, not errors - but apparently not. Both #pragma warning disable in code and <NoWarn> in csproj works fine. I didn't try modifying the .editorconfig file but that probably also works.

Thanks for the workaround 🙏

@DavidBoike
Copy link
Member

@oskar Fixed this in #6733 and it will be released with NServiceBus 8.1.0.

As it was judged not to be a critical bug, especially given the workaround documented here, it won't be backported to previous releases.

@oskar
Copy link
Author

oskar commented May 10, 2023

Great news! I agree it's not critical.

@DavidBoike
Copy link
Member

Closing this as we are now actively working on releasing 8.1.0 soon.

@DavidBoike DavidBoike changed the title Saga analyzer rule NSB0006 gives false positive when mapping is done with base interface Analyzer rule NSB0006 gives false positive when mapping is done with base interface Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants