From 27a50603522dfc40af95782bc4eaccbcae4d4ecb Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Wed, 4 Nov 2026 15:47:32 +0100 Subject: [PATCH 1/2] #455 - cleverage:process:execute: replace the invalid UTF-8 of the json-stream output file Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 3 +++ docs/01-quick_start.md | 2 +- src/Command/ExecuteProcessCommand.php | 12 ++++++++-- tests/Command/ExecuteProcessCommandTest.php | 25 +++++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d50229c..c7f3c422 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ Latest * [#436](https://github.com/cleverage/process-bundle/issues/436) `xpath_evaluator`: a boolean expression whose value is `false` (`boolean()`, `not()`, a false comparison) returns `false`, instead of throwing `Invalid xpath query '...': unknown error`. Only an expression reporting libxml errors is invalid. Add tests. * [#438](https://github.com/cleverage/process-bundle/issues/438) Generic transformers: the codes of their `transformers` and `contextual_options` keep their dashes, as the codes of the processes, tasks and generic transformers since 5.7. A generic transformer referencing a generic transformer whose code has a dash no longer triggers the deprecation of its former name, and a transformer service whose code has a dash is found. Update documentation, add tests. +### Fixes +* [#455](https://github.com/cleverage/process-bundle/issues/455) `cleverage:process:execute -o -t json-stream`: the invalid UTF-8 sequences of the output are replaced, as on STDOUT (a `JsonException` was thrown, also by the buffer of `process_launcher`); an output that cannot be encoded as JSON is reported on the error output. Update documentation, add tests. + v5.8 ----- diff --git a/docs/01-quick_start.md b/docs/01-quick_start.md index f3f00c77..f9eb166e 100644 --- a/docs/01-quick_start.md +++ b/docs/01-quick_start.md @@ -276,7 +276,7 @@ Process 'project_prefix.process_name' executed successfully | `--input-from-stdin` | | Read the input value from STDIN instead (e.g. `cat file.json \| bin/console cleverage:process:execute ...`) | | `--context=:` | `-c` | Contextual value, can be repeated. The key must only contain word characters (`\w+`: letters, digits, `_`). The value is parsed as YAML, so `-c limit:10` gives an integer and `-c name:"'foo'"` forces a string (also needed for dates: `-c date:2024-01-01` gives an integer timestamp) | | `--output=` | `-o` | Where to dump the value returned by the task defined by the process `end_point`: `-` (default) for STDOUT, or a file path | -| `--output-format=` | `-t` | Format of the dumped output: `dump` (Symfony VarDumper, STDOUT only) or `json-stream` (written to STDOUT, invalid UTF-8 sequences replaced, or into the `--output` file if the value is an array: otherwise, or when the value cannot be encoded as JSON for STDOUT, a warning is written on the error output). Nothing is dumped when this option is omitted. Without `end_point`, the output is `null`. The format and the process codes are checked before any process is executed | +| `--output-format=` | `-t` | Format of the dumped output: `dump` (Symfony VarDumper, STDOUT only) or `json-stream` (written to STDOUT, or into the `--output` file if the value is an array, invalid UTF-8 sequences replaced: otherwise, or when the value cannot be encoded as JSON, a warning is written on the error output). Nothing is dumped when this option is omitted. Without `end_point`, the output is `null`. The format and the process codes are checked before any process is executed | Example: diff --git a/src/Command/ExecuteProcessCommand.php b/src/Command/ExecuteProcessCommand.php index 911ccac5..e92decf1 100644 --- a/src/Command/ExecuteProcessCommand.php +++ b/src/Command/ExecuteProcessCommand.php @@ -190,8 +190,16 @@ protected function handleOutputData(mixed $data, InputInterface $input, OutputIn return; } - $outputFile = new JsonStreamFile($input->getOption('output'), 'wb'); - $outputFile->writeLine($data); + // As on STDOUT, the invalid UTF-8 sequences are replaced (e.g. a value read from a Latin-1 file); the buffer of + // the process_launcher task is written here too + $outputFile = new JsonStreamFile($input->getOption('output'), 'wb', null, [\JSON_THROW_ON_ERROR, \JSON_INVALID_UTF8_SUBSTITUTE]); + try { + $outputFile->writeLine($data); + } catch (\JsonException $exception) { + $this->getErrorOutput($output)->writeln(\sprintf("Output not stored in '%s': it cannot be encoded as JSON (%s)", $input->getOption('output'), $exception->getMessage())); + + return; + } if ($output->isVerbose()) { $output->writeln(\sprintf("Output stored in '%s'", $input->getOption('output'))); } diff --git a/tests/Command/ExecuteProcessCommandTest.php b/tests/Command/ExecuteProcessCommandTest.php index bb8d6bdc..d052ff78 100644 --- a/tests/Command/ExecuteProcessCommandTest.php +++ b/tests/Command/ExecuteProcessCommandTest.php @@ -116,6 +116,31 @@ public function testAnArrayOutputIsStoredInTheFile(): void self::assertSame('{"foo":"bar"}', trim((string) file_get_contents($this->outputFile))); } + /** + * As on STDOUT: a JsonException was thrown (e.g. for the buffer of the process_launcher task). + */ + public function testInvalidUtf8IsReplacedInTheFile(): void + { + $tester = $this->createTester($this->once(), ['name' => "Caf\xe9"]); + + self::assertSame(0, $tester->execute(['processCodes' => ['test.process'], '--output' => $this->outputFile, '--output-format' => 'json-stream'])); + self::assertSame('{"name":"Caf\ufffd"}', trim((string) file_get_contents($this->outputFile))); + } + + public function testAnOutputThatCannotBeEncodedAsJsonIsReportedInsteadOfBeingStored(): void + { + $tester = $this->createTester($this->once(), ['value' => \NAN]); + + self::assertSame(0, $tester->execute( + ['processCodes' => ['test.process'], '--output' => $this->outputFile, '--output-format' => 'json-stream'], + ['capture_stderr_separately' => true], + )); + self::assertStringContainsString( + "Output not stored in '{$this->outputFile}': it cannot be encoded as JSON (Inf and NaN cannot be JSON encoded)", + $tester->getErrorOutput() + ); + } + public function testANonArrayOutputIsReportedInsteadOfBeingSilentlyLost(): void { $tester = $this->createTester($this->once(), 'scalar'); From 76f83df53e0542805306b7d79e18d8ff6805281a Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Wed, 4 Nov 2026 17:09:25 +0100 Subject: [PATCH 2/2] #455 - Fix the section of the CHANGELOG entry Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75a47a23..a8be8dd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,15 +7,13 @@ Latest * [#440](https://github.com/cleverage/process-bundle/issues/440) Documentation: `json_stream_reader` and `file_splitter` open their path with `SplFileObject`, any PHP stream wrapper is accepted (`http://`, `php://filter`...); the path must not be built from untrusted input (SSRF, reading local files), validate it before the task. ## Fixes +* [#455](https://github.com/cleverage/process-bundle/issues/455) `cleverage:process:execute -o -t json-stream`: the invalid UTF-8 sequences of the output are replaced, as on STDOUT (a `JsonException` was thrown, also by the buffer of `process_launcher`); an output that cannot be encoded as JSON is reported on the error output. Update documentation, add tests. * [#434](https://github.com/cleverage/process-bundle/issues/434) Signals: when the same SIGINT or SIGTERM is received again while a process is running, the command exits right away, as documented. The console application dispatches the same event at each signal, whose exit stayed aborted by the first signal. Add tests. * [#436](https://github.com/cleverage/process-bundle/issues/436) `xpath_evaluator`: a boolean expression whose value is `false` (`boolean()`, `not()`, a false comparison) returns `false`, instead of throwing `Invalid xpath query '...': unknown error`. Only an expression reporting libxml errors is invalid. Add tests. * [#438](https://github.com/cleverage/process-bundle/issues/438) Generic transformers: the codes of their `transformers` and `contextual_options` keep their dashes, as the codes of the processes, tasks and generic transformers since 5.7. A generic transformer referencing a generic transformer whose code has a dash no longer triggers the deprecation of its former name, and a transformer service whose code has a dash is found. Update documentation, add tests. * [#447](https://github.com/cleverage/process-bundle/issues/447) `date_parser`: the trailing data allowed by a `+` in the format (e.g. `!Y-m-d+`) is no longer rejected (`Trailing data` warning, rejected since 5.7 with the other warnings). Update documentation, add tests. * [#449](https://github.com/cleverage/process-bundle/issues/449) `sprintf`: a width or a precision given as an argument (`%*d`, `%.*f`, `%1$*2$d`) is accepted by the validation of the `format` option (rejected since 5.7). Add tests. -### Fixes -* [#455](https://github.com/cleverage/process-bundle/issues/455) `cleverage:process:execute -o -t json-stream`: the invalid UTF-8 sequences of the output are replaced, as on STDOUT (a `JsonException` was thrown, also by the buffer of `process_launcher`); an output that cannot be encoded as JSON is reported on the error output. Update documentation, add tests. - v5.8 -----