PlaygroundService::createSession() does not JSON-encode metadata array, causing metadata loss
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3569982. -->
Reported by: [d34dman](https://www.drupal.org/user/751698)
>>>
<h3>Problem/Motivation</h3>
<p>When creating a playground session via <code>PlaygroundService::createSession()</code>, the metadata parameter is passed directly to <code>FlowDropPlaygroundSession::create()</code> as a PHP array. However, the <code>metadata</code> field is defined as a <code>string_long</code> field type that expects a JSON string.</p>
<p>Drupal's string field type does not handle arrays - it casts the array to the literal string <code>"Array"</code>. This means any metadata passed to <code>createSession()</code> is lost, and subsequent calls to <code>$session->getMetadata()</code> return an empty array.</p>
<h3>Steps to reproduce</h3>
<ol>
<li>Call <code>PlaygroundService::createSession()</code> with metadata:<br>
<pre>
$metadata = ['my_key' => 'my_value'];
$session = $playgroundService->createSession($workflow, 'Test Session', $metadata);
</pre></li>
<li>Retrieve the metadata:<br>
<pre>
$retrievedMetadata = $session->getMetadata();
// Expected: ['my_key' => 'my_value']
// Actual: []
</pre></li>
<li>If you inspect the database, the <code>metadata</code> column contains the literal string <code>"Array"</code> instead of valid JSON.</li>
</ol>
<h3>Root cause</h3>
<p>In <code>PlaygroundService::createSession()</code> (around line 175):</p>
<pre>
$session = FlowDropPlaygroundSession::create([
'workflow_id' => $workflow->id(),
'name' => $sessionName,
'status' => 'idle',
'uid' => $this->currentUser->id(),
'metadata' => $metadata, // Array passed directly to string_long field
]);
</pre><p>The <code>metadata</code> field is defined in <code>FlowDropPlaygroundSession::baseFieldDefinitions()</code> as:</p>
<pre>
$fields['metadata'] = BaseFieldDefinition::create('string_long')
->setLabel(new TranslatableMarkup('Metadata'))
->setDescription(new TranslatableMarkup('Custom session metadata in JSON format.'))
->setDefaultValue('{}');
</pre><p>When an array is assigned to a <code>string_long</code> field during entity creation, PHP casts it to the string <code>"Array"</code>, which is not valid JSON.</p>
<h3>Proposed resolution</h3>
<p>JSON-encode the metadata array before passing it to <code>create()</code>:</p>
<pre>
$session = FlowDropPlaygroundSession::create([
'workflow_id' => $workflow->id(),
'name' => $sessionName,
'status' => 'idle',
'uid' => $this->currentUser->id(),
'metadata' => Json::encode($metadata), // Properly encode as JSON
]);
</pre><p>Alternatively, use <code>setMetadata()</code> after creation (which already handles JSON encoding):</p>
<pre>
$session = FlowDropPlaygroundSession::create([
'workflow_id' => $workflow->id(),
'name' => $sessionName,
'status' => 'idle',
'uid' => $this->currentUser->id(),
]);
$session->setMetadata($metadata);
$session->save();
</pre><h3>Workaround</h3>
<p>Until this is fixed, callers can work around the issue by re-setting the metadata after session creation:</p>
<pre>
$session = $playgroundService->createSession($workflow, $name, $metadata);
$session->setMetadata($metadata);
$session->save();
</pre>
issue