Velocity 模板将无效 JSON 解析为有效 JSON
Velocity template parses invalid JSON as valid JSON
我有以下模板,它的工作方式符合我的预期:
#set($allParams = $input.params())
{
"body" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
#set($params = $allParams.get($type))
"$type" : {
#foreach($paramName in $params.keySet())
"$paramName" : "$util.escapeJavaScript($params.get($paramName))"#if($foreach.hasNext),#end
#end
}#if($foreach.hasNext),#end
#end
}
}
困扰我的是这个模板还是会解析无效JSON.
例如,在转换前给定以下无效 JSON:
{
"example": !@#$%^&*()_+
}
使用上面的模板会将其转换为:
{
"body" : {
"asdasd": "!@#$%^&*()"
},
...
}
我的问题是为什么? $input.json('$')
不应该无法解析无效的 JSON 字符串吗?
编辑后的日志如下:
Execution log for request test-request
Fri Oct 06 21:27:13 UTC 2017 : Starting execution for request: test-invoke-request
Fri Oct 06 21:27:13 UTC 2017 : HTTP Method: POST, Resource Path: /equipment
Fri Oct 06 21:27:13 UTC 2017 : Method request path: {}
Fri Oct 06 21:27:13 UTC 2017 : Method request query string: {}
Fri Oct 06 21:27:13 UTC 2017 : Method request headers: {}
Fri Oct 06 21:27:13 UTC 2017 : Method request body before transformations: {
"asdasd": 123123$%^&*()
}
Fri Oct 06 21:27:13 UTC 2017 : Endpoint request body after transformations: {
"body" : {"asdasd":"123123$%^&*()"},
"params" : {
....
我能够通过使用请求正文模型解决我的问题:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Example",
"type": "object",
"required": [
"example"
],
"properties": {
"example": {
"type":"string"
}
}
}
因此 API 网关将根据上面的 JSON 模式模型检查请求正文 在 将其发送到 Lambda 之前。
我有以下模板,它的工作方式符合我的预期:
#set($allParams = $input.params())
{
"body" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
#set($params = $allParams.get($type))
"$type" : {
#foreach($paramName in $params.keySet())
"$paramName" : "$util.escapeJavaScript($params.get($paramName))"#if($foreach.hasNext),#end
#end
}#if($foreach.hasNext),#end
#end
}
}
困扰我的是这个模板还是会解析无效JSON.
例如,在转换前给定以下无效 JSON:
{
"example": !@#$%^&*()_+
}
使用上面的模板会将其转换为:
{
"body" : {
"asdasd": "!@#$%^&*()"
},
...
}
我的问题是为什么? $input.json('$')
不应该无法解析无效的 JSON 字符串吗?
编辑后的日志如下:
Execution log for request test-request
Fri Oct 06 21:27:13 UTC 2017 : Starting execution for request: test-invoke-request
Fri Oct 06 21:27:13 UTC 2017 : HTTP Method: POST, Resource Path: /equipment
Fri Oct 06 21:27:13 UTC 2017 : Method request path: {}
Fri Oct 06 21:27:13 UTC 2017 : Method request query string: {}
Fri Oct 06 21:27:13 UTC 2017 : Method request headers: {}
Fri Oct 06 21:27:13 UTC 2017 : Method request body before transformations: {
"asdasd": 123123$%^&*()
}
Fri Oct 06 21:27:13 UTC 2017 : Endpoint request body after transformations: {
"body" : {"asdasd":"123123$%^&*()"},
"params" : {
....
我能够通过使用请求正文模型解决我的问题:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Example",
"type": "object",
"required": [
"example"
],
"properties": {
"example": {
"type":"string"
}
}
}
因此 API 网关将根据上面的 JSON 模式模型检查请求正文 在 将其发送到 Lambda 之前。