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

Proper use of json_encode and error handling for outputArray() in processors #modxsnowup #13389

Closed

Conversation

christianseel
Copy link
Contributor

What does it do?

This PR replaces the $modx->toJSON() and the hardcoded string in modProcessor::outputArray() with a full json_encode() call. It also adds a log message to the MODX error log if json_encode() fails and makes sure the processor still returns valid JSON.

Why is it needed?

If the $array was for any reason malformed (in our case it contained illegal UTF-8 characters), the $modx->toJSON() returns false instead of valid JSON. Due to the fact that the rest of the JSON response was hardcoded as a string the processor returned invalid JSON like

{"success":true,"total":"3","results":}

(notice the missing data after "results":)

Related issue(s)/PR(s)

(none known)

PR powered by the #modxsnowup

@christianseel
Copy link
Contributor Author

Travis probably failed not because of the changes in this PR:

PHP Fatal error:  Class 'PHPUnit_Framework_TestCase' not found in /home/travis/build/modxcms/revolution/_build/test/MODxTestCase.php on line 29

@rtripault
Copy link
Contributor

Indeed, unrelated and apparently safe to ignore

@OptimusCrime
Copy link
Contributor

This +100. So much better doing it this way. Note that this breaks compatibility, so it might need to go in the 3.x branch? Perhaps the 2.x could work too? No matter which version it is introduced it, it the PHP JSON extension will need to be a server requirement from the version and up.

@christianseel
Copy link
Contributor Author

@OptimusCrime json_encode is already widely used in the MODX core: https://github.com/modxcms/revolution/search?utf8=%E2%9C%93&q=json_encode
So I'd say it doesn't add breaking changes and it can be considered as a bug fix.

@OptimusCrime
Copy link
Contributor

I see, my bad @christianseel :)

I see that the toJSON method has been changed from xpdo 2.x to 3.x.

I also see the JSON listed in the server requirements under extensions, which makes me wonder why the core has used toJSON to tasks that could just be json_encode/json_decode calls from the beginning of, as this method more or less serves not purpose other than attempting to use a user library in the 2.x version if the native json methods are missing.

@Mark-H
Copy link
Collaborator

Mark-H commented Mar 20, 2017

The toJSON/fromJSON methods date back from a time where json_encode/json_decode support was not as universal as it is today. No longer relevant, and deprecated as listed in #13199.

@OptimusCrime
Copy link
Contributor

Should it be deprecated and removed in 3.x then? This PR targets 2.5.x. I'm all in favor of removing this unnecessary library, I am just trying to understand how you are handling this + xPDO and semver.

@opengeek
Copy link
Member

Yes, toJSON and fromJSON should be deprecated and removed from xPDO in 3.x. We still need to target 2.6 (i.e. 2.x branch) with this change.

@opengeek opengeek changed the base branch from 2.5.x to 2.x March 21, 2017 22:35
@Jako Jako added area-core bug The issue in the code or project, which should be addressed. priority-2-high labels Apr 18, 2017
@Jako Jako added this to the 2.5.7 milestone Apr 18, 2017
@Jako Jako self-assigned this Apr 18, 2017
@opengeek
Copy link
Member

This was merged into 2.5.x and cherry-picked into 2.x — modify the base branch before merging so these PRs will automatically close.

@opengeek opengeek closed this Apr 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core bug The issue in the code or project, which should be addressed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants