Skip to content

Commit

Permalink
fix: lambda integration returning object with body (#1547)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk authored Aug 18, 2022
1 parent 4f6b1e6 commit b053f57
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
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

0 comments on commit b053f57

Please sign in to comment.