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

System.FormatException when MessageAttributes contains type number #1556

Closed
codrinandronic opened this issue Nov 6, 2024 · 1 comment · Fixed by #1557
Closed

System.FormatException when MessageAttributes contains type number #1556

codrinandronic opened this issue Nov 6, 2024 · 1 comment · Fixed by #1557
Assignees
Labels
Milestone

Comments

@codrinandronic
Copy link

Describe the bug
When an SQS message contains MessageAttributes with Type Number, an exception of type System.FormatException is thrown when the message is picked up from the queue.

Steps To reproduce
Send an SQS message coining the following MessageAttributes:
"MessageAttributes" : {
"TotalValue" : {"Type":"Number","Value":"100"}
}

Expected behaviour
Message should be picked up with no exception.

Actual behaviour
The following exception is thrown:
System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
at JustSaying.Messaging.MessageSerialization.NewtonsoftSerializer.GetMessageAttributes(String message) in //src/JustSaying/Messaging/MessageSerialization/NewtonsoftSerializer.cs:line 54
at JustSaying.Messaging.MessageSerialization.MessageSerializationRegister.DeserializeMessage(String body) in /
/src/JustSaying/Messaging/MessageSerialization/MessageSerializationRegister.cs:line 45
at JustSaying.AwsTools.MessageHandling.Dispatch.MessageDispatcher.DeserializeMessage(IQueueMessageContext messageContext, CancellationToken cancellationToken) in /_/src/JustSaying/AwsTools/MessageHandling/Dispatch/MessageDispatcher.cs:line 79

System information:

  • OS: Windows 11
  • Library Version 7.0.0 and above
  • .NET version 8

Additional context
Both SystemTextJsonSerializer and NewtonsoftSerializer implementation of GetMessageAttributes() handle only String data types. Everything else is considered Binary. But AWS documentation specifies that Message Attributes can have 4 data types: string, number, binary and custom which should be handled.

@martincostello
Copy link
Member

From MessageAttributeValue.DataType:

Amazon SQS supports the following logical data types: String, Number,
and Binary. For the Number data type, you must use StringValue.

We need to add support for Number in at least these two places:

var propData = prop.Value;
if (propData == null) continue;
var dataType = propData["Type"].ToString();
var dataValue = propData["Value"].ToString();
var isString = dataType == "String";
var mav = new MessageAttributeValue
{
DataType = dataType,
StringValue = isString ? dataValue : null,
BinaryValue = !isString ? Convert.FromBase64String(dataValue) : null
};
dict.Add(prop.Name, mav);

var dataType = obj.Value.GetProperty("Type").GetString();
var dataValue = obj.Value.GetProperty("Value").GetString();
var isString = dataType == "String";
attributes.Add(obj.Name, new MessageAttributeValue()
{
DataType = dataType,
StringValue = isString ? dataValue : null,
BinaryValue = !isString ? Convert.FromBase64String(dataValue) : null
});

@martincostello martincostello self-assigned this Nov 6, 2024
martincostello added a commit that referenced this issue Nov 6, 2024
Handle message attributes of type `Number` or with a custom suffix label.
Resolves #1556.
@martincostello martincostello added this to the v8.0.0 milestone Nov 6, 2024
martincostello added a commit that referenced this issue Nov 6, 2024
Handle message attributes of type `Number` or with a custom suffix label.
Resolves #1556.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants