diff --git a/src/events/http/HttpServer.js b/src/events/http/HttpServer.js index 68af67886..89dbc6fed 100644 --- a/src/events/http/HttpServer.js +++ b/src/events/http/HttpServer.js @@ -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 } @@ -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', diff --git a/tests/integration/lambda-integration/lambdaIntegration.test.js b/tests/integration/lambda-integration/lambdaIntegration.test.js index 789940d42..4773bf7cb 100644 --- a/tests/integration/lambda-integration/lambdaIntegration.test.js +++ b/tests/integration/lambda-integration/lambdaIntegration.test.js @@ -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({ diff --git a/tests/integration/lambda-integration/serverless.yml b/tests/integration/lambda-integration/serverless.yml index 151f9c683..125cf2201 100644 --- a/tests/integration/lambda-integration/serverless.yml +++ b/tests/integration/lambda-integration/serverless.yml @@ -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: diff --git a/tests/integration/lambda-integration/src/handler.js b/tests/integration/lambda-integration/src/handler.js index ca0f949bf..79ba434b4 100644 --- a/tests/integration/lambda-integration/src/handler.js +++ b/tests/integration/lambda-integration/src/handler.js @@ -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({