joomla / joomla-cms

Home of the Joomla! Content Management System
https://www.joomla.org
GNU General Public License v2.0
4.77k stars 3.65k forks source link

Component build json output #39341

Closed JamesShaver closed 1 year ago

JamesShaver commented 1 year ago

Steps to reproduce the issue

Build a component view for json formatted output.

URL:

http://localhost/joomla/index.php?option=com_testcomponent&view=json&format=json

site\src\View\Json\JsonView.php:
<?php

namespace Joomla\Component\Testcomponent\Site\View\Json;

use Joomla\CMS\MVC\View\JsonView as BaseJsonView;

class JsonView extends BaseJsonView
{
    public $json;

    public function display($tpl = null)
    {
        $array = [
            'key1' => 'value1',
            'key2' => 'value2',
            'key3' => 'value3',
            'key4' => 'value4'
        ];
        $this->json = json_encode($array);

        parent::display($tpl);
    }
}
site\tmpl\json\json.php:
<?= $this->json; ?>

Expected result

{"key1":"value1","key2":"value2","key3":"value3","key4":"value4"}

Actual result

<br />
<b>Warning</b>:  session_write_close(): Failed to write session data using user defined save handler. (session.save_path: \Users\username\OneDrive\Documents\Development\xampp\tmp) in <b>C:\Users\username\OneDrive\Documents\Development\xampp\htdocs\joomla\libraries\vendor\joomla\session\src\Storage\NativeStorage.php</b> on line <b>114</b><br />

System information (as much as possible)

MS Windows 11 XAMPP for Windows 8.1.12 Apache/2.4.54 (Win64) PHP Version 8.1.12

Additional comments

Debug mode is turned on, but nothing populates in everything.php

Irata commented 1 year ago

I am seeing the same messages, intermittently, about failing to write the session when invoking a controller via JS that passes to and is returned JSON.

url = 'index.php?option=com_mycomponent&task=mycontroller.incoming&format=json';

my controller does almost nothing

public function incoming()
    {
        $barney = $this->app->input->post->get('payload', '', 'array');
        $response = new JsonResponse($barney);
        echo $response;
    }

Reloading the page resolves the issue for awhile but its return is seemingly random.

I don't have anymore diagnostics than already supplied at this stage.

PHP Version 8.0.23 Web Server Apache/2.4.54 (Win64) OpenSSL/1.1.1q PHP/8.0.23 WebServer to PHP Interface apache2handler Joomla! Version Joomla! 4.2.5 Stable [ Uaminifu ] 8-November-2022 15:00 GMT

Running on a local LAMP stack.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/39341.

SharkyKZ commented 1 year ago

This is coming from recent changes in debug plugin. When Track Request History option is disabled, an unlimited number of request data is stored in the session until a HTML page is opened.

JamesShaver commented 1 year ago

Confirmed, enabling Track Request History removes the error message.

richard67 commented 1 year ago

@SharkyKZ So it’s a bug, right?

MacJoom commented 1 year ago

Steps to reproduce the issue

Build a component view for json formatted output.

URL:

http://localhost/joomla/index.php?option=com_testcomponent&view=json&format=json

site\src\View\Json\JsonView.php:
<?php

namespace Joomla\Component\Testcomponent\Site\View\Json;

use Joomla\CMS\MVC\View\JsonView as BaseJsonView;

class JsonView extends BaseJsonView
{
    public $json;

    public function display($tpl = null)
    {
        $array = [
            'key1' => 'value1',
            'key2' => 'value2',
            'key3' => 'value3',
            'key4' => 'value4'
        ];
        $this->json = json_encode($array);

        parent::display($tpl);
    }
}
site\tmpl\json\json.php:
<?= $this->json; ?>

Expected result

{"key1":"value1","key2":"value2","key3":"value3","key4":"value4"}

Actual result

<br />
<b>Warning</b>:  session_write_close(): Failed to write session data using user defined save handler. (session.save_path: \Users\username\OneDrive\Documents\Development\xampp\tmp) in <b>C:\Users\username\OneDrive\Documents\Development\xampp\htdocs\joomla\libraries\vendor\joomla\session\src\Storage\NativeStorage.php</b> on line <b>114</b><br />

System information (as much as possible)

MS Windows 11 XAMPP for Windows 8.1.12 Apache/2.4.54 (Win64) PHP Version 8.1.12

Additional comments

Debug mode is turned on, but nothing populates in everything.php

I cannot reproduce the issue with your code since JsonView doesnt apply any template code. So the output is always empty... tmpl\json\json.php is never called. parent::display just puts this->_output into the documents buffer. $this->json is not used. I can think of a problem where the session data is growing too much, but cannot reproduce the issue right now. Please post the complete code with this issue - Thank you

richard67 commented 1 year ago

@MacJoom Which PHP version have you used when trying to reproduce the issue? Was it 7.4? Possibly the issue happens only with PHP 8.x.

richard67 commented 1 year ago

I have found another thing which happens with PHP 8 which happens when the "Track Request History" option in the "Debug" system plugin is disabled and disappears when that option is enabled. See #39403 . It seems they have the same root cause, but I wanted the symptoms to be documented and so created that other issue.

richard67 commented 1 year ago

Closing as having a pull request. Please test #39526 . Thanks in advance.