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-&gt;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' =&gt; 'my_value']; $session = $playgroundService-&gt;createSession($workflow, 'Test Session', $metadata); </pre></li> <li>Retrieve the metadata:<br> <pre> $retrievedMetadata = $session-&gt;getMetadata(); // Expected: ['my_key' =&gt; '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' =&gt; $workflow-&gt;id(), 'name' =&gt; $sessionName, 'status' =&gt; 'idle', 'uid' =&gt; $this-&gt;currentUser-&gt;id(), 'metadata' =&gt; $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') -&gt;setLabel(new TranslatableMarkup('Metadata')) -&gt;setDescription(new TranslatableMarkup('Custom session metadata in JSON format.')) -&gt;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' =&gt; $workflow-&gt;id(), 'name' =&gt; $sessionName, 'status' =&gt; 'idle', 'uid' =&gt; $this-&gt;currentUser-&gt;id(), 'metadata' =&gt; 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' =&gt; $workflow-&gt;id(), 'name' =&gt; $sessionName, 'status' =&gt; 'idle', 'uid' =&gt; $this-&gt;currentUser-&gt;id(), ]); $session-&gt;setMetadata($metadata); $session-&gt;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-&gt;createSession($workflow, $name, $metadata); $session-&gt;setMetadata($metadata); $session-&gt;save(); </pre>
issue