Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions ProcessMaker/Http/Controllers/Api/ScriptController.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,11 @@ public function index(Request $request)
* @OA\JsonContent(
* @OA\Property(
* property="data",
* type="array",
* @OA\Items (type="object"),
* type="object",
* ),
* @OA\Property(
* property="config",
* type="array",
* @OA\Items (type="object"),
* type="object",
* ),
* @OA\Property(
* property="code",
Expand All @@ -187,8 +185,8 @@ public function index(Request $request)
*/
public function preview(Request $request, Script $script)
{
$data = json_decode($request->get('data'), true) ?: [];
$config = json_decode($request->get('config'), true) ?: [];
$data = $this->getRequestArray($request->get('data'));
$config = $this->getRequestArray($request->get('config'));
$code = $request->get('code');
$nonce = $request->get('nonce');

Expand All @@ -215,13 +213,11 @@ public function preview(Request $request, Script $script)
* @OA\JsonContent(
* @OA\Property(
* property="data",
* type="array",
* @OA\Items (type="object"),
* type="object",
* ),
* @OA\Property(
* property="config",
* type="array",
* @OA\Items (type="object"),
* type="object",
* ),
* @OA\Property(
* property="sync",
Expand All @@ -245,8 +241,8 @@ public function execute(Request $request, ...$scriptKey)
$processRequest = ProcessRequestToken::findOrFail($request->task_id)->processRequest;
$script = $script->versionFor($processRequest);
}
$data = json_decode($request->get('data'), true) ?: [];
$config = json_decode($request->get('config'), true) ?: [];
$data = $this->getRequestArray($request->get('data'));
$config = $this->getRequestArray($request->get('config'));
$watcher = $request->get('watcher', uniqid('scr', true));
$code = $script->code;

Expand Down Expand Up @@ -561,4 +557,29 @@ public function close(Script $script)

return response([], 204);
}

/**
* Normalize script input before executing the script.
* Script data and config must be an object (associative array), the script
* microservice rejects list payloads like [{}] with a 422.
*/
private function getRequestArray($value): array
{
if (is_string($value)) {
$value = json_decode($value, true);
}

$result = [];

if (is_array($value)) {
if (!array_is_list($value)) {
$result = $value;
} elseif (count($value) === 1 && is_array($value[0])) {
// Unwrap [{}] or [{"key": "value"}] to object form; ignore other lists.
$result = $value[0];
}
}

return $result;
}
}
Loading