-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Remove zend json from json controller #10342
Remove zend json from json controller #10342
Conversation
- use the \Magento\Framework\Serialize\Serializer\Json instead of Zend_Json
- if we are dealing with something like new \Magento\Framework\DataObject() being used as a response object
- deprecate this method to encourage usage of other methods in this class, - be stricter on types passed into this method,
This PR I expect to come up with plenty of talk/opinions. I am happy to hear what people think would be other options if there are some. |
*/ | ||
public function __construct( | ||
InlineInterface $translateInline, | ||
\Magento\Framework\Serialize\Serializer\Json $serializer = null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we rely on particular implementation and not interface by the way?
https://github.com/magento/magento2/blob/develop/app/etc/di.xml#L162
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#8331 (comment) was the original comment from the team on the matter,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I expected something like this. Not sure why such interface was introduced at all if we keep creating json-only code.
Needs to be mentioned somewhere to avoid confusion like #10335 (comment)
@dmanners This pull request has been reverted due to broken functionality. We need to review this change one more time. Thank you |
@okorshenko sure. Do you have a specific use/test case that this broke for so that I can have something to review when thinking about possible options here. Thanks |
* @throws \InvalidArgumentException | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
* @deprecated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree with deprecating this method. setData
is for setting data which will be encoded and setJsonData
is for setting raw data.
Two arguments which has no effect should be deprecated though.
Please change method description accordingly.
*/ | ||
public function setData($data, $cycleCheck = false, $options = []) | ||
{ | ||
$this->json = \Zend_Json::encode($data, $cycleCheck, $options); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is just setter, no need to call another setter, Magento is already slow enough.
*/ | ||
public function setData($data, $cycleCheck = false, $options = []) | ||
{ | ||
$this->json = \Zend_Json::encode($data, $cycleCheck, $options); | ||
if ($data instanceof \Magento\Framework\DataObject) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why need this explicitly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed to maintain compatibility with the old implementation which took care of this inside.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see...
public static function encode($valueToEncode, $cycleCheck = false, $options = array())
{
if (is_object($valueToEncode)) {
if (method_exists($valueToEncode, 'toJson')) {
return $valueToEncode->toJson();
} elseif (method_exists($valueToEncode, 'toArray')) {
return self::encode($valueToEncode->toArray(), $cycleCheck, $options);
}
}
Why it is not maintained for any object then?
return $this->setArrayData($data->toArray()); | ||
} | ||
|
||
if (is_array($data)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this if
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please clarify
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setData
should be as old one, thus setData([])
just encodes array with no necessity for additional logic.
return $this->setArrayData($data); | ||
} | ||
|
||
if (is_string($data)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing string parameter did encode $data
before, please remove this if
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please clarify
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setData('someString')
was setting json_encode('someString')
and not just 'someString'
.
* @return $this | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function setArrayData(array $data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this method as it does not bring any value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method was introduced to deprecate and remove setData
method, which has too much responsibilities.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what I strongly disagree with. Its responsibility is quite clear - encode value and set it. There is no need to think which value you're passing.
setData
- pass value and it will be encoded
setJsonData
- pass string and it will be set as is
I do agree that setData($object)
could be discouraged and corresponding if
could be marked as deprecated for further removal and setData
method simplification. But introduction of setArrayData
method looks like a mess to me.
@dmanners I didn't do code review previous time but now did, please check my suggestions and prepare a new PR (merged PR cannot be reopened AFAIK). |
@orlangur thanks for the feedback, I appreciate the thoughts. I need to re-think these changes anyway so will take your ideas on board and see what I can put together. |
Description
Removing the usage of
Zend_Json
in the Json Controller. Deprecating the methodsetData
to encourage the use of the more type specific methods in this class after talking through options with @okorshenkoFixed Issues (if relevant)
Manual testing scenarios
Contribution checklist