Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON using VTL different between serverless on API Gateway vs offline #50

Closed
alshamma opened this issue May 31, 2016 · 7 comments
Closed

Comments

@alshamma
Copy link

I am using this construct in my s-templates.json as part of my requestParameters:

    "params": "{#foreach($param in $input.params().path.keySet())\"$param\": \"$util.escapeJavaScript($input.params().path.get($param))\" #if($foreach.hasNext),#end#end}",

When I call my API from API Gateway, my Lambda receives the params as a string, and I have to call JSON.parse on the string.

When I call my API from serverless-offline, my Lambda receives the params as an object (which I actually prefer).

Would be great if both returned the same response, which I guess means offline should match API Gateway/Lambda.

@dherault
Copy link
Owner

dherault commented Jun 1, 2016

Hi @alshamma, you've just pointed out the plugin's main issue! This is due to the difference of implementation between the Java Velocity parser AWS uses and the JavaScript Velocity parser we use (an external library).

To fix your issue one should call JSON.stringify on each velocity output, put this raises issues too.

I'll keep this one open as a reminder.

@dherault dherault added the bug label Jun 1, 2016
@adambiggs
Copy link

adambiggs commented Jun 3, 2016

I just ran into this issue as well. #if() statements seem to act differently between AWS/Offline.

Assuming $elem.value is undefined, the following works offline:

#if($elem.value)
  Value is defined. 
#end

But on AWS the text is still rendered...

Then I tried:

#if($elem.value != $null)
  Value is defined. 
#end

Which works on AWS, but throws an error offline...

I also tried:

#if($elem.value != null)
  Value is defined. 
#end

Which worked offline but not on AWS.

@adambiggs
Copy link

I found a workaround on this page that seems to work the same between AWS/Offline:

#if("$!elem.value" != "")
  Value is defined. 
#end

But this is kind of ugly...

@asierba
Copy link

asierba commented Apr 3, 2017

+1
We run to an issue when we deployed our lambda to aws. It took us a while to realise that we weren't wrapping the response's body with JSON.stringify - the logs in aws didn't tell us anything and everything works locally with serverless offline.
At the moment we have to remember to use JSON.stringify every time, which is not ideal.

@alireza8101
Copy link

any update?

@dnalborczyk
Copy link
Collaborator

@alireza8101 do you have some repro code about the issue?

@alireza8101
Copy link

@alireza8101 do you have some repro code about the issue?

unfortunately no

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants