diff --git a/ProcessMaker/Http/Controllers/Api/ScriptController.php b/ProcessMaker/Http/Controllers/Api/ScriptController.php index 2e7fc5c5dc..e3cca93814 100644 --- a/ProcessMaker/Http/Controllers/Api/ScriptController.php +++ b/ProcessMaker/Http/Controllers/Api/ScriptController.php @@ -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", @@ -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'); @@ -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", @@ -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; @@ -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; + } }