fix #6305: cli formatter: avoid encoding YAML output twice
YAML::XS::Dump already returns UTF-8 encoded octets, but its output
still got encoded once more. This is a leftover from when the YAML
output was generated by CPAN::Meta::YAML, which returns a character
string.
As a result, all non-ASCII characters got mangled with the 'yaml'
output format, for example a cluster description with a German umlaut
when running:
pvesh get /cluster/options --output-format yaml
Print the output of YAML::XS::Dump as is, like it is already done for
the 'json' output format, and add a test for the encoding of both.
Fixes: 99d0252 ("properly encode YAML via YAML::XS")
Signed-off-by: Michal Fox <me@dualfroz.com>
This commit is contained in:
1 parent
92e0c082df
commit
2ad66a1cb4
2 files changed
+17
-1
No files matched your search
@@ -380,7 +380,8 @@ sub print_api_result {
|
||||
}
|
||||
|
||||
if ($format eq 'yaml') {
|
||||
print encode('UTF-8', YAML::XS::Dump($data));
|
||||
# Note: YAML::XS already returns UTF-8 encoded output
|
||||
print YAML::XS::Dump($data);
|
||||
} elsif ($format eq 'json') {
|
||||
# Note: we always use utf8 encoding for json format
|
||||
print to_json($data, { utf8 => 1, allow_nonref => 1, canonical => 1 }) . "\n";
|
||||
|
||||
@@ -7,6 +7,7 @@ use lib '../src';
|
||||
use PVE::JSONSchema;
|
||||
use PVE::CLIFormatter;
|
||||
|
||||
use Encode qw(decode);
|
||||
use Test::More;
|
||||
use Test::MockModule;
|
||||
|
||||
@@ -53,4 +54,18 @@ foreach my $data (@$render_data) {
|
||||
is($actual, $expected, "string format '$renderer_name'");
|
||||
}
|
||||
|
||||
# test that non-ASCII characters survive the structured output formats
|
||||
my $unicode_data = { description => "Erkl\x{e4}rung" };
|
||||
for my $format (qw(json yaml)) {
|
||||
open(my $fh, '>', \my $output) or die "failed to open in-memory file - $!\n";
|
||||
my $stdout = select($fh);
|
||||
PVE::CLIFormatter::print_api_result(
|
||||
$unicode_data, undef, undef, { 'output-format' => $format }, {},
|
||||
);
|
||||
select($stdout);
|
||||
close($fh);
|
||||
|
||||
like(decode('UTF-8', $output), qr/Erkl\x{e4}rung/, "output format '$format' is UTF-8 encoded");
|
||||
}
|
||||
|
||||
done_testing();
|
||||
Reference in new issue
Block a user