Skip to content

Update JsonEncoder.php#4416

Closed
AntonioCS wants to merge 1 commit into
api-platform:mainfrom
AntonioCS:main
Closed

Update JsonEncoder.php#4416
AntonioCS wants to merge 1 commit into
api-platform:mainfrom
AntonioCS:main

Conversation

@AntonioCS
Copy link
Copy Markdown
Contributor

Added JSON_INVALID_UTF8_IGNORE to $jsonEncodeOptions

Q A
Branch? main for features / current stable version branch for bug fixes
Tickets #...
License MIT
Doc PR api-platform/docs#...

Added JSON_INVALID_UTF8_IGNORE to $jsonEncodeOptions
Copy link
Copy Markdown
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Could you rebase against the 2.6 branch as it's a bug fix and add a unit test to prevent any future regression please?

@AntonioCS
Copy link
Copy Markdown
Contributor Author

I had a few issues rebasing, so I guess it would be ok to start with 2.6?
Also, I'm not sure how to replicate this error, as this is something coming from a doctrine exception that API Platform tries to serialize. I'll try something I guess :)

@dunglas
Copy link
Copy Markdown
Member

dunglas commented Sep 5, 2021

Just create a test that serializes a string containing invalid UTF-8 sequences.

By the way, I wonder if it's a good idea to always enable this. Maybe should it be enabled only for the dev environment. Or maybe should would it be better to fix the exception message in Doctrine after all.

I fear that generating JSON documents containing non-valid UTF-8 sequences in prod leads to more harm than good.

@AntonioCS
Copy link
Copy Markdown
Contributor Author

Yeah, it was simple, I thought I had to do something with something from doctrine, but I got it.
Well... this being displayed in prod will also cause issues (at least in my case) as it just hides errors

@AntonioCS
Copy link
Copy Markdown
Contributor Author

Not sure how to fix this is doctrine, as it's trying to insert binary data in a binary field in the DB

@AntonioCS
Copy link
Copy Markdown
Contributor Author

Did this for testing:


    public function testUTF8MalformedHandling()
    {
        $data = ['random_bytes' => random_bytes(16)];

        try {
            $this->assertNotNull($this->encoder->encode($data, 'json'));
        } catch (\Exception $exception) {
            $this->fail('An exception was thrown: ' . $exception->getMessage());
        }
    }
}

With the added flag (JSON_INVALID_UTF8_IGNORE) no exception was triggered.

@dunglas
Copy link
Copy Markdown
Member

dunglas commented Sep 5, 2021

Regarding the test, you could use pack() instead to create the string containing invalid UTF-8 sequences. Using the random number will make the test flaky (what if the generated sequence is a valid UTF-8 string)?

But I wonder if this is the best fix possible: api-platform/api-platform#2000 (comment)

@AntonioCS
Copy link
Copy Markdown
Contributor Author

I've changed the test and branched from 2.6 but I can branch from main and then cherry pick. Is this still a valid approach? (the fix in the jsonencoder)

@soyuka
Copy link
Copy Markdown
Member

soyuka commented Oct 22, 2021

sure, if you have issues just open a new pr no problem

@divine
Copy link
Copy Markdown
Contributor

divine commented Sep 25, 2022

JFYI, this can be closed.
@soyuka @dunglas

Thanks!

@dunglas dunglas closed this Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants