From f5c229cd765f7dad821c3e41429827923f8ecb2f Mon Sep 17 00:00:00 2001 From: kokobd Date: Tue, 24 Feb 2026 19:58:10 -0500 Subject: [PATCH 1/5] MessageGroupId isn't included for non FIFO queues --- src/AWS/Lambda/Events/SQS.hs | 6 +++--- test/AWS/Lambda/Events/SQS/Spec.hs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/AWS/Lambda/Events/SQS.hs b/src/AWS/Lambda/Events/SQS.hs index 69210c1..4161bfb 100644 --- a/src/AWS/Lambda/Events/SQS.hs +++ b/src/AWS/Lambda/Events/SQS.hs @@ -16,7 +16,7 @@ module AWS.Lambda.Events.SQS ( SQSEvent (..) ) where -import Data.Aeson (FromJSON (..), withObject, (.:)) +import Data.Aeson (FromJSON (..), withObject, (.:), (.:?)) import Data.Map (Map) import Data.Text (Text) import GHC.Generics (Generic) @@ -37,7 +37,7 @@ data Attributes = Attributes { sentTimestamp :: Text, senderId :: Text, approximateFirstReceiveTimestamp :: Text, - messageGroupId :: Text + messageGroupId :: Maybe Text } deriving (Show, Eq, Generic) instance FromJSON Attributes where @@ -46,7 +46,7 @@ instance FromJSON Attributes where sentTimestamp <- v .: "SentTimestamp" senderId <- v .: "SenderId" approximateFirstReceiveTimestamp <- v .: "ApproximateFirstReceiveTimestamp" - messageGroupId <- v .: "MessageGroupId" + messageGroupId <- v .:? "MessageGroupId" pure Attributes {..} data SQSEvent = SQSEvent { diff --git a/test/AWS/Lambda/Events/SQS/Spec.hs b/test/AWS/Lambda/Events/SQS/Spec.hs index 156ae7e..b05fad9 100644 --- a/test/AWS/Lambda/Events/SQS/Spec.hs +++ b/test/AWS/Lambda/Events/SQS/Spec.hs @@ -54,7 +54,7 @@ expectedRecords = Records , sentTimestamp = "1523232000000" , senderId = "123456789012" , approximateFirstReceiveTimestamp = "1523232000001" - , messageGroupId = "group-1" + , messageGroupId = Just "group-1" } , messageAttributes = M.fromList [ ("attribute1", "value1") From 853a858ad508e6eb14ae90d9e6e4d3e4b9692128 Mon Sep 17 00:00:00 2001 From: kokobd Date: Mon, 16 Mar 2026 20:57:27 -0500 Subject: [PATCH 2/5] fix messageAttributes type of SQSEvent --- src/AWS/Lambda/Events/SQS.hs | 36 +++++++++++++++++++++++++----- test/AWS/Lambda/Events/SQS/Spec.hs | 34 ++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/AWS/Lambda/Events/SQS.hs b/src/AWS/Lambda/Events/SQS.hs index 4161bfb..ebcac5d 100644 --- a/src/AWS/Lambda/Events/SQS.hs +++ b/src/AWS/Lambda/Events/SQS.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE ApplicativeDo #-} +{-# LANGUAGE ApplicativeDo #-} {-# LANGUAGE RecordWildCards #-} {-| @@ -13,13 +13,17 @@ Stability : stable module AWS.Lambda.Events.SQS ( Records (..), Attributes (..), + MessageAttributeValue (..), SQSEvent (..) ) where -import Data.Aeson (FromJSON (..), withObject, (.:), (.:?)) -import Data.Map (Map) -import Data.Text (Text) -import GHC.Generics (Generic) +import Data.Aeson (FromJSON (..), withObject, (.:), (.:?)) +import Data.ByteString (ByteString) +import qualified Data.ByteString.Base64 as B64 +import Data.Map (Map) +import Data.Text (Text) +import qualified Data.Text.Encoding as TE +import GHC.Generics (Generic) -- | Represents an event from AWS SQS. -- @@ -49,12 +53,29 @@ instance FromJSON Attributes where messageGroupId <- v .:? "MessageGroupId" pure Attributes {..} +data MessageAttributeValue = MessageAttributeValue { + stringValue :: Maybe Text, + binaryValue :: Maybe ByteString, + stringListValues :: [Text], + binaryListValues :: [ByteString], + dataType :: Text +} deriving (Show, Eq, Generic) + +instance FromJSON MessageAttributeValue where + parseJSON = withObject "MessageAttributeValue" $ \v -> do + stringValue <- v .:? "stringValue" + binaryValue <- fmap decodeBase64Text <$> v .:? "binaryValue" + stringListValues <- maybe [] id <$> v .:? "stringListValues" + binaryListValues <- maybe [] (map decodeBase64Text) <$> v .:? "binaryListValues" + dataType <- v .: "dataType" + pure MessageAttributeValue {..} + data SQSEvent = SQSEvent { messageId :: Text, receiptHandle :: Text, body :: Text, attributes :: Attributes, - messageAttributes :: Map Text Text, + messageAttributes :: Map Text MessageAttributeValue, md5OfBody :: Text, eventSource :: Text, eventSourceARN :: Text, @@ -62,3 +83,6 @@ data SQSEvent = SQSEvent { } deriving (Show, Eq, Generic) instance FromJSON SQSEvent + +decodeBase64Text :: Text -> ByteString +decodeBase64Text = B64.decodeLenient . TE.encodeUtf8 diff --git a/test/AWS/Lambda/Events/SQS/Spec.hs b/test/AWS/Lambda/Events/SQS/Spec.hs index b05fad9..293820c 100644 --- a/test/AWS/Lambda/Events/SQS/Spec.hs +++ b/test/AWS/Lambda/Events/SQS/Spec.hs @@ -30,8 +30,18 @@ samplePayload = [r| "MessageGroupId": "group-1" }, "messageAttributes": { - "attribute1": "value1", - "attribute2": "value2" + "attribute1": { + "stringValue": "value1", + "stringListValues": [], + "binaryListValues": [], + "dataType": "String" + }, + "attribute2": { + "binaryValue": "dmFsdWUy", + "stringListValues": [], + "binaryListValues": [], + "dataType": "Binary" + } }, "md5OfBody": "9a0364b9e99bb480dd25e1f0284c8555", "eventSource": "aws:sqs", @@ -57,8 +67,24 @@ expectedRecords = Records , messageGroupId = Just "group-1" } , messageAttributes = M.fromList - [ ("attribute1", "value1") - , ("attribute2", "value2") + [ ( "attribute1" + , MessageAttributeValue + { stringValue = Just "value1" + , binaryValue = Nothing + , stringListValues = [] + , binaryListValues = [] + , dataType = "String" + } + ) + , ( "attribute2" + , MessageAttributeValue + { stringValue = Nothing + , binaryValue = Just "value2" + , stringListValues = [] + , binaryListValues = [] + , dataType = "Binary" + } + ) ] , md5OfBody = "9a0364b9e99bb480dd25e1f0284c8555" , eventSource = "aws:sqs" From 23b4bd6c766de84c7323d19bf6b588329d1cb82b Mon Sep 17 00:00:00 2001 From: kokobd Date: Tue, 17 Mar 2026 17:05:23 -0500 Subject: [PATCH 3/5] add aws doc URL --- src/AWS/Lambda/Events/SQS.hs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/AWS/Lambda/Events/SQS.hs b/src/AWS/Lambda/Events/SQS.hs index ebcac5d..22cb9ed 100644 --- a/src/AWS/Lambda/Events/SQS.hs +++ b/src/AWS/Lambda/Events/SQS.hs @@ -53,6 +53,10 @@ instance FromJSON Attributes where messageGroupId <- v .:? "MessageGroupId" pure Attributes {..} +-- | An SQS message attribute value as it appears in Lambda SQS event payloads. +-- +-- See the +-- for the JSON shape used under @messageAttributes@. data MessageAttributeValue = MessageAttributeValue { stringValue :: Maybe Text, binaryValue :: Maybe ByteString, From b680faf9097569bfd02244ceb2241fcefe62aab2 Mon Sep 17 00:00:00 2001 From: kokobd Date: Tue, 17 Mar 2026 17:06:50 -0500 Subject: [PATCH 4/5] add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index babeffc..65c88c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog][chg] and this project adheres to ## Unreleased - Add `messageGroupId` to SQS Attributes. + - Fix the type of `messageAttributes` in `SQSEvent`. ## `1.1` - 2023-12-18 From b1a004cd14fee3fc6da891f389b38a8ba4e11753 Mon Sep 17 00:00:00 2001 From: Hugh Davidson <32408698+Tristano8@users.noreply.github.com> Date: Mon, 22 Jun 2026 16:14:41 +1000 Subject: [PATCH 5/5] fix: handle null header values --- CHANGELOG.md | 1 + src/AWS/Lambda/Events/Kafka.hs | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65c88c0..eb8459c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog][chg] and this project adheres to ## Unreleased - Add `messageGroupId` to SQS Attributes. - Fix the type of `messageAttributes` in `SQSEvent`. + - Fix header parsing to accept `null` values serialised as `{}` ## `1.1` - 2023-12-18 diff --git a/src/AWS/Lambda/Events/Kafka.hs b/src/AWS/Lambda/Events/Kafka.hs index 38d287c..0fab239 100644 --- a/src/AWS/Lambda/Events/Kafka.hs +++ b/src/AWS/Lambda/Events/Kafka.hs @@ -174,10 +174,15 @@ instance FromJSON (Record' ByteString) where partition <- o .: "partition" offset <- o .: "offset" timestamp <- parseTimestamp o - headers <- o .: "headers" + headers <- o .: "headers" >>= fmap catMaybes . traverse parseNullableHeader key <- fmap (B64.decodeLenient . TE.encodeUtf8) <$> o .:? "key" value <- fmap (B64.decodeLenient . TE.encodeUtf8) <$> o .:? "value" pure Record { topic, partition, offset, timestamp, headers, key, value } + where + -- AWS serialises Kafka headers with null values as {}, which has no + -- key to extract. Skip these rather than failing. + parseNullableHeader (Object o) | KM.null o = pure Nothing + parseNullableHeader v = Just <$> parseJSON v -- | Encodes keys and values into base64. instance ToJSON (Record' ByteString) where