Skip to content

Commit e2b8741

Browse files
committed
update for resourcename
1 parent 239a8c1 commit e2b8741

9 files changed

Lines changed: 191 additions & 118 deletions

File tree

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ public static class DiagnosticDescriptors
166166
public static readonly DiagnosticDescriptor HttpApiAuthorizerNotFound = new DiagnosticDescriptor(
167167
id: "AWSLambda0121",
168168
title: "HTTP API Authorizer Not Found",
169-
messageFormat: "Authorizer '{0}' referenced in [HttpApi] attribute does not exist. Add [HttpApiAuthorizer] to an authorizer Lambda function method named '{0}', or reference it using nameof().",
169+
messageFormat: "Authorizer '{0}' referenced in [HttpApi] attribute does not exist. Ensure a Lambda function with [HttpApiAuthorizer] exists with method name '{0}' or ResourceName = \"{0}\".",
170170
category: "AWSLambdaCSharpGenerator",
171171
DiagnosticSeverity.Error,
172172
isEnabledByDefault: true);
173173

174174
public static readonly DiagnosticDescriptor RestApiAuthorizerNotFound = new DiagnosticDescriptor(
175175
id: "AWSLambda0122",
176176
title: "REST API Authorizer Not Found",
177-
messageFormat: "Authorizer '{0}' referenced in [RestApi] attribute does not exist. Add [RestApiAuthorizer] to an authorizer Lambda function method named '{0}', or reference it using nameof().",
177+
messageFormat: "Authorizer '{0}' referenced in [RestApi] attribute does not exist. Ensure a Lambda function with [RestApiAuthorizer] exists with method name '{0}' or ResourceName = \"{0}\".",
178178
category: "AWSLambdaCSharpGenerator",
179179
DiagnosticSeverity.Error,
180180
isEnabledByDefault: true);

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Generator.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,12 @@ private static AuthorizerModel ExtractAuthorizerModel(IMethodSymbol methodSymbol
359359

360360
if (model != null)
361361
{
362-
// The authorizer name is always derived from the method name
363-
model.Name = methodSymbol.Name;
362+
// Use LambdaFunction's ResourceName if explicitly set, otherwise fall back to method name
363+
var lambdaFunctionAttr = methodSymbol.GetAttributes()
364+
.FirstOrDefault(a => a.AttributeClass?.ToDisplayString() == TypeFullNames.LambdaFunctionAttribute);
365+
var explicitResourceName = lambdaFunctionAttr?.NamedArguments
366+
.FirstOrDefault(arg => arg.Key == "ResourceName").Value.Value as string;
367+
model.Name = explicitResourceName ?? methodSymbol.Name;
364368
return model;
365369
}
366370
}

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/AuthorizerSetupParameters.cs

Lines changed: 140 additions & 71 deletions
Large diffs are not rendered by default.

Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/ServerlessTemplates/authorizerIAuthorizerResult.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"Properties": {
1212
"Auth": {
1313
"Authorizers": {
14-
"SimpleHttpApiAuthorizer": {
14+
"SimpleHttpApiAuth": {
1515
"FunctionArn": {
1616
"Fn::GetAtt": [
1717
"SimpleHttpApiAuth",
@@ -38,7 +38,7 @@
3838
"StageName": "Prod",
3939
"Auth": {
4040
"Authorizers": {
41-
"SimpleRestApiAuthorizer": {
41+
"SimpleRestApiAuth": {
4242
"FunctionArn": {
4343
"Fn::GetAtt": [
4444
"SimpleRestApiAuth",

Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/ServerlessTemplates/customAuthorizerApp.template

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"Properties": {
1212
"Auth": {
1313
"Authorizers": {
14-
"HttpApiAuthorize": {
14+
"CustomAuthorizer": {
1515
"FunctionArn": {
1616
"Fn::GetAtt": [
1717
"CustomAuthorizer",
@@ -28,7 +28,7 @@
2828
"EnableFunctionDefaultPermissions": true,
2929
"AuthorizerResultTtlInSeconds": 0
3030
},
31-
"HttpApiAuthorizeV1": {
31+
"CustomAuthorizerV1": {
3232
"FunctionArn": {
3333
"Fn::GetAtt": [
3434
"CustomAuthorizerV1",
@@ -45,7 +45,7 @@
4545
"EnableFunctionDefaultPermissions": true,
4646
"AuthorizerResultTtlInSeconds": 0
4747
},
48-
"SimpleHttpApiAuthorize": {
48+
"SimpleAuthorizer": {
4949
"FunctionArn": {
5050
"Fn::GetAtt": [
5151
"SimpleAuthorizer",
@@ -72,7 +72,7 @@
7272
"StageName": "Prod",
7373
"Auth": {
7474
"Authorizers": {
75-
"RestApiAuthorize": {
75+
"RestApiAuthorizer": {
7676
"FunctionArn": {
7777
"Fn::GetAtt": [
7878
"RestApiAuthorizer",
@@ -85,7 +85,7 @@
8585
"FunctionPayloadType": "TOKEN",
8686
"AuthorizerResultTtlInSeconds": 0
8787
},
88-
"SimpleRestApiAuthorize": {
88+
"SimpleRestAuthorizer": {
8989
"FunctionArn": {
9090
"Fn::GetAtt": [
9191
"SimpleRestAuthorizer",
@@ -223,7 +223,7 @@
223223
"Path": "/api/protected",
224224
"Method": "GET",
225225
"Auth": {
226-
"Authorizer": "HttpApiAuthorize"
226+
"Authorizer": "CustomAuthorizer"
227227
},
228228
"ApiId": {
229229
"Ref": "AnnotationsHttpApi"
@@ -266,7 +266,7 @@
266266
"Path": "/api/user-info",
267267
"Method": "GET",
268268
"Auth": {
269-
"Authorizer": "HttpApiAuthorize"
269+
"Authorizer": "CustomAuthorizer"
270270
},
271271
"ApiId": {
272272
"Ref": "AnnotationsHttpApi"
@@ -348,7 +348,7 @@
348348
"Path": "/api/rest-user-info",
349349
"Method": "GET",
350350
"Auth": {
351-
"Authorizer": "RestApiAuthorize"
351+
"Authorizer": "RestApiAuthorizer"
352352
},
353353
"RestApiId": {
354354
"Ref": "AnnotationsRestApi"
@@ -393,7 +393,7 @@
393393
"Method": "GET",
394394
"PayloadFormatVersion": "1.0",
395395
"Auth": {
396-
"Authorizer": "HttpApiAuthorizeV1"
396+
"Authorizer": "CustomAuthorizerV1"
397397
},
398398
"ApiId": {
399399
"Ref": "AnnotationsHttpApi"
@@ -436,7 +436,7 @@
436436
"Path": "/api/ihttpresult-user-info",
437437
"Method": "GET",
438438
"Auth": {
439-
"Authorizer": "HttpApiAuthorize"
439+
"Authorizer": "CustomAuthorizer"
440440
},
441441
"ApiId": {
442442
"Ref": "AnnotationsHttpApi"
@@ -479,7 +479,7 @@
479479
"Path": "/api/simple-httpapi-user-info",
480480
"Method": "GET",
481481
"Auth": {
482-
"Authorizer": "SimpleHttpApiAuthorize"
482+
"Authorizer": "SimpleAuthorizer"
483483
},
484484
"ApiId": {
485485
"Ref": "AnnotationsHttpApi"
@@ -522,7 +522,7 @@
522522
"Path": "/api/simple-restapi-user-info",
523523
"Method": "GET",
524524
"Auth": {
525-
"Authorizer": "SimpleRestApiAuthorize"
525+
"Authorizer": "SimpleRestAuthorizer"
526526
},
527527
"RestApiId": {
528528
"Ref": "AnnotationsRestApi"
@@ -565,7 +565,7 @@
565565
"Path": "/api/nonstring-user-info",
566566
"Method": "GET",
567567
"Auth": {
568-
"Authorizer": "HttpApiAuthorize"
568+
"Authorizer": "CustomAuthorizer"
569569
},
570570
"ApiId": {
571571
"Ref": "AnnotationsHttpApi"
@@ -576,4 +576,4 @@
576576
}
577577
}
578578
}
579-
}
579+
}

Libraries/test/TestCustomAuthorizerApp/ProtectedFunction.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ProtectedFunction
1818
/// Debug endpoint to see what's in the RequestContext.Authorizer
1919
/// </summary>
2020
[LambdaFunction(ResourceName = "ProtectedEndpoint")]
21-
[HttpApi(LambdaHttpMethod.Get, "/api/protected", Authorizer = nameof(AuthorizerFunction.HttpApiAuthorize))]
21+
[HttpApi(LambdaHttpMethod.Get, "/api/protected", Authorizer = "CustomAuthorizer")]
2222
public string GetProtectedData(
2323
APIGatewayHttpApiV2ProxyRequest request,
2424
ILambdaContext context)
@@ -88,7 +88,7 @@ public string GetProtectedData(
8888
/// Another protected endpoint showing different usage - just getting the email.
8989
/// </summary>
9090
[LambdaFunction(ResourceName = "GetUserInfo")]
91-
[HttpApi(LambdaHttpMethod.Get, "/api/user-info", Authorizer = nameof(AuthorizerFunction.HttpApiAuthorize))]
91+
[HttpApi(LambdaHttpMethod.Get, "/api/user-info", Authorizer = "CustomAuthorizer")]
9292
public object GetUserInfo(
9393
[FromCustomAuthorizer(Name = "userId")] string userId,
9494
[FromCustomAuthorizer(Name = "email")] string email,
@@ -123,7 +123,7 @@ public string HealthCheck(ILambdaContext context)
123123
/// REST API authorizers use a different context structure than HTTP API v2.
124124
/// </summary>
125125
[LambdaFunction(ResourceName = "RestUserInfo")]
126-
[RestApi(LambdaHttpMethod.Get, "/api/rest-user-info", Authorizer = nameof(AuthorizerFunction.RestApiAuthorize))]
126+
[RestApi(LambdaHttpMethod.Get, "/api/rest-user-info", Authorizer = "RestApiAuthorizer")]
127127
public object GetRestUserInfo(
128128
[FromCustomAuthorizer(Name = "userId")] string userId,
129129
[FromCustomAuthorizer(Name = "email")] string email,
@@ -149,7 +149,7 @@ public object GetRestUserInfo(
149149
/// where RequestContext.Authorizer is a dictionary, not RequestContext.Authorizer.Lambda.
150150
/// </summary>
151151
[LambdaFunction(ResourceName = "HttpApiV1UserInfo")]
152-
[HttpApi(LambdaHttpMethod.Get, "/api/http-v1-user-info", Version = HttpApiVersion.V1, Authorizer = nameof(AuthorizerFunction.HttpApiAuthorizeV1))]
152+
[HttpApi(LambdaHttpMethod.Get, "/api/http-v1-user-info", Version = HttpApiVersion.V1, Authorizer = "CustomAuthorizerV1")]
153153
public object GetHttpApiV1UserInfo(
154154
[FromCustomAuthorizer(Name = "userId")] string userId,
155155
[FromCustomAuthorizer(Name = "email")] string email,
@@ -175,7 +175,7 @@ public object GetHttpApiV1UserInfo(
175175
/// when authorizer context is missing (the handler returns Stream, not response object).
176176
/// </summary>
177177
[LambdaFunction(ResourceName = "IHttpResultUserInfo")]
178-
[HttpApi(LambdaHttpMethod.Get, "/api/ihttpresult-user-info", Authorizer = nameof(AuthorizerFunction.HttpApiAuthorize))]
178+
[HttpApi(LambdaHttpMethod.Get, "/api/ihttpresult-user-info", Authorizer = "CustomAuthorizer")]
179179
public IHttpResult GetIHttpResult(
180180
[FromCustomAuthorizer(Name = "userId")] string userId,
181181
[FromCustomAuthorizer(Name = "email")] string email,
@@ -197,7 +197,7 @@ public IHttpResult GetIHttpResult(
197197
/// protected endpoint extracts context values via [FromCustomAuthorizer].
198198
/// </summary>
199199
[LambdaFunction(ResourceName = "SimpleHttpApiUserInfo")]
200-
[HttpApi(LambdaHttpMethod.Get, "/api/simple-httpapi-user-info", Authorizer = nameof(AuthorizerFunction.SimpleHttpApiAuthorize))]
200+
[HttpApi(LambdaHttpMethod.Get, "/api/simple-httpapi-user-info", Authorizer = "SimpleAuthorizer")]
201201
public object GetSimpleHttpApiUserInfo(
202202
[FromCustomAuthorizer(Name = "userId")] string userId,
203203
[FromCustomAuthorizer(Name = "email")] string email,
@@ -222,7 +222,7 @@ public object GetSimpleHttpApiUserInfo(
222222
/// API Gateway accepts → protected endpoint extracts context values via [FromCustomAuthorizer].
223223
/// </summary>
224224
[LambdaFunction(ResourceName = "SimpleRestApiUserInfo")]
225-
[RestApi(LambdaHttpMethod.Get, "/api/simple-restapi-user-info", Authorizer = nameof(AuthorizerFunction.SimpleRestApiAuthorize))]
225+
[RestApi(LambdaHttpMethod.Get, "/api/simple-restapi-user-info", Authorizer = "SimpleRestAuthorizer")]
226226
public object GetSimpleRestApiUserInfo(
227227
[FromCustomAuthorizer(Name = "userId")] string userId,
228228
[FromCustomAuthorizer(Name = "email")] string email,
@@ -247,7 +247,7 @@ public object GetSimpleRestApiUserInfo(
247247
/// the Lambda authorizer context.
248248
/// </summary>
249249
[LambdaFunction(ResourceName = "NonStringUserInfo")]
250-
[HttpApi(LambdaHttpMethod.Get, "/api/nonstring-user-info", Authorizer = nameof(AuthorizerFunction.HttpApiAuthorize))]
250+
[HttpApi(LambdaHttpMethod.Get, "/api/nonstring-user-info", Authorizer = "CustomAuthorizer")]
251251
public object GetNonStringUserInfo(
252252
APIGatewayHttpApiV2ProxyRequest request,
253253
[FromCustomAuthorizer(Name = "numericTenantId")] int tenantId,

Libraries/test/TestCustomAuthorizerApp/serverless.template

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"Properties": {
1212
"Auth": {
1313
"Authorizers": {
14-
"HttpApiAuthorize": {
14+
"CustomAuthorizer": {
1515
"FunctionArn": {
1616
"Fn::GetAtt": [
1717
"CustomAuthorizer",
@@ -28,7 +28,7 @@
2828
"EnableFunctionDefaultPermissions": true,
2929
"AuthorizerResultTtlInSeconds": 0
3030
},
31-
"HttpApiAuthorizeV1": {
31+
"CustomAuthorizerV1": {
3232
"FunctionArn": {
3333
"Fn::GetAtt": [
3434
"CustomAuthorizerV1",
@@ -45,7 +45,7 @@
4545
"EnableFunctionDefaultPermissions": true,
4646
"AuthorizerResultTtlInSeconds": 0
4747
},
48-
"SimpleHttpApiAuthorize": {
48+
"SimpleAuthorizer": {
4949
"FunctionArn": {
5050
"Fn::GetAtt": [
5151
"SimpleAuthorizer",
@@ -72,7 +72,7 @@
7272
"StageName": "Prod",
7373
"Auth": {
7474
"Authorizers": {
75-
"RestApiAuthorize": {
75+
"RestApiAuthorizer": {
7676
"FunctionArn": {
7777
"Fn::GetAtt": [
7878
"RestApiAuthorizer",
@@ -85,7 +85,7 @@
8585
"FunctionPayloadType": "TOKEN",
8686
"AuthorizerResultTtlInSeconds": 0
8787
},
88-
"SimpleRestApiAuthorize": {
88+
"SimpleRestAuthorizer": {
8989
"FunctionArn": {
9090
"Fn::GetAtt": [
9191
"SimpleRestAuthorizer",
@@ -223,7 +223,7 @@
223223
"Path": "/api/protected",
224224
"Method": "GET",
225225
"Auth": {
226-
"Authorizer": "HttpApiAuthorize"
226+
"Authorizer": "CustomAuthorizer"
227227
},
228228
"ApiId": {
229229
"Ref": "AnnotationsHttpApi"
@@ -266,7 +266,7 @@
266266
"Path": "/api/user-info",
267267
"Method": "GET",
268268
"Auth": {
269-
"Authorizer": "HttpApiAuthorize"
269+
"Authorizer": "CustomAuthorizer"
270270
},
271271
"ApiId": {
272272
"Ref": "AnnotationsHttpApi"
@@ -348,7 +348,7 @@
348348
"Path": "/api/rest-user-info",
349349
"Method": "GET",
350350
"Auth": {
351-
"Authorizer": "RestApiAuthorize"
351+
"Authorizer": "RestApiAuthorizer"
352352
},
353353
"RestApiId": {
354354
"Ref": "AnnotationsRestApi"
@@ -393,7 +393,7 @@
393393
"Method": "GET",
394394
"PayloadFormatVersion": "1.0",
395395
"Auth": {
396-
"Authorizer": "HttpApiAuthorizeV1"
396+
"Authorizer": "CustomAuthorizerV1"
397397
},
398398
"ApiId": {
399399
"Ref": "AnnotationsHttpApi"
@@ -436,7 +436,7 @@
436436
"Path": "/api/ihttpresult-user-info",
437437
"Method": "GET",
438438
"Auth": {
439-
"Authorizer": "HttpApiAuthorize"
439+
"Authorizer": "CustomAuthorizer"
440440
},
441441
"ApiId": {
442442
"Ref": "AnnotationsHttpApi"
@@ -479,7 +479,7 @@
479479
"Path": "/api/simple-httpapi-user-info",
480480
"Method": "GET",
481481
"Auth": {
482-
"Authorizer": "SimpleHttpApiAuthorize"
482+
"Authorizer": "SimpleAuthorizer"
483483
},
484484
"ApiId": {
485485
"Ref": "AnnotationsHttpApi"
@@ -522,7 +522,7 @@
522522
"Path": "/api/simple-restapi-user-info",
523523
"Method": "GET",
524524
"Auth": {
525-
"Authorizer": "SimpleRestApiAuthorize"
525+
"Authorizer": "SimpleRestAuthorizer"
526526
},
527527
"RestApiId": {
528528
"Ref": "AnnotationsRestApi"
@@ -565,7 +565,7 @@
565565
"Path": "/api/nonstring-user-info",
566566
"Method": "GET",
567567
"Auth": {
568-
"Authorizer": "HttpApiAuthorize"
568+
"Authorizer": "CustomAuthorizer"
569569
},
570570
"ApiId": {
571571
"Ref": "AnnotationsHttpApi"

Libraries/test/TestServerlessApp/aws-lambda-tools-defaults.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"template": "serverless.template",
1414
"template-parameters": "",
1515
"docker-host-build-output-dir": "./bin/Release/lambda-publish",
16-
"s3-bucket" : "test-serverless-app-b2c0a6d3",
17-
"stack-name" : "test-serverless-app-b2c0a6d3",
18-
"function-architecture" : "x86_64"
19-
}
16+
"s3-bucket": "test-serverless-app",
17+
"stack-name": "test-serverless-app",
18+
"function-architecture": "x86_64"
19+
}

Libraries/test/TestServerlessApp/serverless.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"Properties": {
1515
"Auth": {
1616
"Authorizers": {
17-
"SimpleHttpApiAuthorizer": {
17+
"SimpleHttpApiAuth": {
1818
"FunctionArn": {
1919
"Fn::GetAtt": [
2020
"SimpleHttpApiAuth",
@@ -41,7 +41,7 @@
4141
"StageName": "Prod",
4242
"Auth": {
4343
"Authorizers": {
44-
"SimpleRestApiAuthorizer": {
44+
"SimpleRestApiAuth": {
4545
"FunctionArn": {
4646
"Fn::GetAtt": [
4747
"SimpleRestApiAuth",

0 commit comments

Comments
 (0)