Verified Commit db40a106 authored by Roman Lacko's avatar Roman Lacko
Browse files

API: Fix incorrect parameter handling

Array or hash parameters require '[]' suffix when passed to API. This
commit fixes problems when the suffix was not added for other than GET
requests (notably POST and PUT).
parent f19ff947
......@@ -202,10 +202,22 @@ sub create_uri {
# add query parameters
foreach my $param (@{$tmpl->{query} // []}) {
my $type = '';
if ($param =~ s/\[\]$//) {
$type = 'ARRAY';
} elsif ($param =~ s/\{\}//) {
$type = 'HASH';
}
if (exists $args->{$param}) {
if (ref $args->{$param} ne $type) {
croak "Argument '$param' is expected to be of type '$type',
but '" . ref($args->{$param}) . "' is provided";
}
if (ref $args->{$param} eq "ARRAY") {
foreach my $val (@{ $args->{$param} }) {
$uri->query_param_append($param, $val);
$uri->query_param_append("$param\[\]", $val);
}
} elsif (ref $args->{$param} eq "HASH") {
while (my ($key, $val) = each %{$args->{$param}}) {
......@@ -214,6 +226,7 @@ sub create_uri {
} else {
$uri->query_param_append($param => $args->{$param});
}
delete $args->{$param};
}
}
......@@ -221,6 +234,47 @@ sub create_uri {
return $uri;
}
sub _param_exists {
my ($self, $tmpl, $param) = @_;
return grep { $_ eq $param }
@{ $tmpl->{optional} // [] },
@{ $tmpl->{required} // [] };
}
sub process_post_args {
my ($self, $tmpl, $args) = @_;
my @result;
foreach my $param (keys %$args) {
if (ref $args->{$param} eq 'ARRAY') {
if (!$self->_param_exists($tmpl, $param . '[]')) {
croak "$param: Is an array but $param\[\] is not allowed for this request";
}
foreach my $value (@{ $args->{$param} }) {
push @result, "$param\[\]", $value;
}
} elsif (ref $args->{$param} eq 'HASH') {
if (!$self->_param_exists($tmpl, $param . '{}')) {
croak "$param: Is a hash but $param\{\} is not allowed for this request";
}
foreach my $key (sort keys %{ $args->{$param} }) {
push @result, "$param\[$key\]", $args->{$param}->{$key};
}
} else {
if (!$self->_param_exists($tmpl, $param)) {
croak "$param: Is a scalar but $param is not allowed for this request";
}
push @result, $param, $args->{$param};
}
}
return @result;
}
sub clean_data {
my ($self, $response) = @_;
......@@ -277,13 +331,17 @@ sub exec_request {
croak "Missing arguments [" . join(", ", @missing) . "] for '$tmpl->{name}'" if scalar @missing;
# validate optional arguments
my %allargs = map { $_ => 1 } (@{$tmpl->{required}}, @{$tmpl->{optional}});
my @extra = grep { !defined $allargs{$_} } keys %$args;
my %allargs = map { ($_ =~ s/(\[\]|\{\})$//gr) => 1 }
(@{$tmpl->{required}}, @{$tmpl->{optional}});
my @extra = grep { !defined $allargs{$_} }
keys %$args;
carp "Extra arguments [" . join(", ", @extra) . "] for '$tmpl->{name}'" if scalar @extra;
my @postdata = %$args;
my @postdata = $self->process_post_args($tmpl, $args);
my $data = [];
my $response;
my $data = [];
croak "Cannot request -iterator if -page is specified as well"
if $rtargs->{-iterator} && defined $rtargs->{-page};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment