Skip to content

Fix issue with Source generator for Lambda functions that return void #1257

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
merged 1 commit into from
Aug 2, 2022

Conversation

normj
Copy link
Member

@normj normj commented Jul 31, 2022

Description of changes:
For Amazon.Lambda.Annotations if a method with the LambdaMethod attribute has its return type set to void the source generator would generate invalid code.

For example if you had a Lambda function like this

public class VoidExample
{
    [LambdaFunction()]
    public void VoidReturn(string text, ILambdaContext context)
    {
        context.Logger.LogLine(text);
    }
}

The generated code that wraps this call would previously look like this with an invalid return statement when calling this void method.

public class VoidExample_VoidReturn_Generated
{
    private readonly VoidExample voidExample;

...

    public void VoidReturn(string text, Amazon.Lambda.Core.ILambdaContext __context__)
    {
        return voidExample.VoidReturn(text, __context__);
    }

...
}

This fix removes the invalid return statement. The reason this code was happening is the code was treating methods that return void and Task as the same via the ReturnVoidOrTask property. Since it is not safe to treat void and task the same I replaced this property with ReturnVoid and ReturnTask forcing users to think what is the right thing to do in the context.

Ignore the LambdaFunctionTemplate.cs file because that is generated from the LambdaFunctionTemplate.tt file.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@normj normj force-pushed the normj/annotations-void-fix branch 2 times, most recently from 04311e5 to 1dbc9d2 Compare August 1, 2022 18:39
@@ -0,0 +1 @@
{"AWSTemplateFormatVersion":"2010-09-09","Transform":"AWS::Serverless-2016-10-31","Description":"An AWS Serverless Application.","Parameters":{"ArchitectureTypeParameter":{"Type":"String","Default":"x86_64","AllowedValues":["x86_64","arm64"]}},"Globals":{"Function":{"Architectures":[{"Ref":"ArchitectureTypeParameter"}]}},"Resources":{"GreeterSayHello":{"Type":"AWS::Serverless::Function","Metadata":{"Tool":"Amazon.Lambda.Annotations","SyncedEvents":["RootGet"]},"Properties":{"MemorySize":1024,"Timeout":30,"Role":null,"Policies":["AWSLambdaBasicExecutionRole"],"Events":{"RootGet":{"Type":"HttpApi","Properties":{"Path":"/Greeter/SayHello","Method":"GET","PayloadFormatVersion":"1.0"}}},"PackageType":"Image","ImageUri":"626492997873.dkr.ecr.us-west-2.amazonaws.com/testserverlessapp:greetersayhello-c013811cde6a-latest","ImageConfig":{"Command":["TestServerlessApp::TestServerlessApp.Greeter_SayHello_Generated::SayHello"]}}},"SimpleCalculatorAdd":{"Type":"AWS::Serverless::Function","Metadata":{"Tool":"Amazon.Lambda.Annotations","SyncedEvents":["RootGet"]},"Properties":{"MemorySize":256,"Timeout":30,"Role":null,"Policies":["AWSLambdaBasicExecutionRole"],"Events":{"RootGet":{"Type":"Api","Properties":{"Path":"/SimpleCalculator/Add","Method":"GET"}}},"PackageType":"Image","ImageUri":"626492997873.dkr.ecr.us-west-2.amazonaws.com/testserverlessapp:greetersayhello-c013811cde6a-latest","ImageConfig":{"Command":["TestServerlessApp::TestServerlessApp.SimpleCalculator_Add_Generated::Add"]}}},"SimpleCalculatorSubtract":{"Type":"AWS::Serverless::Function","Metadata":{"Tool":"Amazon.Lambda.Annotations","SyncedEvents":["RootGet"]},"Properties":{"MemorySize":256,"Timeout":30,"Role":null,"Policies":["AWSLambdaBasicExecutionRole"],"Events":{"RootGet":{"Type":"Api","Properties":{"Path":"/SimpleCalculator/Subtract","Method":"GET"}}},"PackageType":"Image","ImageUri":"626492997873.dkr.ecr.us-west-2.amazonaws.com/testserverlessapp:greetersayhello-c013811cde6a-latest","ImageConfig":{"Command":["TestServerlessApp::TestServerlessApp.SimpleCalculator_Subtract_Generated::Subtract"]}}},"SimpleCalculatorMultiply":{"Type":"AWS::Serverless::Function","Metadata":{"Tool":"Amazon.Lambda.Annotations","SyncedEvents":["RootGet"]},"Properties":{"MemorySize":256,"Timeout":30,"Role":null,"Policies":["AWSLambdaBasicExecutionRole"],"Events":{"RootGet":{"Type":"Api","Properties":{"Path":"/SimpleCalculator/Multiply/{x}/{y}","Method":"GET"}}},"PackageType":"Image","ImageUri":"626492997873.dkr.ecr.us-west-2.amazonaws.com/testserverlessapp:greetersayhello-c013811cde6a-latest","ImageConfig":{"Command":["TestServerlessApp::TestServerlessApp.SimpleCalculator_Multiply_Generated::Multiply"]}}},"SimpleCalculatorDivideAsync":{"Type":"AWS::Serverless::Function","Metadata":{"Tool":"Amazon.Lambda.Annotations","SyncedEvents":["RootGet"]},"Properties":{"MemorySize":256,"Timeout":30,"Role":null,"Policies":["AWSLambdaBasicExecutionRole"],"Events":{"RootGet":{"Type":"Api","Properties":{"Path":"/SimpleCalculator/DivideAsync/{x}/{y}","Method":"GET"}}},"PackageType":"Image","ImageUri":"626492997873.dkr.ecr.us-west-2.amazonaws.com/testserverlessapp:greetersayhello-c013811cde6a-latest","ImageConfig":{"Command":["TestServerlessApp::TestServerlessApp.SimpleCalculator_DivideAsync_Generated::DivideAsync"]}}},"TestServerlessAppComplexCalculatorAddGenerated":{"Type":"AWS::Serverless::Function","Metadata":{"Tool":"Amazon.Lambda.Annotations","SyncedEvents":["RootPost"]},"Properties":{"MemorySize":256,"Timeout":30,"Role":null,"Policies":["AWSLambdaBasicExecutionRole"],"Events":{"RootPost":{"Type":"HttpApi","Properties":{"Path":"/ComplexCalculator/Add","Method":"POST","PayloadFormatVersion":"2.0"}}},"PackageType":"Image","ImageUri":"626492997873.dkr.ecr.us-west-2.amazonaws.com/testserverlessapp:greetersayhello-c013811cde6a-latest","ImageConfig":{"Command":["TestServerlessApp::TestServerlessApp.ComplexCalculator_Add_Generated::Add"]}}},"TestServerlessAppComplexCalculatorSubtractGenerated":{"Type":"AWS::Serverless::Function","Metadata":{"Tool":"Amazon.Lambda.Annotations","SyncedEvents":["RootPost"]},"Properties":{"MemorySize":256,"Timeout":30,"Policies":["AWSLambdaBasicExecutionRole"],"Events":{"RootPost":{"Type":"HttpApi","Properties":{"Path":"/ComplexCalculator/Subtract","Method":"POST","PayloadFormatVersion":"2.0"}}},"PackageType":"Image","ImageUri":"626492997873.dkr.ecr.us-west-2.amazonaws.com/testserverlessapp:greetersayhello-c013811cde6a-latest","ImageConfig":{"Command":["TestServerlessApp::TestServerlessApp.ComplexCalculator_Subtract_Generated::Subtract"]}}},"PI":{"Type":"AWS::Serverless::Function","Metadata":{"Tool":"Amazon.Lambda.Annotations"},"Properties":{"MemorySize":256,"Timeout":30,"Policies":["AWSLambdaBasicExecutionRole"],"PackageType":"Image","ImageUri":"626492997873.dkr.ecr.us-west-2.amazonaws.com/testserverlessapp:greetersayhello-c013811cde6a-latest","ImageConfig":{"Command":["TestServerlessApp::TestServerlessApp.SimpleCalculator_Pi_Generated::Pi"]}}},"Random":{"Type":"AWS::Serverless::Function","Metadata":{"Tool":"Amazon.Lambda.Annotations"},"Properties":{"MemorySize":256,"Timeout":30,"Policies":["AWSLambdaBasicExecutionRole"],"PackageType":"Image","ImageUri":"626492997873.dkr.ecr.us-west-2.amazonaws.com/testserverlessapp:greetersayhello-c013811cde6a-latest","ImageConfig":{"Command":["TestServerlessApp::TestServerlessApp.SimpleCalculator_Random_Generated::Random"]}}},"Randoms":{"Type":"AWS::Serverless::Function","Metadata":{"Tool":"Amazon.Lambda.Annotations"},"Properties":{"MemorySize":256,"Timeout":30,"Policies":["AWSLambdaBasicExecutionRole"],"PackageType":"Image","ImageUri":"626492997873.dkr.ecr.us-west-2.amazonaws.com/testserverlessapp:greetersayhello-c013811cde6a-latest","ImageConfig":{"Command":["TestServerlessApp::TestServerlessApp.SimpleCalculator_Randoms_Generated::Randoms"]}}},"GreeterSayHelloAsync":{"Type":"AWS::Serverless::Function","Metadata":{"Tool":"Amazon.Lambda.Annotations","SyncedEvents":["RootGet"]},"Properties":{"MemorySize":256,"Timeout":50,"Policies":["AWSLambdaBasicExecutionRole"],"PackageType":"Image","Events":{"RootGet":{"Type":"HttpApi","Properties":{"Path":"/Greeter/SayHelloAsync","Method":"GET","PayloadFormatVersion":"1.0"}}},"ImageUri":"626492997873.dkr.ecr.us-west-2.amazonaws.com/testserverlessapp:greetersayhello-c013811cde6a-latest","ImageConfig":{"Command":["TestServerlessApp::TestServerlessApp.Greeter_SayHelloAsync_Generated::SayHelloAsync"]}}},"ToUpper":{"Type":"AWS::Serverless::Function","Metadata":{"Tool":"Amazon.Lambda.Annotations"},"Properties":{"Runtime":"dotnet6","CodeUri":"s3://normj-west2/","MemorySize":256,"Timeout":30,"Policies":["AWSLambdaBasicExecutionRole"],"PackageType":"Zip","Handler":"TestServerlessApp::TestServerlessApp.Sub1.Functions_ToUpper_Generated::ToUpper"}}},"Outputs":{"RestApiURL":{"Description":"Rest API endpoint URL for Prod environment","Value":{"Fn::Sub":"https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod"}},"HttpApiURL":{"Description":"HTTP API endpoint URL for Prod environment","Value":{"Fn::Sub":"https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com"}}}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this exist?

serverless.template must have the new function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

@normj normj force-pushed the normj/annotations-void-fix branch from 1dbc9d2 to 454cf93 Compare August 2, 2022 16:25
@normj normj merged commit 25b9bc3 into dev Aug 2, 2022
@hounddog22030
Copy link
Contributor

@normj - is this related: #1200

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants