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

Request validation and lambda integration #676

Closed
douwewermer opened this issue May 29, 2019 · 5 comments
Closed

Request validation and lambda integration #676

douwewermer opened this issue May 29, 2019 · 5 comments

Comments

@douwewermer
Copy link

@douwewermer douwewermer commented May 29, 2019

I run into an issue when using request validation in combination with lambda integration. When deploying to AWS all seems to works fine, but when testing with serverless-offline, I get the following error when making a request.

{
    "errorMessage": "Invalid request body for 'createUser' handler",
    "errorType": "SyntaxError",
    "stackTrace": [
        "SyntaxError: Unexpected token u in JSON at position 0",
        "at JSON.parse (<anonymous>)",
        "at Object.validate (/home/me/projects/apitest/node_modules/serverless-offline/src/requestBodyValidator.js:18:35)",
        "at handler (/home/me/projects/apitest/node_modules/serverless-offline/src/index.js:949:38)",
        "at Object.internals.handler (/home/me/projects/apitest/node_modules/hapi/lib/handler.js:101:51)",
        "at request._protect.run (/home/me/projects/apitest/node_modules/hapi/lib/handler.js:32:23)",
        "at module.exports.internals.Protect.internals.Protect.run (/home/me/projects/apitest/node_modules/hapi/lib/protect.js:60:12)",
        "at exports.execute (/home/me/projects/apitest/node_modules/hapi/lib/handler.js:26:22)",
        "at each (/home/me/projects/apitest/node_modules/hapi/lib/request.js:401:16)",
        "at iterate (/home/me/projects/apitest/node_modules/items/lib/index.js:36:13)",
        "at done (/home/me/projects/apitest/node_modules/items/lib/index.js:28:25)",
        "at internals.Auth.payload (/home/me/projects/apitest/node_modules/hapi/lib/auth.js:235:16)",
        "at each (/home/me/projects/apitest/node_modules/hapi/lib/request.js:401:16)",
        "at iterate (/home/me/projects/apitest/node_modules/items/lib/index.js:36:13)",
        "at done (/home/me/projects/apitest/node_modules/items/lib/index.js:28:25)",
        "at onParsed (/home/me/projects/apitest/node_modules/hapi/lib/route.js:341:20)",
        "at Subtext.parse (/home/me/projects/apitest/node_modules/hapi/lib/route.js:380:20)",
        "at next (/home/me/projects/apitest/node_modules/subtext/lib/index.js:46:26)",
        "at Wreck.read (/home/me/projects/apitest/node_modules/subtext/lib/index.js:243:16)",
        "at finish (/home/me/projects/apitest/node_modules/wreck/lib/index.js:374:20)",
        "at wrapped (/home/me/projects/apitest/node_modules/wreck/node_modules/hoek/lib/index.js:879:20)",
        "at module.exports.internals.Recorder.onReaderFinish (/home/me/projects/apitest/node_modules/wreck/lib/index.js:449:16)",
        "at Object.onceWrapper (events.js:281:20)",
        "at module.exports.internals.Recorder.emit (events.js:198:15)",
        "at module.exports.internals.Recorder.EventEmitter.emit (domain.js:504:23)",
        "at finishMaybe (_stream_writable.js:646:14)",
        "at endWritable (_stream_writable.js:663:3)",
        "at module.exports.internals.Recorder.Writable.end (_stream_writable.js:594:5)",
        "at IncomingMessage.onend (_stream_readable.js:667:10)",
        "at Object.onceWrapper (events.js:281:20)",
        "at IncomingMessage.emit (events.js:198:15)",
        "at IncomingMessage.EventEmitter.emit (domain.js:481:20)",
        "at endReadableNT (_stream_readable.js:1139:12)"
    ],
    "offlineInfo": "If you believe this is an issue with the plugin please submit it, thanks. https://github.com/dherault/serverless-offline/issues"
}

It seems like the function validate in requestBodyValidatior.js tries to parse the body using JSON.parse, but when using lambda integration instead of lambda-proxy integration the body already is an object.

@dnalborczyk

This comment has been minimized.

Copy link
Collaborator

@dnalborczyk dnalborczyk commented Jun 3, 2019

@douwewermer could you provide some repro steps, or better, a small repro repository to illustrate your issue?

@dnalborczyk

This comment has been minimized.

Copy link
Collaborator

@dnalborczyk dnalborczyk commented Jun 4, 2019

@douwewermer feel free to re-open when you have some repro steps or an repro repository. thanks!

@dnalborczyk dnalborczyk closed this Jun 4, 2019
@douwewermer

This comment has been minimized.

Copy link
Author

@douwewermer douwewermer commented Jun 6, 2019

Hi, thanks for your reply!
I made a small sample project which demonstrates the issue:
https://github.com/douwewermer/serverless-example
See README.md for a more elaborate description on the issue and instructions on how to reproduce the error.

@dherault dherault reopened this Jun 6, 2019
@dnalborczyk

This comment has been minimized.

Copy link
Collaborator

@dnalborczyk dnalborczyk commented Jul 6, 2019

hey @douwewermer thank you for the repository! I was able to repro. You are right for the reasons of JSON.parse on an object.

the way I see it is that this feature should not exist the way it was implemented, and should probably be removed.

the way I see it is that the implementation is based on another plugin: https://github.com/deliveryhero/serverless-aws-documentation which uses the custom documentation type.

I believe your repository model is also never going to be deployed to aws anyways as is, for the same reason it's just a custom variable.

now, model validation was added to serverless in March: serverless/serverless#5956

@dherault I think we should remove this and eventually re-implement it.

update: @douwewermer you can read more about this in the PR, just found it with the github history: #589

@dnalborczyk

This comment has been minimized.

Copy link
Collaborator

@dnalborczyk dnalborczyk commented Jul 6, 2019

gonna close this in favor of: #734

@dnalborczyk dnalborczyk closed this Jul 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.