awslabs / aws-mobile-appsync-sdk-js

JavaScript library files for Offline, Sync, Sigv4. includes support for React Native
Apache License 2.0
921 stars 266 forks source link

AWSJSON Double Encoded #533

Open peterservisbot opened 4 years ago

peterservisbot commented 4 years ago

Do you want to request a feature or report a bug? Bug

What is the current behavior? AWSJSON fields arriving on a subscription are double encoded.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem.

Layout:

Schema: type Message { event: AWSJSON! } type Mutation { createMessage(event: AWSJSON!): Message } type Subscription { subscribeToNewMessage(): Message @aws_subscribe(mutations: ["createMessage"]) }

Pipeline Resolver attached to createMessage mutation

Before Mapping template: $util.qr($ctx.stash.put("source", "ourSource")) {}

After Mapping Template `

set( $ctx.result.event = $ctx.args.event)

$util.toJson($context.result) `

One function in the pipeline Request Mapping: { "version": "2017-02-28", "payload": { "event": $util.toJson($context.arguments.event) } }

Response Mapping: $util.toJson($ctx.result)

What is the expected behavior? Single encoding of AWSJSON on a subscription as per previous versions

Which versions and which environment (browser, react-native, nodejs) / OS are affected by this issue? Did this work in previous versions?

Happening on Chrome in macOS 10.15 The issue only presented itself after moving from aws-appsync 1.8.1 -> 3.0.2 Worked in 1.8.1

In 1.8.1 the 'event' JSON appeared as: {"id":"w3VFb03WGK","version":"v2","timestamp":1583838183535}

In 3.0.2 the 'event' JSON appears as: "{\"id\":\"5SEt78RoYx\",\"version\":\"v2\",\"timestamp\":1583837494973}"

With no changes to the underlying appsync API

0xdevalias commented 4 years ago

Curious if you are still getting this error? I had an application break overnight even though the code hasn't been touched in ages, and in debugging it, the error is where we were doing a double decode to work around this issue:

const payload = JSON.parse(JSON.parse(_payload))

Contrived debug example shows that the error is being caused by the double decode now, and a single decode seems to work as expected:

image

peterservisbot commented 4 years ago

We are seeing the same.

0xdevalias commented 4 years ago

Interesting. That implies to me that it’s probably on the AppSync side rather than this lib. I wonder if there’s an AWS AppSync changelog/etc type entry somewhere that explains/ ores the change.

uclaeamsavino commented 12 months ago

I found this thread because I'm finding AppSync expects incoming AWSJSON input to be double-stringified, and AWSJSON responses from appsync have to be parse twice. Weird.

123jumi commented 5 months ago

same issue, the appsync queries tester does not allow objects that are single-stringified. The data needs to be double-parse to get the object. Postman does this automatically though.