Skip to content
Merged
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
11 changes: 11 additions & 0 deletions .autover/changes/5ead6e62-ef2d-4841-9d34-cc88924beba3.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<Content Include="Generated\**" />
</ItemGroup>
<ItemGroup>
<None Remove="ComplexHeaderParameter.cs.error" />
<None Remove="ComplexQueryParameter.cs.error" />
<None Remove="CustomizeResponseWithErrors.cs.error" />
<None Remove="InvalidParameterAttributeNames.cs.error" />
Expand Down
Loading