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

SQSClient receiveMessage JSON Parse error #2821

Closed
xto opened this issue Nov 14, 2023 · 9 comments
Closed

SQSClient receiveMessage JSON Parse error #2821

xto opened this issue Nov 14, 2023 · 9 comments
Assignees
Labels
bug This issue is a bug. p2 This is a standard priority issue

Comments

@xto
Copy link

xto commented Nov 14, 2023

Describe the bug

Calling receiveMessage on an instance of SqsClient throws an exception. We found this through a library upgrade triggered by Dependabot as it failed our CI. Here's the most minimal code to that triggers the exception.

    public function testJsonFailure(): void
    {
        $awsSdk = $this->client->getContainer()->get(AwsSdk::class);
        $sqsClient = $awsSdk->createSqS();
        $result = $sqsClient->receiveMessage(
            [
                'MaxNumberOfMessages' => 1,
                'QueueUrl' => "http://aws:4566/000000000000/internal_job_queue_dlq",
                'WaitTimeSeconds' => 0
            ]
        );
        $this->assertNull($result->get('Messages'));
    }

I did some digging and at https://github.com/aws/aws-sdk-php/blob/3.285.1/src/Api/Parser/PayloadParserTrait.php#L18, $json holds a GuzzleHttp\Psr7\Stream object and calling getContents on that object revealed that it does not contain a JSON string but rather an XML string. This is the result of dump($json->getContents()):

 "<ReceiveMessageResponse><ReceiveMessageResult></ReceiveMessageResult><ResponseMetadata><RequestId>4PDN22TK8BPVAKSD643TKWBWUD8BHMKZIF8VNGJ3JLXTCLH8811J</RequestId></ResponseMetadata></ReceiveMessageResponse>"

This issue was not present in 3.285.0

Expected Behavior

The expected behaviour is that the parser receives a JSON payload and parses it successfully

Current Behavior

An exception happens with the following message gets thrown:
Aws\Api\Parser\Exception\ParserException: Error parsing JSON: Syntax error

Reproduction Steps

<?php

namespace MyApp\IntegrationTest;

use Aws\Sdk;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class InternalJobQueueTest extends WebTestCase
{
    protected KernelBrowser $client;

    /**
     * @test
     */
    public function testJsonFailure(): void
    {
        self::createKernel();
        $this->client = self::createClient();

        $awsSdk = $this->client->getContainer()->get(Sdk::class);
        $sqsClient = $awsSdk->createSqS();
        $result = $sqsClient->receiveMessage(
            [
                'MaxNumberOfMessages' => 1,
                'QueueUrl' => "http://aws:4566/000000000000/internal_job_queue_dlq",
                'WaitTimeSeconds' => 0
            ]
        );
        $this->assertNull($result->get('Messages'));
    }
}

Possible Solution

No response

Additional Information/Context

No response

SDK version used

3.285.1

Environment details (Version of PHP (php -v)? OS name and version, etc.)

PHP 8.2.4

@xto xto added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Nov 14, 2023
@xto xto changed the title (short issue description) SQSClient receiveMessage JSON Parse error Nov 14, 2023
@yenfryherrerafeliz yenfryherrerafeliz self-assigned this Nov 14, 2023
@yenfryherrerafeliz yenfryherrerafeliz added the investigating This issue is being investigated and/or work is in progress to resolve the issue. label Nov 14, 2023
@yenfryherrerafeliz
Copy link
Contributor

Hi @xto, sorry to hear about your issues. I am not sure what could be causing this at your end. I just tried to reproduce this but I was not able to.
Here is the code I have used:

<?php

use Aws\Sqs\SqsClient;

require '../vendor/autoload.php';

$client = new SqsClient(['region' => 'us-east-2']);
$response = $client->receiveMessage([
    'QueueUrl' => 'test-queue',
    'MaxNumberOfMessages' => 1,
    'WaitTimeSeconds' => 0
]);

print_r($response);

Here is the response:

Response
Aws\Result Object
(
    [data:Aws\Result:private] => Array
        (
            [Messages] => Array
                (
                )

            [@metadata] => Array
                (
                    [statusCode] => 200
                    [effectiveUri] => https://sqs.us-east-2.amazonaws.com/test-queue
                    [headers] => Array
                        (
                            [x-amzn-requestid] => REQ-ID
                            [date] => Tue, 14 Nov 2023 17:32:57 GMT
                            [content-type] => application/x-amz-json-1.0
                            [content-length] => 15
                            [connection] => keep-alive
                        )

                    [transferStats] => Array
                        (
                            [http] => Array
                                (
                                    [0] => Array
                                        (
                                        )

                                )

                        )

                )

        )

    [monitoringEvents:Aws\Result:private] => Array
        (
        )

)

Could you please try to create new project from scratch and test it?, also, could you please make sure the SDK dependency is up to date?.

I look forward to your response.

Thanks!

@yenfryherrerafeliz yenfryherrerafeliz added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p2 This is a standard priority issue and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. needs-triage This issue or PR still needs to be triaged. labels Nov 14, 2023
@stobrien89
Copy link
Member

stobrien89 commented Nov 14, 2023

@xto,

Also, could you could provide debug logs from the test by adding 'debug' => true to your client config? Please make sure to obscure any sensitive information.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Nov 15, 2023
@bentsku
Copy link

bentsku commented Nov 15, 2023

Hello, I believe this issue might be related to LocalStack, see localstack/localstack#8267

@xto this should now be fixed in the latest version of LocalStack, you can read about it in the linked issue.

@xto
Copy link
Author

xto commented Nov 15, 2023

@bentsku thanks for the info. I'll report back on my issue after trying updating localstack.

@stobrien89
Copy link
Member

@xto,

Sorry for the confusion. #2822 prompted an investigation on our side into how we were sending requests with QueueUrl. That issue has been fixed now and will be available in today's release. Localstack's changes should resolve the xml response issue, but you may need to update your code to expect an empty Messages in the response as opposed to null.

@ziyanli-amazon
Copy link
Contributor

@xto @stobrien89 Thanks guys. SQS is also fixing the Messages being returned as null as opposed to an empty array if no messages exist.

@ziyanli-amazon
Copy link
Contributor

@bentsku Thanks for fixing the LocalStack.

@xto
Copy link
Author

xto commented Nov 16, 2023

I'm going to close this. Upgrading to the latest localstack version seems to have resolved the error. Thanks for the assist @bentsku @stobrien89 @yenfryherrerafeliz 🙇

@xto xto closed this as completed Nov 16, 2023
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

5 participants