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

Problem with parsing JSON requestTemplate since it is already js object #3

Closed
rallu opened this issue Feb 26, 2016 · 7 comments
Closed

Comments

@rallu
Copy link

rallu commented Feb 26, 2016

I had problem with parsing my request templates. I made this little fix to test if template is already js object:

In index.js:163

var jsonContent = requestTemplates[contentType];
let toApply = jsonContent;
if (typeof jsonContent != 'object') {
  toApply = JSON.parse(jsonContent);
}
@dherault
Copy link
Owner

You're right, requestTemplates as defined per const requestTemplates = endpoint.requestTemplates; is already an Object, so there is no need for parsing. I've been working on 1.0 (that should supports velocity in full) on master but I'll create a branch for it.

Allow 30 min, I'll publish a new version applying your fix. Make sure you've read the source (you've done it obviously) before using the --useTemplates option, it's very opinionated.

Thanks for the issue!

@rallu
Copy link
Author

rallu commented Feb 26, 2016

Yeah. I noticed that after fiddling for a while. Then I found another error in second route and ended up catching whole error:

try {
  toApply = JSON.parse(jsonContent);
} catch (e) {
  toApply = {};
}

This way it at least doesn't crash all the time.

@dherault
Copy link
Owner

I wanted to get rid of the JSON parsing, but do you think it is still necessary ?

@rallu
Copy link
Author

rallu commented Feb 26, 2016

Just get rid of it. It was nice to have something automatic for simple cases.

@dherault
Copy link
Owner

This is a fix, not amazing:

// Then we create the event object
const event = Object.assign({ isServerlessOffline: true }, request);

if (requestTemplates && this.evt.useTemplates) {
  // Apply request template to event
  const requestTemplate = requestTemplate[request.mime || 'application/json'];
  if (typeof requestTemplate === 'object') {
    try {
      // TODO: proces $context variables in a more complete way http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference
      // TODO: $input could also be dealt with in a more robust way http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#input-variable-reference
      for (let key in requestTemplate) {

        if (requestTemplate[key] === '$context.httpMethod') {
          event[key] = request.method.toUpperCase();
        } else if (requestTemplate[key] === '$input.params()') {
          event[key] = request.params;
        } else {
          const reResp = reInputParam.exec(requestTemplate[key]);
          if (reResp) {
            // lookup variable replacement in params
            const paramName = reResp[1];
            event[key] = paramName in event.params ? event.params[paramName] : '';
          }
          else {
            event[key] = requestTemplate[key];
          }
        }
      }
    }
    catch (err) {
      SCli.log('Error while trying to use your templates:');
      console.log(err.stack || err);
      serverResponse.statusCode = 500;
      serverResponse.source = 'Error while trying to use your templates.';
      serverResponse.send();
      return;
    }
  }
}

What do you think ?

@dherault
Copy link
Owner

I replaced if (typeof requestTemplate === 'object') { with if (requestTemplate) {

@dherault
Copy link
Owner

Publised under v0.2.2

kajdjakne pushed a commit to kajdjakne/serverless-offline that referenced this issue Oct 22, 2016
…t-rebased

Explained integration types in the docs
@dnalborczyk dnalborczyk mentioned this issue Jul 2, 2019
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants