Problem/Motivation

When creating a playground session via PlaygroundService::createSession(), the metadata parameter is passed directly to FlowDropPlaygroundSession::create() as a PHP array. However, the metadata field is defined as a string_long field type that expects a JSON string.

Drupal's string field type does not handle arrays - it casts the array to the literal string "Array". This means any metadata passed to createSession() is lost, and subsequent calls to $session->getMetadata() return an empty array.

Steps to reproduce

  1. Call PlaygroundService::createSession() with metadata:
    $metadata = ['my_key' => 'my_value'];
    $session = $playgroundService->createSession($workflow, 'Test Session', $metadata);
    
  2. Retrieve the metadata:
    $retrievedMetadata = $session->getMetadata();
    // Expected: ['my_key' => 'my_value']
    // Actual: []
    
  3. If you inspect the database, the metadata column contains the literal string "Array" instead of valid JSON.

Root cause

In PlaygroundService::createSession() (around line 175):

$session = FlowDropPlaygroundSession::create([
  'workflow_id' => $workflow->id(),
  'name' => $sessionName,
  'status' => 'idle',
  'uid' => $this->currentUser->id(),
  'metadata' => $metadata,  // Array passed directly to string_long field
]);

The metadata field is defined in FlowDropPlaygroundSession::baseFieldDefinitions() as:

$fields['metadata'] = BaseFieldDefinition::create('string_long')
  ->setLabel(new TranslatableMarkup('Metadata'))
  ->setDescription(new TranslatableMarkup('Custom session metadata in JSON format.'))
  ->setDefaultValue('{}');

When an array is assigned to a string_long field during entity creation, PHP casts it to the string "Array", which is not valid JSON.

Proposed resolution

JSON-encode the metadata array before passing it to create():

$session = FlowDropPlaygroundSession::create([
  'workflow_id' => $workflow->id(),
  'name' => $sessionName,
  'status' => 'idle',
  'uid' => $this->currentUser->id(),
  'metadata' => Json::encode($metadata),  // Properly encode as JSON
]);

Alternatively, use setMetadata() after creation (which already handles JSON encoding):

$session = FlowDropPlaygroundSession::create([
  'workflow_id' => $workflow->id(),
  'name' => $sessionName,
  'status' => 'idle',
  'uid' => $this->currentUser->id(),
]);
$session->setMetadata($metadata);
$session->save();

Workaround

Until this is fixed, callers can work around the issue by re-setting the metadata after session creation:

$session = $playgroundService->createSession($workflow, $name, $metadata);
$session->setMetadata($metadata);
$session->save();

Comments

d34dman created an issue. See original summary.

  • d34dman committed 501aec51 on 1.x
    feat: #3569982 PlaygroundService::createSession() does not JSON-encode...
d34dman’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.