swift-server / swift-aws-lambda-runtime

Swift implementation of AWS Lambda Runtime
Apache License 2.0
1.12k stars 101 forks source link

How to fix newline in input causing Swift AWS lambda to fail in localhost testing #280

Closed alextaz closed 1 year ago

alextaz commented 1 year ago

I posted this on stackoverflow earlier today - https://stackoverflow.com/questions/75000142/how-to-fix-newline-in-input-causing-swift-aws-lambda-to-fail-in-localhost-testin

but just now thought of submitting an issue here

If my Lambda input is a String, why do I get an error when my string contains newline \n \r etc

Expected behavior

my lambda to run with no issues

Actual behavior

I got this error in the HTTP response

{"errorType":"FunctionError", "errorMessage": "requestDecoding(Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: \"The given data was not valid JSON.\", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 \"Unescaped control character around line 1, column 2.\" UserInfo={NSDebugDescription=Unescaped control character around line 1, column 2., NSJSONSerializationErrorIndex=2}))))"}

Steps to reproduce

  1. Create HelloWorld project that takes a String as input similar to this one Lambda.run { (context, payload: String, completion: @escaping (Result<String, Error>) -> Void) in
  2. Run in Xcode to start localhost server
  3. Using PostMan send the following in the payload "hello world"

and you get the error

if there is no newline, then there is no error

If possible, minimal yet complete reproducer code (or URL to code)

Lambda.run { (context, payload: String, completion: @escaping (Result<String, Error>) -> Void) in completion(.success("Hello, (payload)")) }

SwiftAWSLambdaRuntime version/commit hash

0.5.2 / dc64ce195b1c51356f6655935c3509e296c35696

Swift & OS version (output of swift --version && uname -a)

swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51) Target: x86_64-apple-macosx13.0

Darwin 22.1.0 Darwin Kernel Version 22.1.0: Sun Oct 9 20:14:54 PDT 2022; root:xnu-8792.41.9~2/RELEASE_X86_64 x86_64

tomerd commented 1 year ago

The runtime implementation is largely based on JSON payloads and newline is not valid in JSON. you could escape the newline to make this work.

alextaz commented 1 year ago

Thanks for the information, will work on escaping the newline to make this work.