Skip to content

Commit

Permalink
Fixed an issue where primitive valuesin payload for InvokeFunctionCom…
Browse files Browse the repository at this point in the history
…mand were not working.
  • Loading branch information
ashishdhingra committed Oct 30, 2024
1 parent 969aa52 commit 1d803c3
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 6 deletions.
9 changes: 8 additions & 1 deletion aws-extensions-for-dotnet-cli.sln
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Amazon.Lambda.Tools.Integ.T
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestHttpFunction", "testapps\TestHttpFunction\TestHttpFunction.csproj", "{AD31D053-97C5-4262-B187-EC42BFD51A9F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestNativeAotNet8WebApp", "testapps\TestNativeAotNet8WebApp\TestNativeAotNet8WebApp.csproj", "{69FFA03C-D29F-40E0-9E7F-572D5E10AF77}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestNativeAotNet8WebApp", "testapps\TestNativeAotNet8WebApp\TestNativeAotNet8WebApp.csproj", "{69FFA03C-D29F-40E0-9E7F-572D5E10AF77}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestIntegerFunction", "testapps\TestIntegerFunction\TestIntegerFunction.csproj", "{D7F1DFA4-066B-469C-B04C-DF032CF152C1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -163,6 +165,10 @@ Global
{69FFA03C-D29F-40E0-9E7F-572D5E10AF77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69FFA03C-D29F-40E0-9E7F-572D5E10AF77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69FFA03C-D29F-40E0-9E7F-572D5E10AF77}.Release|Any CPU.Build.0 = Release|Any CPU
{D7F1DFA4-066B-469C-B04C-DF032CF152C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D7F1DFA4-066B-469C-B04C-DF032CF152C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7F1DFA4-066B-469C-B04C-DF032CF152C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7F1DFA4-066B-469C-B04C-DF032CF152C1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -193,6 +199,7 @@ Global
{7B2AE176-8AB5-4050-8E22-A2A80E88BB92} = {BB0A8314-3127-4159-8B6A-64F97FEF9474}
{AD31D053-97C5-4262-B187-EC42BFD51A9F} = {BB3CF729-8213-4DDD-85AE-A5E7754F3944}
{69FFA03C-D29F-40E0-9E7F-572D5E10AF77} = {BB3CF729-8213-4DDD-85AE-A5E7754F3944}
{D7F1DFA4-066B-469C-B04C-DF032CF152C1} = {BB3CF729-8213-4DDD-85AE-A5E7754F3944}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DBFC70D6-49A2-40A1-AB08-5D9504AB7112}
Expand Down
2 changes: 1 addition & 1 deletion src/Amazon.Lambda.Tools/Amazon.Lambda.Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<Copyright>Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.</Copyright>
<Product>AWS Lambda Tools for .NET CLI</Product>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<Version>5.11.0</Version>
<Version>5.11.1</Version>
</PropertyGroup>
<ItemGroup>
<Content Include="Resources\build-lambda-zip.exe">
Expand Down
16 changes: 13 additions & 3 deletions src/Amazon.Lambda.Tools/Commands/InvokeFunctionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ protected override void ParseCommandArguments(CommandOptions values)

protected override async Task<bool> PerformActionAsync()
{

var invokeRequest = new InvokeRequest
{
FunctionName = this.GetStringValueOrDefault(this.FunctionName, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_NAME, true),
Expand All @@ -81,9 +80,20 @@ protected override async Task<bool> PerformActionAsync()
invokeRequest.Payload = this.Payload.Trim();
}

if(!invokeRequest.Payload.StartsWith("{"))
// We should still check for empty payload in case it is read from a file.
if (!string.IsNullOrEmpty(invokeRequest.Payload))
{
invokeRequest.Payload = "\"" + invokeRequest.Payload + "\"";
if (invokeRequest.Payload[0] != '\"' && invokeRequest.Payload[0] != '{' && invokeRequest.Payload[0] != '[')
{
double d;
long l;
bool b;
if (!double.TryParse(invokeRequest.Payload, out d) && !long.TryParse(invokeRequest.Payload, out l) &&
!bool.TryParse(invokeRequest.Payload, out b))
{
invokeRequest.Payload = "\"" + invokeRequest.Payload + "\"";
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Amazon.Lambda.Tools\Amazon.Lambda.Tools.csproj" />
<ProjectReference Include="..\..\testapps\TestFunction\TestFunction.csproj" />
<ProjectReference Include="..\..\testapps\TestIntegerFunction\TestIntegerFunction.csproj" />
<ProjectReference Include="..\Amazon.Tools.TestHelpers\Amazon.Tools.TestHelpers.csproj" />
</ItemGroup>
<ItemGroup>
Expand Down
45 changes: 44 additions & 1 deletion test/Amazon.Lambda.Tools.Test/DeployTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,50 @@ public async Task RunDeployCommand()
}
}
}


[Fact]
public async Task RunDeployAndInvokeWithIntegerPayloadCommand()
{
var assembly = this.GetType().GetTypeInfo().Assembly;
var toolLogger = new TestToolLogger(_testOutputHelper);

var fullPath = Path.GetFullPath(Path.GetDirectoryName(assembly.Location) + "../../../../../../testapps/TestIntegerFunction");
var command = new DeployFunctionCommand(toolLogger, fullPath, new string[0]);
command.FunctionName = "test-function-intpayload-" + DateTime.Now.Ticks;
command.Handler = "TestIntegerFunction::TestIntegerFunction.Function::FunctionHandler";
command.Timeout = 10;
command.MemorySize = 512;
command.Role = await TestHelper.GetTestRoleArnAsync();
command.Configuration = "Release";
command.Runtime = "dotnet6";
command.DisableInteractive = true;

var created = await command.ExecuteAsync();
try
{
Assert.True(created);

await LambdaUtilities.WaitTillFunctionAvailableAsync(new TestToolLogger(_testOutputHelper), command.LambdaClient, command.FunctionName);

toolLogger.ClearBuffer();

var invokeCommand = new InvokeFunctionCommand(toolLogger, fullPath, new string[0]);
invokeCommand.FunctionName = command.FunctionName;
invokeCommand.Payload = "42";

await invokeCommand.ExecuteAsync();

Assert.Contains("\"Hello 42\"", toolLogger.Buffer);
}
finally
{
if (created)
{
await command.LambdaClient.DeleteFunctionAsync(command.FunctionName);
}
}
}

[Fact]
public async Task TestPowerShellLambdaParallelTestCommand()
{
Expand Down
17 changes: 17 additions & 0 deletions testapps/TestIntegerFunction/Function.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace TestIntegerFunction
{

public class Function
{
[Amazon.Lambda.Core.LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
public string FunctionHandler(int input)
{
return "Hello " + input;
}
}
}
14 changes: 14 additions & 0 deletions testapps/TestIntegerFunction/TestIntegerFunction.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AssemblyName>TestIntegerFunction</AssemblyName>
<OutputType>Library</OutputType>
<PackageId>TestIntegerFunction</PackageId>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.0.0" />
</ItemGroup>
</Project>
5 changes: 5 additions & 0 deletions testapps/TestIntegerFunction/aws-lambda-tools-defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"region": "us-east-2",
"disable-version-check": true,
"function-memory-size": 128
}

0 comments on commit 1d803c3

Please sign in to comment.