diff --git a/.autover/changes/5ead6e62-ef2d-4841-9d34-cc88924beba3.json b/.autover/changes/5ead6e62-ef2d-4841-9d34-cc88924beba3.json new file mode 100644 index 000000000..a89830601 --- /dev/null +++ b/.autover/changes/5ead6e62-ef2d-4841-9d34-cc88924beba3.json @@ -0,0 +1,11 @@ +{ + "Projects": [ + { + "Name": "Amazon.Lambda.Annotations", + "Type": "Patch", + "ChangelogMessages": [ + "Report a compile-time diagnostic (AWSLambda0146) when a [FromHeader] parameter uses a type other than a primitive .NET type or an enumerable of one, instead of generating code that fails at runtime" + ] + } + ] +} diff --git a/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md b/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md index ffb739ddf..b9007a01d 100644 --- a/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md +++ b/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md @@ -28,3 +28,4 @@ AWSLambda0142 | AWSLambdaCSharpGenerator | Error | Invalid DurableExecution meth AWSLambda0143 | AWSLambdaCSharpGenerator | Info | DurableExecution function with explicit Role needs checkpoint permissions AWSLambda0144 | AWSLambdaCSharpGenerator | Error | Invalid DurableExecutionAttribute AWSLambda0145 | AWSLambdaCSharpGenerator | Warning | Durable execution envelope types not registered with JsonSerializerContext +AWSLambda0146 | AWSLambdaCSharpGenerator | Error | Unsupported Header Parameter Type diff --git a/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs b/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs index cc40ce60a..d4bc67b45 100644 --- a/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs +++ b/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs @@ -330,5 +330,12 @@ public static class DiagnosticDescriptors category: "AWSLambdaCSharpGenerator", DiagnosticSeverity.Warning, isEnabledByDefault: true); + + public static readonly DiagnosticDescriptor UnsupportedHeaderParameterType = new DiagnosticDescriptor(id: "AWSLambda0146", + title: "Unsupported Header Parameter Type", + messageFormat: "Unsupported header parameter '{0}' of type '{1}' encountered. Only primitive .NET types and their corresponding enumerable can be used as header parameters.", + category: "AWSLambdaCSharpGenerator", + DiagnosticSeverity.Error, + isEnabledByDefault: true); } } diff --git a/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Validation/LambdaFunctionValidator.cs b/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Validation/LambdaFunctionValidator.cs index b580f2466..99912fc84 100644 --- a/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Validation/LambdaFunctionValidator.cs +++ b/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Validation/LambdaFunctionValidator.cs @@ -352,6 +352,15 @@ private static void ValidateApiGatewayEvents(LambdaFunctionModel lambdaFunctionM } } + // Validate [FromHeader] parameter types - only primitive types allowed + if (parameter.Attributes.Any(att => att.Type.FullName == TypeFullNames.FromHeaderAttribute)) + { + if (!parameter.Type.IsPrimitiveType() && !parameter.Type.IsPrimitiveEnumerableType()) + { + diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.UnsupportedHeaderParameterType, methodLocation, parameter.Name, parameter.Type.FullName)); + } + } + // Validate attribute names for FromQuery, FromRoute, and FromHeader foreach (var att in parameter.Attributes) { @@ -402,6 +411,15 @@ private static void ValidateApiGatewayEvents(LambdaFunctionModel lambdaFunctionM } } + // Validate [FromHeader] parameter types - only primitive types allowed + if (parameter.Attributes.Any(att => att.Type.FullName == TypeFullNames.FromHeaderAttribute)) + { + if (!parameter.Type.IsPrimitiveType() && !parameter.Type.IsPrimitiveEnumerableType()) + { + diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.UnsupportedHeaderParameterType, methodLocation, parameter.Name, parameter.Type.FullName)); + } + } + foreach (var att in parameter.Attributes) { var parameterAttributeName = string.Empty; @@ -512,6 +530,15 @@ private static void ValidateAlbEvents(LambdaFunctionModel lambdaFunctionModel, L } } + // Validate [FromHeader] parameter types - only primitive types allowed + if (parameter.Attributes.Any(att => att.Type.FullName == TypeFullNames.ALBFromHeaderAttribute)) + { + if (!parameter.Type.IsPrimitiveType() && !parameter.Type.IsPrimitiveEnumerableType()) + { + diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.UnsupportedHeaderParameterType, methodLocation, parameter.Name, parameter.Type.FullName)); + } + } + // Validate attribute names for FromQuery and FromHeader foreach (var att in parameter.Attributes) { diff --git a/Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs b/Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs index 009c6643c..b96a46b2d 100644 --- a/Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs +++ b/Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs @@ -1221,6 +1221,33 @@ public async Task ComplexQueryParameters_AreNotSupported() }.RunAsync(); } + [Fact] + public async Task ComplexHeaderParameters_AreNotSupported() + { + await new VerifyCS.Test + { + TestState = + { + Sources = + { + (Path.Combine("TestServerlessApp", "PlaceholderClass.cs"), await File.ReadAllTextAsync(Path.Combine("TestServerlessApp", "PlaceholderClass.cs"))), + (Path.Combine("TestServerlessApp", "ComplexHeaderParameter.cs"), await File.ReadAllTextAsync(Path.Combine("TestServerlessApp", "ComplexHeaderParameter.cs.error"))), + (Path.Combine("Amazon.Lambda.Annotations", "LambdaFunctionAttribute.cs"), await File.ReadAllTextAsync(Path.Combine("Amazon.Lambda.Annotations", "LambdaFunctionAttribute.cs"))), + (Path.Combine("Amazon.Lambda.Annotations", "LambdaStartupAttribute.cs"), await File.ReadAllTextAsync(Path.Combine("Amazon.Lambda.Annotations", "LambdaStartupAttribute.cs"))), + (Path.Combine("Amazon.Lambda.Annotations", "APIGateway", "RestApiAttribute.cs"), await File.ReadAllTextAsync(Path.Combine("Amazon.Lambda.Annotations", "APIGateway", "RestApiAttribute.cs"))), + (Path.Combine("Amazon.Lambda.Annotations", "APIGateway", "HttpApiAttribute.cs"), await File.ReadAllTextAsync(Path.Combine("Amazon.Lambda.Annotations", "APIGateway", "HttpApiAttribute.cs"))), + (Path.Combine("TestServerlessApp", "AssemblyAttributes.cs"), await File.ReadAllTextAsync(Path.Combine("TestServerlessApp", "AssemblyAttributes.cs"))), + }, + ExpectedDiagnostics = + { + DiagnosticResult.CompilerError("AWSLambda0146").WithSpan($"TestServerlessApp{Path.DirectorySeparatorChar}ComplexHeaderParameter.cs", 11, 9, 16, 10) + .WithMessage("Unsupported header parameter 'requestId' of type 'System.Guid' encountered. Only primitive .NET types and their corresponding enumerable can be used as header parameters.") + }, + } + + }.RunAsync(); + } + [Fact] public async Task InvalidParameterAttributeNames_ThrowsDiagnosticErrors() { diff --git a/Libraries/test/TestServerlessApp/ComplexHeaderParameter.cs.error b/Libraries/test/TestServerlessApp/ComplexHeaderParameter.cs.error new file mode 100644 index 000000000..b8189c044 --- /dev/null +++ b/Libraries/test/TestServerlessApp/ComplexHeaderParameter.cs.error @@ -0,0 +1,18 @@ +using Amazon.Lambda.Annotations; +using Amazon.Lambda.Annotations.APIGateway; +using Amazon.Lambda.Core; +using System; + +namespace TestServerlessApp +{ + public class ComplexHeaderParameter + { + // This fails because the header parameter 'requestId' is of type Guid, which is not a supported header parameter type. + [LambdaFunction(ResourceName = "TestHeaderParams", Policies = "AdministratorAccess")] + [RestApi(LambdaHttpMethod.Get, "/api/say-hello")] + public string SayHello([FromHeader] Guid requestId, ILambdaContext context) + { + return $"Hello! {requestId}"; + } + } +} diff --git a/Libraries/test/TestServerlessApp/TestServerlessApp.csproj b/Libraries/test/TestServerlessApp/TestServerlessApp.csproj index 4efe4a451..9415d6f88 100644 --- a/Libraries/test/TestServerlessApp/TestServerlessApp.csproj +++ b/Libraries/test/TestServerlessApp/TestServerlessApp.csproj @@ -15,6 +15,7 @@ +