Skip to content

AppSync: BigInt Values Being Rounded in Subscription Payload

0

I'm encountering an issue with AWS AppSync when publishing messages that contain large integer values (BigInt). I’m using the default AppSync settings (default namespace and channel).

I published the following message to a subscription channel:

{
  "username": "user a",
  "userid": 263505143177162752
}

The publish operation completes successfully without any errors.

However, on the subscriber's end, the received message appears as:

{
  "username": "user a",
  "userid": 263505143177162750
}

As you can see, the userid has been rounded, which causes data integrity issues. When I checked the raw event format received by the subscriber, I noticed the correct value is present inside the raw event string:

{
  "id": "2e601848-9ceb-4cc2-8642-0a2990b1e315",
  "type": "data",
  "event": "{\"username\":\"user a\",\"userid\":263505143177162752}"
}

It seems like the issue is related to JSON parsing/serialization of large integers on the client side or in the AppSync transport layer. The value is being deserialized in a way that loses precision.

Expected Behavior: Large integer values like 263505143177162752 should be preserved exactly as published and should not be rounded or altered in any way on the subscriber's end.

Request message: Enter image description here

Parsed response: Enter image description here

Raw response: Enter image description here

asked a year ago221 views

2 Answers
1
Accepted Answer

The issue you're experiencing with large integer values being rounded in AppSync subscriptions is likely related to the handling of JSON numbers in JavaScript. JavaScript has limitations when it comes to representing large integers, as it uses 64-bit floating-point numbers (IEEE 754) for all numeric values. This can lead to precision loss for integers larger than 2^53 - 1.

In your case, the value 263505143177162752 exceeds this limit, which is why it's being rounded when parsed as a regular JavaScript number.

To address this issue, you have a few options:

  1. Use string representation: Instead of sending the large integer as a number, you can send it as a string. This will preserve the exact value without any rounding.

  2. Use BigInt in JavaScript: If your client-side environment supports BigInt, you can use it to handle large integers without precision loss. However, be aware that BigInt is not supported in all JavaScript environments.

  3. Custom parsing: Implement custom parsing logic on the client-side to handle large integers correctly. This could involve checking if a number exceeds a certain threshold and treating it specially.

  4. Use a data type that preserves precision: If possible, consider using a data type in your schema that can handle large integers without loss of precision, such as a string or a custom scalar type.

It's important to note that the issue is not with AppSync itself, as the correct value is present in the raw event string. The problem occurs during the JSON parsing/deserialization process, likely on the client side.

To ensure data integrity, you may need to adjust your client-side code to handle these large integer values appropriately, possibly by using one of the methods mentioned above.
Sources
AWS AppSync Events concepts - AWS AppSync Events
Type system (request mapping) - AWS AppSync

answered a year ago

EXPERT

reviewed a year ago

0

Thanks . Yes, I made it as string value and it is working fine. Do we have any documentations or articles to talk about these kind of limitations in general ?

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.