为什么这个 AWS API 网关映射模板没有输出正确的变量?

Why does this AWS API Gateway Mapping Template not output the correct variable?

我正在使用 AWS API 网关。我有来自后端的响应,我正在尝试使用映射模板将响应映射到不同的输出。

我正在尝试 return 每个 hits.hits._source.display 元素。但是映射模板 return 是在末尾添加了文字“_source.display”字符串的整个对象。

原始未修改的服务器响应就像-

{
   "took":7,
   "timed_out":false,
   "_shards":{
      "total":1,
      "successful":1,
      "skipped":0,
      "failed":0
   },
   "hits":{
      "total":{
         "value":10000,
         "relation":"gte"
      },
      "max_score":1.0,
      "hits":[
         {
            "_index":"address",
            "_type":"_doc",
            "_id":"GAVIC411064535",
            "_score":1.0,
            "_source":{
               "id":"GAVIC411064535",
               "display":"1/28 George Street, Casterton VIC 3311",
               "location":{
                  "lat":-37.59205672,
                  "lon":141.38825665
               },
               "component":{
                  "buildingName":"",
                  "number":"1/28",
                  "street":"George Street",
                  "locality":"Casterton",
                  "state":"VIC",
                  "postcode":"3311"
               }
            }
         },
         {
            "_index":"address",
            "_type":"_doc",
            "_id":"GAVIC411066597",
            "_score":1.0,
            "_source":{
               "id":"GAVIC411066597",
               "display":"52 St Georges Road, Corio VIC 3214",
               "location":{
                  "lat":-37.59205672,
                  "lon":141.38825665
               },
               "component":{
                  "buildingName":"",
                  "number":"52",
                  "street":"St Georges Road",
                  "locality":"Corio ",
                  "state":"VIC",
                  "postcode":"3214"
               }
            }
         },

到目前为止,我的映射模板如下所示 -

#set($inputRoot = $input.path('$'))
{
#foreach($elem in $inputRoot.hits.hits)
    {
      "address": $elem._source.display,
    }#if($foreach.hasNext),#end
        
#end
}

结果输出如下所示。您可以看到它返回了整个 $elem 而不是 returning 我要求的属性。即._source.display

它也字面打印了._source.display到最后。

{
  "address": {_index=address, _type=_doc, _id=GAVIC411064535, _score=1.0, _source={id=GAVIC411064535, display=1/28 George Street, Casterton VIC 3311, location={lat=-37.59205672, lon=141.38825665}, component={buildingName=, number=1/28, street=George Street, locality=Casterton, state=VIC, postcode=3311}}}._source.display,
},      
{
  "address": {_index=address, _type=_doc, _id=GAVIC411066597, _score=1.0, _source={id=GAVIC411066597, display=52 St Georges Road, Corio VIC 3214, location={lat=-37.59205672, lon=141.38825665}, component={buildingName=, number=52, street=Georges Road, locality=Corio , state=VIC, postcode=3214}}}._source.display,
},

为简洁起见,我截断了服务器响应和映射模板输出。

期望的映射模板输出为-

{
   "address" : "28 George Street, Casterton VIC 3311",
   "address" : "52 St Georges Road, Corio VIC 3214"
}

这是 2.x 版本中的 bug in version 1.7 which has been fixed(2016 年 7 月...AWS 有时确实应该升级其库)。

您可以像这样解决此错误:

#foreach($elem in $inputRoot.hits.hits)
    {
      "address": "$elem.get('_source').display"
    }#if($foreach.hasNext),#end
        
#end