Add AWSLambda0145 diagnostic for unregistered durable envelope types#2464
Add AWSLambda0145 diagnostic for unregistered durable envelope types#2464GarrettBeatty wants to merge 1 commit into
Conversation
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]. Unlike the reflection-based DefaultLambdaJsonSerializer, the context only serializes types explicitly registered, so a missing registration fails only at invocation time. The generator now emits AWSLambda0145 (Warning) at build time when either envelope type is missing.
|
created a test program this shows diagnostic. then when we uncomment it goes away |
| category: "AWSLambdaCSharpGenerator", | ||
| DiagnosticSeverity.Error, | ||
| isEnabledByDefault: true); | ||
|
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
right now it looks like
[JsonSerializable(typeof(Amazon.Lambda.DurableExecution.DurableExecutionInvocationInput))]
i dont really like this option
[JsonSerializable(typeof(global::Amazon.Lambda.DurableExecution.DurableExecutionInvocationInput))]
and envelopeTypeSymbol.Name would be
[JsonSerializable(typeof(DurableExecutionInvocationInput))]
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
Description
Adds a new build-time diagnostic AWSLambda0145 (Warning) to the Annotations source generator.
Motivation
With the Annotations model, a
[DurableExecution]function's generated handler has the signature:Users never see
DurableExecutionInvocationInput/DurableExecutionInvocationOutputin their own code — the generator introduces them. If the function registers the source-generator serializer (SourceGeneratorLambdaJsonSerializer<TContext>), the durable runtime (de)serializes that envelope through the user'sJsonSerializerContext. Unlike the reflection-basedDefaultLambdaJsonSerializer, a source-gen context only handles types explicitly registered via[JsonSerializable]. If the envelope types aren't registered, the function fails at invocation time with no build-time signal.This diagnostic surfaces the problem at build time, consistent with the other durable diagnostics (AWSLambda0142–0144).
Behavior
Emitted (Warning) only when all hold:
[DurableExecution], andSourceGeneratorLambdaJsonSerializer<TContext>, andTContext(walking its base contexts) does not register a given envelope type via[JsonSerializable(typeof(...))].One warning per missing envelope type. Silent for
DefaultLambdaJsonSerializer/ any non-source-gen serializer, and skipped if the durable types can't be resolved (package not referenced — other diagnostics cover that).Changes
TypeFullNames.cs: addSourceGeneratorLambdaJsonSerializer1andJsonSerializableAttribute` constants.DiagnosticDescriptors.cs: addDurableExecutionMissingSerializableEnvelope(AWSLambda0145, Warning).AnalyzerReleases.Unshipped.md: register the new rule.LambdaFunctionValidator.cs: addValidateDurableExecutionSerializerContext, called fromValidateDurableExecution.DurableExecutionSerializerContextDiagnosticsTests— both registered (no diagnostic), one missing, both missing, and DefaultLambdaJsonSerializer (no diagnostic).Amazon.Lambda.Annotations, Minor).Testing
dotnet build -c Releaseon the generator: clean.DurableExecution*generator tests pass (net10.0), including the 4 new ones.