[pdm-devel] [PATCH proxmox 1/3] pve-api-types: schema2rust: generate arrays for types with format `-list`
Hannes Laimer
h.laimer at proxmox.com
Tue Oct 21 15:50:15 CEST 2025
Since [1] the PVE API does take in actual arrays for parameters with a
format that ends in `-list`. With this we generate `Vec<String>` for
those, return types are not affected, so they will still just generate a
`String`.
[1] pve-common 69d9edcc ("section config: implement array support")
Signed-off-by: Hannes Laimer <h.laimer at proxmox.com>
---
pve-api-types/generator-lib/Schema2Rust.pm | 35 ++++++++++++++++++++--
1 file changed, 33 insertions(+), 2 deletions(-)
diff --git a/pve-api-types/generator-lib/Schema2Rust.pm b/pve-api-types/generator-lib/Schema2Rust.pm
index c5ac4aad..73ca4150 100644
--- a/pve-api-types/generator-lib/Schema2Rust.pm
+++ b/pve-api-types/generator-lib/Schema2Rust.pm
@@ -39,6 +39,7 @@ my $dedup_enum = {};
my $dedup_array_types = {};
our $__err_path = '';
+our $__list_format_as_array = undef;
my sub to_doc_comment : prototype($);
my sub strip_doc_comment : prototype($);
@@ -504,11 +505,12 @@ my sub print_method_without_body : prototype($$$$$) {
my $arg = $fields->{$name};
my $rust_name = $arg->{rust_name};
-
if ($arg->{type} eq 'Option<bool>') {
print {$out} " .maybe_bool_arg(\"$name\", p_$rust_name)\n";
} elsif ($arg->{is_string_list}) {
print {$out} " .maybe_list_arg(\"$name\", p_$rust_name)\n";
+ } elsif ($arg->{type} =~ /^Option<Vec<.*>>$/) {
+ print {$out} " .maybe_list_arg(\"$name\", &p_$rust_name)\n";
} elsif ($arg->{optional}) {
print {$out} " .maybe_arg(\"$name\", &p_$rust_name)\n";
} else {
@@ -534,7 +536,7 @@ my sub print_method_without_body : prototype($$$$$) {
if ($arg->{type} eq 'Option<bool>') {
print {$out} " .maybe_bool_arg(\"$name\", $rust_name)\n";
- } elsif ($arg->{is_string_list}) {
+ } elsif ($arg->{is_string_list} || $arg->{type} =~ /^Option<Vec<.*>>$/) {
print {$out} " .maybe_list_arg(\"$name\", &$rust_name)\n";
} elsif ($arg->{optional}) {
print {$out} " .maybe_arg(\"$name\", &$rust_name)\n";
@@ -809,6 +811,22 @@ my sub type_of : prototype($) {
my ($schema) = @_;
my $kind = $schema->{type};
+
+ # If type is 'string' but format ends with `-list`, treat as array
+ # but only for inputs to endpoints.
+ #
+ # We want a typed Vec<String>, and since [1] the PVE API does accept
+ # actual arrays for parameters with a format ending in `-list`.
+ # This is only for inputs though, so we can only have Vec's for
+ # inputs, returns are still string lists.
+ #
+ # [1] pve-common 69d9edcc ("section config: implement array support")
+ if ($kind eq 'string' && defined($schema->{format}) && $schema->{format} =~ /-list$/) {
+ if (defined($__list_format_as_array) && $__list_format_as_array) {
+ return 'array';
+ }
+ }
+
return $kind if $kind;
if (exists $schema->{properties}) {
@@ -1132,6 +1150,15 @@ my sub array_type : prototype($$$) {
optional => undef,
description => '',
};
+ if (!$schema->{items} && $schema->{format} =~ /-list$/) {
+ my $format_name = $schema->{format};
+ $format_name =~ s/-list$//;
+ $schema->{items} = {
+ description => "List item of type $format_name.",
+ format => $format_name,
+ type => 'string',
+ };
+ }
my $items = delete $schema->{items} or die "missing 'items' in array schema\n";
my $description = $items->{description};
@@ -1578,6 +1605,8 @@ my sub url_parameters : prototype($) {
my sub method_parameters : prototype($$$$$) {
my ($def, $api_url, $param_name, $api_method, $rust_method_name) = @_;
+ local $__list_format_as_array = 1;
+
my $url_params = url_parameters($api_url);
my $parameters = $api_method->{parameters} // {};
@@ -1676,6 +1705,8 @@ my sub method_parameters : prototype($$$$$) {
my sub method_return_type : prototype($$$$) {
my ($def, $method, $return_name, $extra) = @_;
+ local $__list_format_as_array = 0;
+
if (defined(my $returns = $extra->{'output-type'})) {
$def->{output_type} = $returns;
return;
--
2.47.3
More information about the pdm-devel
mailing list