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

fix: lambda integration returning object with body #1547

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/events/http/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,12 +788,6 @@ export default class HttpServer {
response.variety = 'buffer'
} else if (typeof result === 'string') {
response.source = stringify(result)
} else if (result && result.body && typeof result.body !== 'string') {
return this.#reply502(
response,
'According to the API Gateway specs, the body content must be stringified. Check your Lambda response and make sure you are invoking JSON.stringify(YOUR_CONTENT) on your body object',
{},
)
} else {
response.source = result
}
Expand Down Expand Up @@ -888,6 +882,7 @@ export default class HttpServer {
response.variety = 'buffer'
} else {
if (result && result.body && typeof result.body !== 'string') {
// FIXME TODO we should probably just write to console instead of returning a payload
return this.#reply502(
response,
'According to the API Gateway specs, the body content must be stringified. Check your Lambda response and make sure you are invoking JSON.stringify(YOUR_CONTENT) on your body object',
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/lambda-integration/lambdaIntegration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ describe('lambda integration tests', function desc() {
status: 200,
},

// https://github.com/dherault/serverless-offline/issues/1502
{
description: 'should return JSON',
expected: {
body: {
foo: 'bar',
},
statusCode: 200,
},
path: '/dev/lambda-integration-json-with-body',
status: 200,
},

{
description: 'should return stringified JSON',
expected: stringify({
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/lambda-integration/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ functions:
path: '/lambda-integration-json'
handler: src/handler.lambdaIntegrationJson

integrationJsonWithBody:
events:
- http:
integration: lambda
method: get
path: '/lambda-integration-json-with-body'
handler: src/handler.lambdaIntegrationJsonWithBody

integrationStringified:
events:
- http:
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/lambda-integration/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ exports.lambdaIntegrationJson = async function lambdaIntegrationJson() {
}
}

exports.lambdaIntegrationJsonWithBody =
async function lambdaIntegrationJsonWithBody() {
return {
body: {
foo: 'bar',
},
statusCode: 200,
}
}

exports.lambdaIntegrationStringified =
async function lambdaIntegrationStringified() {
return stringify({
Expand Down