Skip to content
Draft
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 @@ -14,7 +14,7 @@
/// <summary>
/// Analyzer that checks for mismatches between the input and output types of Activities invocations and their definitions.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]

Check warning on line 17 in src/Analyzers/Activities/MatchingInputOutputTypeActivityAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)
public class MatchingInputOutputTypeActivityAnalyzer : DiagnosticAnalyzer
{
/// <summary>
Expand Down Expand Up @@ -75,6 +75,9 @@
IMethodSymbol taskActivityRunAsync = knownSymbols.TaskActivityBase.GetMembers("RunAsync").OfType<IMethodSymbol>().Single();
INamedTypeSymbol voidSymbol = context.Compilation.GetSpecialType(SpecialType.System_Void);

// Get common DI types that should not be treated as activity input
INamedTypeSymbol? functionContextSymbol = context.Compilation.GetTypeByMetadataName("Microsoft.Azure.Functions.Worker.FunctionContext");

// Search for Activity invocations
ConcurrentBag<ActivityInvocation> invocations = [];
context.RegisterOperationAction(
Expand Down Expand Up @@ -161,6 +164,12 @@
return;
}

// If the parameter is FunctionContext, skip validation for this activity (it's a DI parameter, not real input)
if (functionContextSymbol != null && SymbolEqualityComparer.Default.Equals(inputParam.Type, functionContextSymbol))
{
return;
}

ITypeSymbol? inputType = inputParam.Type;

ITypeSymbol? outputType = methodSymbol.ReturnType;
Expand Down Expand Up @@ -306,7 +315,8 @@
continue;
}

if (!SymbolEqualityComparer.Default.Equals(invocation.InputType, activity.InputType))
// Check input type compatibility
if (!AreTypesCompatible(ctx.Compilation, invocation.InputType, activity.InputType))
{
string actual = invocation.InputType?.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat) ?? "none";
string expected = activity.InputType?.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat) ?? "none";
Expand All @@ -316,7 +326,8 @@
ctx.ReportDiagnostic(diagnostic);
}

if (!SymbolEqualityComparer.Default.Equals(invocation.OutputType, activity.OutputType))
// Check output type compatibility
if (!AreTypesCompatible(ctx.Compilation, activity.OutputType, invocation.OutputType))
{
string actual = invocation.OutputType?.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat) ?? "none";
string expected = activity.OutputType?.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat) ?? "none";
Expand All @@ -330,6 +341,48 @@
});
}

/// <summary>
/// Checks if the source type is compatible with (can be assigned to) the target type.
/// This handles polymorphism, interface implementation, inheritance, and collection type compatibility.
/// </summary>
static bool AreTypesCompatible(Compilation compilation, ITypeSymbol? sourceType, ITypeSymbol? targetType)
{
// Both null = compatible (no input/output on both sides)
if (sourceType == null && targetType == null)
{
return true;
}

// Special case: null (no input/output provided) can be passed to explicitly nullable parameters
// This handles nullable value types (int?) and nullable reference types (string?)
if (sourceType == null && targetType != null)
{
// Check if target is a nullable value type (Nullable<T>)
if (targetType.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
{
return true;
}

// Check if target is a nullable reference type (string?)
if (targetType.NullableAnnotation == NullableAnnotation.Annotated)
{
return true;
}

// Not nullable, so null input is incompatible
return false;
}

// If targetType is null but sourceType is not, they're incompatible
if (targetType == null && sourceType != null)
{
return false;
}

var conversion = compilation.ClassifyConversion(sourceType!, targetType!);
return conversion.IsImplicit || conversion.IsIdentity;
}

struct ActivityInvocation
{
public string Name { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,147 @@ async Task Method(TaskOrchestrationContext context)
await VerifyCS.VerifyDurableTaskAnalyzerAsync(code);
}

[Fact]
// Verifies that when FunctionContext is marked with [ActivityTrigger],
// calling the activity without input produces NO DURABLE2001/DURABLE2002 warnings.
// This fixes the issue where FunctionContext was incorrectly treated as required input.
public async Task DurableFunctionActivityWithFunctionContextAsActivityTrigger_CalledWithoutInput()
{
string code = Wrapper.WrapDurableFunctionOrchestration(@"
async Task Method(TaskOrchestrationContext context)
{
int num = await context.CallActivityAsync<int>(nameof(GetNumber));
}

[Function(nameof(GetNumber))]
int GetNumber([ActivityTrigger] FunctionContext context)
{
return 42;
}
");

await VerifyCS.VerifyDurableTaskAnalyzerAsync(code);
}

[Fact]
// Verifies that when FunctionContext is marked with [ActivityTrigger],
// calling the activity WITH input also produces NO DURABLE2001/DURABLE2002 warnings.
// The analyzer completely skips validation for FunctionContext activities.
public async Task DurableFunctionActivityWithFunctionContextAsActivityTrigger_CalledWithInput()
{
string code = Wrapper.WrapDurableFunctionOrchestration(@"
async Task Method(TaskOrchestrationContext context)
{
int num = await context.CallActivityAsync<int>(nameof(GetNumber), ""someInput"");
}

[Function(nameof(GetNumber))]
int GetNumber([ActivityTrigger] FunctionContext context)
{
return 42;
}
");

await VerifyCS.VerifyDurableTaskAnalyzerAsync(code);
}

[Fact]
// Verifies that polymorphism is supported for input types - NO WARNINGS expected.
// A derived type (Exception) should be assignable to a base type (object).
// This tests that the analyzer uses type compatibility rather than exact type matching.
public async Task DurableFunctionActivityWithPolymorphicInput_DerivedToBase()
{
string code = Wrapper.WrapDurableFunctionOrchestration(@"
async Task Method(TaskOrchestrationContext context)
{
Exception ex = new Exception(""error"");
await context.CallActivityAsync(nameof(LogError), ex);
}

[Function(nameof(LogError))]
void LogError([ActivityTrigger] object error)
{
}
");

await VerifyCS.VerifyDurableTaskAnalyzerAsync(code);
}

[Fact]
// Verifies that collection type compatibility works: List<T> → IReadOnlyList<T> - NO WARNINGS expected.
// This tests the exact scenario from the issue: passing List<int> to an activity expecting IReadOnlyList<int>.
// Uses TaskActivity<> pattern since it works better with generic collection types.
public async Task TaskActivityWithCollectionPolymorphism_ListToIReadOnlyList()
{
string code = Wrapper.WrapTaskOrchestrator(@"
using System.Collections.Generic;

public class Caller {
async Task Method(TaskOrchestrationContext context)
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
int sum = await context.CallActivityAsync<int>(nameof(SumActivity), numbers);
}
}

public class SumActivity : TaskActivity<IReadOnlyList<int>, int>
{
public override Task<int> RunAsync(TaskActivityContext context, IReadOnlyList<int> numbers)
{
return Task.FromResult(42);
}
}
");

await VerifyCS.VerifyDurableTaskAnalyzerAsync(code);
}

[Fact]
// Verifies that polymorphism is supported for output types - NO WARNINGS expected.
// When an activity returns string but the caller expects object, no warning should occur.
// This tests covariance - a more specific return type is acceptable.
public async Task DurableFunctionActivityWithPolymorphicOutput_StringToObject()
{
string code = Wrapper.WrapDurableFunctionOrchestration(@"
async Task Method(TaskOrchestrationContext context)
{
object result = await context.CallActivityAsync<object>(nameof(GetValue), ""input"");
}

[Function(nameof(GetValue))]
string GetValue([ActivityTrigger] string input)
{
return ""hello"";
}
");

await VerifyCS.VerifyDurableTaskAnalyzerAsync(code);
}

[Fact]
// Verifies that truly incompatible types still produce DURABLE2001 warnings.
// Passing string when int is expected should fail - this is not a valid conversion.
// This test ensures the analyzer still catches real type mismatches.
public async Task DurableFunctionActivityWithIncompatibleTypes_ShouldFail()
{
string code = Wrapper.WrapDurableFunctionOrchestration(@"
async Task Method(TaskOrchestrationContext context)
{
await {|#0:context.CallActivityAsync<int>(nameof(GetNumber), ""text"")|};
}

[Function(nameof(GetNumber))]
int GetNumber([ActivityTrigger] int value)
{
return value;
}
");

DiagnosticResult expected = BuildInputDiagnostic().WithLocation(0).WithArguments("string", "int", "GetNumber");

await VerifyCS.VerifyDurableTaskAnalyzerAsync(code, expected);
}


static DiagnosticResult BuildInputDiagnostic()
{
Expand Down
Loading