]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIFormatter.pm
print api res: always allow result to be undef if optional
[pve-common.git] / src / PVE / CLIFormatter.pm
index 47021800956a806904aec6d885aadb2804bc8fa1..84dbed1e6e2af6fbaf63c2e7d3631247f94bd664 100644 (file)
@@ -2,12 +2,14 @@ package PVE::CLIFormatter;
 
 use strict;
 use warnings;
+
 use I18N::Langinfo;
 use POSIX qw(strftime);
 use CPAN::Meta::YAML; # comes with perl-modules
 
 use PVE::JSONSchema;
 use PVE::PTY;
+
 use JSON;
 use utf8;
 use Encode;
@@ -356,7 +358,7 @@ sub extract_properties_to_print {
 # takes formatting information from the results property of the call
 # if $props_to_print is provided, prints only those columns. otherwise
 # takes all fields of the results property, with a fallback
-# to all fields occuring in items of $data.
+# to all fields occurring in items of $data.
 sub print_api_list {
     my ($data, $result_schema, $props_to_print, $options, $terminal_opts) = @_;
 
@@ -409,8 +411,9 @@ sub print_api_result {
 
     my $format = $options->{'output-format'} // 'text';
 
-    if ($result_schema) {
+    if ($result_schema && defined($result_schema->{type})) {
        return if $result_schema->{type} eq 'null';
+       return if $result_schema->{optional} && !defined($data);
     } else {
        my $type = $guess_type->($data);
        $result_schema = { type => $type };