Reputation: 18905
I'm following along: Using Amazon API Gateway as a proxy for DynamoDB
Taking the blog's example, I want to tighten what and how data is written to a dynamodb table frontend by Api Gateway. To this end I define the following mapping in Api Gateway:
{
"TableName": "Comments",
"Item": {
"commentId": {
"S": "$context.requestId"
},
"pageId": {
"S": "$input.path('$.pageId')"
},
"userName": {
"S": "$input.path('$.userName')"
},
"message": {
"S": "$input.path('$.message')"
}
}
}
Following along, testing with the following example works just fine:
{
"pageId": "breaking-news-story-01-18-2016",
"userName": "Just Saying Thank You",
"message": "I really enjoyed this story!!"
}
However, let's say I want to keep the same mapping as above but want to make message
optional. How would I do that? I can't get it to work. I've tried:
message
-attribute. --> "One or more parameter values were invalid: An AttributeValue may not contain an empty string"
2 using the above mapping as is, but sending a body with message=null
. --> "One or more parameter values were invalid: An AttributeValue may not contain an empty string"mapping
-> passing a body now without message
succeeds obviously. However, sending a body with message
doesn't pass message
through (which is what I expected, but wanted to exhaust all options) Obviously I could use AWS lambda instead to do the mapping but this feels like such a common use-case, i.e.: optional attributes, that this must be possible directly in Api Gateway.
Upvotes: 1
Views: 1706
Reputation: 18905
Courtesy of @ka:
Explatation: APi gateway auto-populates non-existing attributes with empty strings or something. This needs to be excluded from the if.
Also notice the critical ,
right after the if statement.
{
"TableName": "Comments",
"Item": {
"commentId": {
"S": "$context.requestId"
},
"pageId": {
"S": "$input.path('$.pageId')"
},
"userName": {
"S": "$input.path('$.userName')"
}#if($input.path('$.description') && $input.path('$.message') != ""),
"message": {
"S": "$input.path('$.message')"
}
#end
}
}
Upvotes: 2
Reputation: 6515
Would you mind trying this mapping template? If the message is NULL, it will not be added to the body.
{
"TableName": "Comments",
"Item": {
"commentId": {
"S": "$context.requestId"
},
"pageId": {
"S": "$input.path('$.pageId')"
},
"userName": {
"S": "$input.path('$.userName')"
}#if ($.message),
"message": {
"S": "$input.path('$.message')"
}
#end
}
}
Thanks, -Ka Hou
Upvotes: 0