Skip to content

Data did not match any variant of untagged enum LambdaRequest when testing from API Gateway in the AWS Console #367

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

Closed
ionionascu opened this issue Nov 19, 2021 · 6 comments

Comments

@ionionascu
Copy link

ionionascu commented Nov 19, 2021

I have a Rust lambda that is accessed through an API Gateway with proxy integration. When I test the API Gateway's method directly in the AWS Console without passing any headers (or no query parameters), I get an error from the Lambda: Error: Error("data did not match any variant of untagged enum LambdaRequest", line: 0, column: 0)

This seems to be caused by the fact that in the API Gateway JSON request, the headers, multiValueHeaders, queryStringParameters and multiValueQueryStringParameters elements are passed as null (I do not remember seeing this error before so it may be that this used to be passed as an empty object, {}).

Using a modified sample request JSON from a different issue, I can reproduce the error with this test:

#[test]
fn test() {
    const PAYLOAD: &str = r#"{
        "version": "2.0",
        "routeKey": "GET /hello",
        "rawPath": "/hello",
        "rawQueryString": "",
        "cookies": [],

        "headers": null,
        "multiValueHeaders": null,
        "queryStringParameters": null,
        "multiValueQueryStringParameters": null,

        "requestContext": {
            "accountId": "123456789012",
            "apiId": "1234567890",
            "http": {
                "method": "GET",
                "path": "/hello",
                "protocol": "HTTP/1.1",
                "sourceIp": "127.0.0.1",
                "userAgent": "Custom User Agent String"
            },
            "requestId": "1ac06eee-f687-44ec-9036-dfd49d0be0a3",
            "routeKey": "GET /hello",
            "stage": "$default",
            "time": "16/Nov/2021:11:54:33 +0000",
            "timeEpoch": 1637063673,
            "domainName": "localhost",
            "domainPrefix": "localhost"
        },
        "body": "",
        "pathParameters": {},
        "stageVariables": null,
        "isBase64Encoded": false
    }"#;
    
    let val: lambda_http::request::LambdaRequest = serde_json::from_str(PAYLOAD).unwrap();
    println!("{:?}", val);
}

I tested with the current master branch (commit 576f6013dd188de08928dd503170b525bc17c26e), which contains the changes from another issue similar to this one (#365).

@nmoutschen
Copy link
Contributor

Hey @ionionascu!

Thanks for creating this! I was able to reproduce that a null headers value result in an error. I've created a PR that should address that issue -> #371

@ionionascu
Copy link
Author

Hi @nmoutschen. Sorry for the delay in testing the changes. I have checked the current master (which includes #371) and I still see an error reported in the AWS API Gateway's test console. The problem is caused by "multiValueHeaders": null. This test fails because of it:

#[test]
fn test() {
    const PAYLOAD: &str = r#"{
        "resource": "/test",
        "path": "/test",
        "httpMethod": "GET",
        "headers": null,
        "multiValueHeaders": null,
        "queryStringParameters": null,
        "multiValueQueryStringParameters": null,
        "pathParameters": null,
        "stageVariables": null,
        "requestContext": {
            "resourceId": "test",
            "resourcePath": "/test",
            "operationName": "test",
            "httpMethod": "GET",
            "extendedRequestId": "test",
            "requestTime": "21/Dec/2021:12:30:16 +0000",
            "path": "/test",
            "accountId": "123",
            "protocol": "HTTP/1.1",
            "stage": "test",
            "domainPrefix": "test",
            "requestTimeEpoch": 1640089816496,
            "requestId": "123",
            "identity": {
                "cognitoIdentityPoolId": null,
                "cognitoIdentityId": null,
                "apiKey": "test",
                "principalOrgId": null,
                "cognitoAuthenticationType": null,
                "userArn": "arn",
                "apiKeyId": "test",
                "userAgent": "test",
                "accountId": "123",
                "caller": "test",
                "sourceIp": "test",
                "accessKey": "123",
                "cognitoAuthenticationProvider": null,
                "user": "test"
            },
            "domainName": "test",
            "apiId": "test"
        },
        "body": null,
        "isBase64Encoded": false
    }"#;
    
    let val: lambda_http::request::LambdaRequest = serde_json::from_str(PAYLOAD).unwrap();
    println!("{:?}", val);
}

@mbergkvist
Copy link
Contributor

@ionionascu I've created PR #387, which just do the same thing as @nmoutschen did in #371 but for multi-value headers too. I've run your test locally and it now passes.

@Crescent617
Copy link

I have the same problem in 0.4.1... Will this bug be fixed in next release?

@mbergkvist
Copy link
Contributor

#387 is merged to main branch so I think we can assume it will be included in the next release.

@ionionascu
Copy link
Author

Thank you @mbergkvist, @nmoutschen . I have tested with the latest code on master and everything looks good. I will close this issue.

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

No branches or pull requests

4 participants