8000 Jsonresponse fix by kanariezwart · Pull Request #16551 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Jsonresponse fix #16551

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

Closed
wants to merge 4 commits into from
Closed

Jsonresponse fix #16551

wants to merge 4 commits into from

Conversation

kanariezwart
Copy link

Fixed JsonResponse setContent after settinh encoding options
[HttpFoundation][JsonResponse.php] udated setData() with getContent() when getContent is not empty

Q A
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR [The reference to the documentation PR if any]

Added new tests for testing setting content and setting data after setting EncodingOptions
* jsonresponse-fix:
  [HttpFoundation] JsonResponse.php. Updated JsonResponseTest
  [HttpFoundation] JsonResponse.php. Updated setData() with getContent() when getContent is not empty
@@ -144,6 +144,10 @@ public function setEncodingOptions($encodingOptions)
{
$this->encodingOptions = (int) $encodingOptions;

if ($this->content !== null) {
$this->data = $this->content;
Copy link
Member

Choose a reason for hiding this comment

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

this looks wrong to me

Copy link
Member

Choose a reason for hiding this comment

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

and btw, I don't see how your new tests are covering this

@kanariezwart
Copy link
Author

@stof please look at #16541

@stof
Copy link
Member
stof commented Nov 16, 2015

I think the bug is that it should decode $this->content, not $this->data (which is never containing a JSON string). But assigning content into data is wrong

@stof
Copy link
Member
stof commented Nov 16, 2015

and now, I agree that your test covers the bug. But the original test was not covering it

@kanariezwart
Copy link
Author

@stof I see your point. However in $this->setData() $this->data contains a json string at one point.

I think it depends on what is leading, $this->data or $this->content.
When setting encodingOptions() $this->update() is invoked which invokes setData()

@stof
Copy link
Member
stof commented Nov 16, 2015

ah wait, $this->data contains the encoded data, not the data set by the setter. This got confusing.

But your fix is wrong. $this->content will not contain a JSON string which can be decoded in case of using the class for a JSONP response. $this->data must be used

@kanariezwart
Copy link
Author

@stof $this->content holds the serialized data from $this->data. If the case of adding encoding options, re-decoding $this->content it should be no problem, although it is not a very nice fix.

JsonResponse has two ways of setting content, which can be misleading. JsonResponse being a subclass of Response (and some sort of a helperClass if you will), $this->data was added, next to $this->content to ease the creation of a valid Response object with json data (headers etc).

If $this->data is the prefered way to set/add and update content, I propose this quickfix. As $this->update() updates $this->content always with $this->data.

    public function setEncodingOptions($encodingOptions)
    {
        $this->encodingOptions = (int) $encodingOptions;
        // when content is set by calling $this->setContent, instead of $this->data
        if (!empty($this->content)) {
            return $this->setData(json_decode($this->content));
        }
        return $this->setData(json_decode($this->data));
    }
public function addData($data)  //instead setData
{
    $this->data = $data;
    $this->update();
}

@kanariezwart kanariezwart deleted the jsonresponse-fix branch November 16, 2015 21:17
@kanariezwart kanariezwart restored the jsonresponse-fix branch November 16, 2015 21:36
@kanariezwart kanariezwart deleted the jsonresponse-fix branch November 16, 2015 21:36
@kanariezwart kanariezwart mentioned this pull request Nov 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0