-
Notifications
You must be signed in to change notification settings - Fork 502
Add AWSLambda0145 diagnostic for unregistered durable envelope types #2464
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
Merged
GarrettBeatty
merged 1 commit into
dev
from
feature/durable-serializer-context-diagnostic
Jul 9, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
.autover/changes/durable-serializer-context-diagnostic.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "Projects": [ | ||
| { | ||
| "Name": "Amazon.Lambda.Annotations", | ||
| "Type": "Minor", | ||
| "ChangelogMessages": [ | ||
| "Add diagnostic AWSLambda0145 (Warning) for durable execution functions. When a [DurableExecution] function registers the source-generator serializer (SourceGeneratorLambdaJsonSerializer<TContext>), the durable invocation envelope types DurableExecutionInvocationInput and DurableExecutionInvocationOutput must be registered on the JsonSerializerContext with [JsonSerializable]. The generator now warns at build time when either is missing, instead of the function failing at invocation time. Preview." | ||
| ] | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
...a.Annotations.SourceGenerators.Tests/DurableExecutionSerializerContextDiagnosticsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Testing; | ||
| using Xunit; | ||
| using VerifyCS = Amazon.Lambda.Annotations.SourceGenerators.Tests.CSharpSourceGeneratorVerifier<Amazon.Lambda.Annotations.SourceGenerator.Generator>; | ||
|
|
||
| namespace Amazon.Lambda.Annotations.SourceGenerators.Tests | ||
| { | ||
| /// <summary> | ||
| /// Tests for AWSLambda0145: when a [DurableExecution] function registers the source-generator | ||
| /// serializer (SourceGeneratorLambdaJsonSerializer<TContext>), the durable invocation envelope | ||
| /// types must be registered on TContext with [JsonSerializable], otherwise serialization fails at | ||
| /// invocation time. | ||
| /// </summary> | ||
| public class DurableExecutionSerializerContextDiagnosticsTests | ||
| { | ||
| // Minimal durable SDK stubs. The real Amazon.Lambda.DurableExecution package cannot be referenced | ||
| // by this test project (its AWSSDK.Core 4.x conflicts with the 3.7.x pin required by the generator | ||
| // test framework), so the types the generator resolves by metadata name are supplied as source. | ||
| private const string DurableStubs = @" | ||
| namespace Amazon.Lambda.DurableExecution | ||
| { | ||
| public interface IDurableContext { } | ||
| public sealed class DurableExecutionInvocationInput { } | ||
| public sealed class DurableExecutionInvocationOutput { } | ||
| } | ||
| "; | ||
|
|
||
| private static async Task<string> AnnotationsSource(string fileName) => | ||
| await File.ReadAllTextAsync(Path.Combine("Amazon.Lambda.Annotations", fileName)); | ||
|
|
||
| // The user source registers the serializer itself (via [assembly: LambdaSerializer]) so the default | ||
| // DefaultLambdaJsonSerializer registration is intentionally not added here. | ||
| private static async Task<VerifyCS.Test> NewTestAsync(string userSource) | ||
| { | ||
| var test = new VerifyCS.Test | ||
| { | ||
| TestState = | ||
| { | ||
| OutputKind = OutputKind.ConsoleApplication, | ||
| Sources = | ||
| { | ||
| ("Workflow.cs", userSource), | ||
| ("DurableStubs.cs", DurableStubs), | ||
| }, | ||
| } | ||
| }; | ||
| test.TestState.Sources.Add((Path.Combine("Amazon.Lambda.Annotations", "LambdaFunctionAttribute.cs"), await AnnotationsSource("LambdaFunctionAttribute.cs"))); | ||
| test.TestState.Sources.Add((Path.Combine("Amazon.Lambda.Annotations", "DurableExecutionAttribute.cs"), await AnnotationsSource("DurableExecutionAttribute.cs"))); | ||
|
|
||
| // Generation proceeds (Warning severity does not halt it); ignore the per-file codegen Info | ||
| // (AWSLambda0103), the generated-source list, and compiler errors from the deliberately | ||
| // unimplemented JsonSerializerContext (only the generator's symbol resolution matters here). | ||
| test.DisabledDiagnostics.Add("AWSLambda0103"); | ||
| test.TestBehaviors |= TestBehaviors.SkipGeneratedSourcesCheck; | ||
| test.CompilerDiagnostics = CompilerDiagnostics.None; | ||
| return test; | ||
| } | ||
|
|
||
| // Builds a workflow that registers SourceGeneratorLambdaJsonSerializer<MyContext>, where MyContext | ||
| // registers whichever envelope types are passed in via extraJsonSerializable. | ||
| private static string WorkflowSource(string extraJsonSerializable) => $@" | ||
| using System.Threading.Tasks; | ||
| using System.Text.Json.Serialization; | ||
| using Amazon.Lambda.Annotations; | ||
| using Amazon.Lambda.Core; | ||
| using Amazon.Lambda.DurableExecution; | ||
| using Amazon.Lambda.Serialization.SystemTextJson; | ||
|
|
||
| [assembly: LambdaSerializer(typeof(SourceGeneratorLambdaJsonSerializer<MyApp.MyContext>))] | ||
|
|
||
| namespace MyApp | ||
| {{ | ||
| {extraJsonSerializable} | ||
| public partial class MyContext : JsonSerializerContext {{ }} | ||
|
|
||
| public class Workflows | ||
| {{ | ||
| [LambdaFunction] | ||
| [DurableExecution(executionTimeout: 300)] | ||
| public Task<string> Run(string input, IDurableContext ctx) => Task.FromResult(input); | ||
| }} | ||
| }}"; | ||
|
|
||
| [Fact] | ||
| public async Task BothEnvelopeTypesRegistered_NoDiagnostic() | ||
| { | ||
| var source = WorkflowSource( | ||
| " [JsonSerializable(typeof(DurableExecutionInvocationInput))]\n" + | ||
| " [JsonSerializable(typeof(DurableExecutionInvocationOutput))]"); | ||
| var test = await NewTestAsync(source); | ||
| // No AWSLambda0145 expected. | ||
| await test.RunAsync(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task OutputEnvelopeTypeMissing_ReportsWarning() | ||
| { | ||
| var source = WorkflowSource( | ||
| " [JsonSerializable(typeof(DurableExecutionInvocationInput))]"); | ||
| var test = await NewTestAsync(source); | ||
| test.TestState.ExpectedDiagnostics.Add( | ||
| new DiagnosticResult("AWSLambda0145", DiagnosticSeverity.Warning) | ||
| .WithSpan("Workflow.cs", 18, 9, 20, 94) | ||
| .WithArguments("MyApp.MyContext", "Amazon.Lambda.DurableExecution.DurableExecutionInvocationOutput")); | ||
| await test.RunAsync(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task BothEnvelopeTypesMissing_ReportsTwoWarnings() | ||
| { | ||
| var source = WorkflowSource(string.Empty); | ||
| var test = await NewTestAsync(source); | ||
| test.TestState.ExpectedDiagnostics.Add( | ||
| new DiagnosticResult("AWSLambda0145", DiagnosticSeverity.Warning) | ||
| .WithSpan("Workflow.cs", 18, 9, 20, 94) | ||
| .WithArguments("MyApp.MyContext", "Amazon.Lambda.DurableExecution.DurableExecutionInvocationInput")); | ||
| test.TestState.ExpectedDiagnostics.Add( | ||
| new DiagnosticResult("AWSLambda0145", DiagnosticSeverity.Warning) | ||
| .WithSpan("Workflow.cs", 18, 9, 20, 94) | ||
| .WithArguments("MyApp.MyContext", "Amazon.Lambda.DurableExecution.DurableExecutionInvocationOutput")); | ||
| await test.RunAsync(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DefaultSerializer_NoDiagnostic() | ||
| { | ||
| // The reflection-based DefaultLambdaJsonSerializer needs no [JsonSerializable] registration, | ||
| // so the durable envelope check does not apply and AWSLambda0145 is never emitted. | ||
| var source = @" | ||
| using System.Threading.Tasks; | ||
| using Amazon.Lambda.Annotations; | ||
| using Amazon.Lambda.Core; | ||
| using Amazon.Lambda.DurableExecution; | ||
|
|
||
| [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] | ||
|
|
||
| namespace MyApp | ||
| { | ||
| public class Workflows | ||
| { | ||
| [LambdaFunction] | ||
| [DurableExecution(executionTimeout: 300)] | ||
| public Task<string> Run(string input, IDurableContext ctx) => Task.FromResult(input); | ||
| } | ||
| }"; | ||
| var test = await NewTestAsync(source); | ||
| await test.RunAsync(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diagnostic message suggests code that may not compile without a using directive:
In LambdaFunctionValidator.cs:139, the {1} argument passed to the diagnostic is the full metadata name (Amazon.Lambda.DurableExecution.DurableExecutionInvocationInput). The
message format says:
▎ "add [JsonSerializable(typeof({1}))] to {0}"
If the user doesn't have using Amazon.Lambda.DurableExecution; in the file where their context is declared, copy-pasting this won't compile. The {0} argument (context name) uses ToDisplayString() which produces the natural C# name.
Suggestion: Use envelopeTypeSymbol.ToDisplayString() for {1} as well (which would produce the fully-qualified C# form like global::Amazon.Lambda.DurableExecution.DurableExecutionInvocationInput), or just use the short type name (envelopeTypeSymbol.Name) since users with [DurableExecution] will almost certainly have the using directive already.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right now it looks like
i dont really like this option
and envelopeTypeSymbol.Name would be
I think the current way is the best no? because if they copy and paste it, their IDE should theoretically auto import the using statement anyway