Specifying Numbers in VTL on AMS API Gateway

↘锁芯ラ 提交于 2020-01-07 03:20:25

问题


Doc Ref: http://docs.aws.amazon.com/apigateway/latest/developerguide/models-mappings.html

In AMS VTL one specifies dictionary fields in a model schema thus:

"field1" : {"type":"string"},
"field2" : {"type":"number"},

and so a mapping template can populate such fields thus:

#set($inputRoot = $input.path('$'))
"questions" :
[
#foreach($elem in $inputRoot)
{
  "field1" : "$elem.field1",
  "field2" : $elem.field2
}#if($foreach.hasNext),#end
#end
]

However... my iOS app complains the received data isn't in JSON format. If I add quotes around $elem.field2 then iOS accepts the JSON and converts all fields to strings.

My Lambda function is returning is returning a standard JSON list of dictionaries with field2 defined as an integer.

But APIG returns strings for all my fields, delimited with {} and a prefix:

{S=some text}
{N=10000000500}

So I can see that field2 isn't a number but a string {N=10000000500}.

How do I handle numbers in this system?


回答1:


Undocumented but you can simply specify the type after the field name in a mapping template:

#set($inputRoot = $input.path('$'))
"questions" :
[
#foreach($elem in $inputRoot)
{
  "field1" : "$elem.field1.S",
  "field2" : $elem.field2.N
}#if($foreach.hasNext),#end
#end
]

Note that string fields need to be delimited in quotes.



来源:https://stackoverflow.com/questions/35743522/specifying-numbers-in-vtl-on-ams-api-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!