aws / aws-lambda-dotnet

Libraries, samples and tools to help .NET Core developers develop AWS Lambda functions.
Apache License 2.0
1.58k stars 478 forks source link

Unable to get FromQuery to work #1509

Open irvanherz opened 1 year ago

irvanherz commented 1 year ago

Describe the bug

I created a REST API using lambda annotations. The FromBody works like charm, but I found that FromQuery annotation didn't work like expected. It gives me null value instead of object.

Expected Behavior

FromQuery should works like FromBody

Current Behavior

It doesn't work.

Reproduction Steps

I created a lambda function like below.

public class TestQueryParamsDto
{
    public string x { get; set; } = "hello";
    public string y { get; set; }
}

[LambdaFunction(ResourceName = "TestQueryParams", Policies = "AdministratorAccess")]
[RestApi(LambdaHttpMethod.Get, "/api/test-query-params")]
public APIGatewayProxyResponse TestQueryParams(APIGatewayProxyRequest request, [FromQuery] TestQueryParamsDto query)
{
    var response = new APIGatewayProxyResponse();

    response.StatusCode = 200;
    response.Body = JsonSerializer.Serialize(new
    {
        message = "OK",
        request = request,
        query = query
    });

    return response;
}

After I test it in lambda-test-tool, I get the query argument unexpectedly has null value instead of object. Same thing happen to published functions.

Sample function input for lambda test tool:

{
  "resource": "/{proxy+}",
  "path": "/path/to/resource",
  "httpMethod": "GET",
  "queryStringParameters": {
    "x": "abc",
    "y": "abc"
  },
  "pathParameters": {
    "proxy": "path/to/resource"
  },
  "stageVariables": {
    "baz": "qux"
  },
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Encoding": "gzip, deflate, sdch",
    "Accept-Language": "en-US,en;q=0.8",
    "Cache-Control": "max-age=0",
    "CloudFront-Forwarded-Proto": "https",
    "CloudFront-Is-Desktop-Viewer": "true",
    "CloudFront-Is-Mobile-Viewer": "false",
    "CloudFront-Is-SmartTV-Viewer": "false",
    "CloudFront-Is-Tablet-Viewer": "false",
    "CloudFront-Viewer-Country": "US",
    "Host": "1234567890.execute-api.{dns_suffix}",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Custom User Agent String",
    "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
    "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
    "X-Forwarded-For": "127.0.0.1, 127.0.0.2",
    "X-Forwarded-Port": "443",
    "X-Forwarded-Proto": "https"
  },
  "requestContext": {
    "accountId": "123456789012",
    "resourceId": "123456",
    "stage": "prod",
    "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
    "identity": {
      "cognitoIdentityPoolId": null,
      "accountId": null,
      "cognitoIdentityId": null,
      "caller": null,
      "apiKey": null,
      "sourceIp": "127.0.0.1",
      "cognitoAuthenticationType": null,
      "cognitoAuthenticationProvider": null,
      "userArn": null,
      "userAgent": "Custom User Agent String",
      "user": null
    },
    "resourcePath": "/{proxy+}",
    "httpMethod": "POST",
    "apiId": "1234567890"
  }
}

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <!--<TargetFramework>netcoreapp3.1</TargetFramework>-->
      <TargetFramework>net6.0</TargetFramework>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
    <AWSProjectType>Lambda</AWSProjectType>
    <!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <Configurations>Debug;Release;DEV</Configurations>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Amazon.Lambda.Annotations" Version="0.13.2" />
    <PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
    <PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
    <PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
    <PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.102.35" />
    <PackageReference Include="EntityFramework" Version="6.4.4" />
    <PackageReference Include="LazopSdk" Version="2.0.0.1" />
    <PackageReference Include="MethodBoundaryAspect.Fody" Version="2.0.148" />
    <PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
    <PackageReference Include="RestSharp" Version="110.2.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
  </ItemGroup>
</Project>

Targeted .NET Platform

.NET 6 (C#/PowerShell)

Operating System and version

Windows 10

ashishdhingra commented 1 year ago

Appears to be reproducible. Simple parameters are bound successfully. However, complex parameter is not bound (might be it should follow convention at https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-7.0#complex-types). It doesn't work after actual deployment as well.

Tested using:

Sample Code: LambdaAnnotationsTest.zip