Skip to content

Commit 5ca5750

Browse files
committed
AC-15893: Admin Order Creation: Session Size Overflow When Adding 20+ Products (Session size exceeded 256KB limit)
Fix for PAT failure
1 parent b5a8406 commit 5ca5750

File tree

3 files changed

+60
-20
lines changed

3 files changed

+60
-20
lines changed

app/code/Magento/Sales/Controller/Adminhtml/Order/Create/LoadBlock.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,15 @@ public function execute()
128128
}
129129

130130
$result = $resultPage->getLayout()->renderElement('content');
131-
if ($request->getParam('as_js_varname') && !$asJson) {
132-
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setUpdateResult($result);
131+
if ($request->getParam('as_js_varname')) {
132+
$session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
133+
// Compress data for JSON responses to prevent session bloat while maintaining redirect pattern
134+
if ($asJson && function_exists('gzencode')) {
135+
$compressed = gzencode($result, 6); // Level 6 compression for balance of speed/size
136+
$session->setUpdateResult(['compressed' => true, 'data' => $compressed]);
137+
} else {
138+
$session->setUpdateResult($result);
139+
}
133140
return $this->resultRedirectFactory->create()->setPath('sales/*/showUpdateResult');
134141
}
135142
return $this->resultRawFactory->create()->setContents($result);

app/code/Magento/Sales/Controller/Adminhtml/Order/Create/ShowUpdateResult.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,19 @@ public function execute()
5555
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
5656
$resultRaw = $this->resultRawFactory->create();
5757
$session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
58-
if ($session->hasUpdateResult() && is_scalar($session->getUpdateResult())) {
59-
$resultRaw->setContents($session->getUpdateResult());
58+
59+
if ($session->hasUpdateResult()) {
60+
$updateResult = $session->getUpdateResult();
61+
62+
// Handle compressed data (for JSON responses to reduce session bloat)
63+
if (is_array($updateResult) && isset($updateResult['compressed']) && $updateResult['compressed']) {
64+
$decompressed = gzdecode($updateResult['data']);
65+
$resultRaw->setContents($decompressed ?: '');
66+
} elseif (is_scalar($updateResult)) {
67+
$resultRaw->setContents($updateResult);
68+
}
6069
}
70+
6171
$session->unsUpdateResult();
6272
return $resultRaw;
6373
}

dev/tests/integration/testsuite/Magento/Sales/Controller/Adminhtml/Order/Create/LoadBlockTest.php

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,16 @@ public function testAddProductToOrderFromShoppingCart(bool $asJson, bool $asJsVa
127127
$this->assertNotNull($newQuoteItemsCollection->getItemByColumnValue('sku', 'simple2'));
128128
if ($asJsVarname) {
129129
$this->assertRedirect($this->stringContains('sales/order_create/showUpdateResult'));
130-
$body = (string) $this->_objectManager->get(\Magento\Backend\Model\Session::class)->getUpdateResult();
130+
$sessionData = $this->_objectManager->get(\Magento\Backend\Model\Session::class)->getUpdateResult();
131+
// Handle compressed data for JSON responses
132+
if (is_array($sessionData) && isset($sessionData['compressed']) && $sessionData['compressed']) {
133+
$body = gzdecode($sessionData['data']);
134+
} else {
135+
$body = (string) $sessionData;
136+
}
137+
if ($asJson) {
138+
$body = json_decode($body, true, 512, JSON_THROW_ON_ERROR)['sidebar'];
139+
}
131140
} elseif ($asJson) {
132141
$body = json_decode($this->getResponse()->getBody(), true, 512, JSON_THROW_ON_ERROR)['sidebar'];
133142
} else {
@@ -154,7 +163,7 @@ public static function responseFlagsProvider(): array
154163
}
155164

156165
/**
157-
* Test that JSON response with as_js_varname does not store result in session
166+
* Test that JSON response with as_js_varname stores compressed data to prevent session bloat
158167
*
159168
* @return void
160169
*/
@@ -164,7 +173,7 @@ public static function responseFlagsProvider(): array
164173
DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$', 'is_active' => 1], as: 'quote'),
165174
DataFixture(AddProductToCart::class, ['cart_id' => '$quote.id$', 'product_id' => '$product.id$', 'qty' => 1])
166175
]
167-
public function testJsonResponseWithJsVarnameDoesNotStoreInSession(): void
176+
public function testJsonResponseWithJsVarnameUsesCompressionToPreventSessionBloat(): void
168177
{
169178
/** @var CartInterface $quote */
170179
$quote = $this->fixtures->get('quote');
@@ -198,23 +207,37 @@ public function testJsonResponseWithJsVarnameDoesNotStoreInSession(): void
198207
$newQuoteItemsCollection = $newQuote->getItemsCollection(false);
199208
$this->assertNotNull($newQuoteItemsCollection->getItemByColumnValue('sku', $product->getSku()));
200209

201-
// Session should NOT contain updateResult (prevents session bloat)
202-
$backendSession = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
203-
$this->assertFalse(
204-
$backendSession->hasUpdateResult(),
205-
'Session should not store updateResult for JSON responses to prevent session bloat'
206-
);
207-
208-
// Verify response is returned directly (not a redirect)
210+
// Verify redirect happens (maintains 2-request pattern for PAT compatibility)
209211
$response = $this->getResponse();
210-
$this->assertFalse(
212+
$this->assertTrue(
211213
$response->isRedirect(),
212-
'Response should not be a redirect when json=true with as_js_varname'
214+
'Response should redirect to ShowUpdateResult'
213215
);
216+
$this->assertRedirect($this->stringContains('sales/order_create/showUpdateResult'));
214217

215-
// Verify response body is valid JSON with expected content
216-
$responseBody = $response->getBody();
217-
$this->assertNotEmpty($responseBody, 'Response body should not be empty');
218+
// Verify session stores compressed data to prevent bloat
219+
$backendSession = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
220+
$this->assertTrue(
221+
$backendSession->hasUpdateResult(),
222+
'Session should contain compressed updateResult'
223+
);
224+
225+
$sessionData = $backendSession->getUpdateResult();
226+
$this->assertIsArray($sessionData, 'Session data should be array for compressed format');
227+
$this->assertTrue($sessionData['compressed'], 'Data should be marked as compressed');
228+
$this->assertArrayHasKey('data', $sessionData, 'Compressed data should exist');
229+
230+
// Verify compression actually reduces size
231+
$decompressed = gzdecode($sessionData['data']);
232+
$this->assertNotEmpty($decompressed, 'Decompressed data should not be empty');
233+
234+
$originalSize = strlen($decompressed);
235+
$compressedSize = strlen($sessionData['data']);
236+
$this->assertLessThan(
237+
$originalSize,
238+
$compressedSize,
239+
'Compressed size should be smaller than original'
240+
);
218241
}
219242

220243
/**

0 commit comments

Comments
 (0)