admin管理员组

文章数量:1379395

I have the following code which takes a POST input and writes the data in a DynamoDB table. It works if I don't add the validation to check if the key:value pair exists. Here is the code:

    const payeeProfileIntegration = new AwsIntegration({
      service: "dynamodb",
      action: "UpdateItem",
      options: {
          credentialsRole: apiGatewayRole,
          requestTemplates: {
            "application/json": `
{
  "TableName": "${dynamoDb.tableName}",
  "Key": {
    "id": { "S": "$input.path('$.id')" }
  },
  "UpdateExpression": "SET #Status = :status" 
  #if($input.path('$.dob') != "")
    ,"#DOB = :dob"
  #end
  #if($input.path('$.nationality') != "")
    ,"#Nationality = :nationality"
  #end
  #if($input.path('$pany_reg_number') != "")
      ,"#CompanyRegNumber = :companyRegNumber"
  #end
  #if($input.path('$.incorporation_country') != "")
    ,"#IncCountry = :incCountry"
  #end
  #if($input.path('$.address') != "")
    ,"#Address = :address"
  #end
  ,
  "ExpressionAttributeNames": {
    "#DOB": "DOB",
    "#Nationality": "Nationality",
    "#Status": "Status",
    "#Address": "Postal Address",
    "#CompanyRegNumber": "CompanyRegistrationNumber",
    "#IncCountry": "IncorporationCountry"
  },
  "ExpressionAttributeValues": {
    ":status": { "S": "VERIFYING" }
    #if($input.path('$.dob') != "")
      ,":dob": { "S": "$input.path('$.dob')" }
    #end
    #if($input.path('$.nationality') != "")
      ,":nationality": { "S": "$input.path('$.nationality')" }
    #end
    #if($input.path('$pany_reg_number') != "")
      ,":companyRegNumber": { "S": "$input.path('$pany_reg_number')" }
    #end
    #if($input.path('$.incorporation_country') != "")
      ,":incCountry": { "S": "$input.path('$.incorporation_country')" }
    #end
    #if($input.path('$.address') != "")
      ,":address": {
        "M": {
          "Address": { "S": "$input.path('$.address.address')" },
          #if($input.path('$.address.address1') != "")
            ,"Address1": { "S": "$input.path('$.address.address1')" }
          #end
          #if($input.path('$.address.address2') != "")
            ,"Address2": { "S": "$input.path('$.address.address2')" }
          #end
          "City": { "S": "$input.path('$.address.city')" },
          "Region": { "S": "$input.path('$.address.region')" },
          "Post Code": { "S": "$input.path('$.address.post_code')" },
          "Country": { "S": "$input.path('$.address.country')" }
        }
      }
    #end
  },
  "ReturnValues": "UPDATED_NEW"
}
  `
          },
          integrationResponses: [
            {
              statusCode: "200",
              responseTemplates: {
                "application/json": `{"message": "Profile updated successfully", "updatedAttributes": $input.json('$.Attributes')}`
              }
            },
            {
              statusCode: "400",
              selectionPattern: ".*SerializationException.*",
              responseTemplates: {
                "application/json": `{"message": "Invalid request data", "error": "$input.path('$.__type')"}`
              }
            },
            {
              statusCode: "500",
              selectionPattern: ".*InternalServerError.*",
              responseTemplates: {
                "application/json": `{"message": "Internal server error"}`
              }
            }
          ]
        }
    });
      
    const payeeProfileResource = payeeOnBoardingResource.addResource("profile");
    payeeProfileResource.addMethod("POST", payeeProfileIntegration, {
      methodResponses: [
        { statusCode: "200" },
        { statusCode: "400" },
        { statusCode: "500" }
      ]
    });

When testing the above, I get

Wed Mar 19 10:11:43 UTC 2025 : Endpoint response body before transformations: {"__type":"com.amazon.coral.service#SerializationException"}

If I remove all the #if #end checks, the data is written to DynamoDB but it adds Empty Value for the fields that haven't been included in the payload.

Any idea as to how I can exclude fields which are not in the initial request?

本文标签: aws api gatewayAwsIntegration write to DynamoDB SerializationExceptionStack Overflow