#732 Node Search via API for known node returns invalid or empty JSON

A few things going on here ... swagger submitting "false" query
params which intefered with truth testing by existence, and also the
Node search not using {results => {}} format response for template,
and also that response then containing DBIC objects which cannot be
translated to JSON without running HRI first.
This commit is contained in:
Oliver Gorwits
2020-07-06 19:00:38 +01:00
parent 650c419dc7
commit fc4bd4b342
2 changed files with 31 additions and 2 deletions

View File

@@ -16,6 +16,9 @@ use HTML::Entities (); # to ensure dependency is met
use URI::QueryParam (); # part of URI, to add helper methods
use Path::Class 'dir';
use Module::Load ();
use Data::Visitor::Tiny;
use Scalar::Util 'blessed';
use App::Netdisco::Util::Web qw/
interval_to_daterange
request_is_api
@@ -156,6 +159,13 @@ hook 'before' => sub {
}
};
# swagger submits "false" params whereas web UI does not - remove them
# so that code testing for param existence as truth still works.
hook 'before' => sub {
return unless request_is_api_report or request_is_api_search;
map {delete params->{$_} if params->{$_} eq 'false'} keys %{params()};
};
hook 'before_template' => sub {
# search or report from navbar, or reset of sidebar, can ignore params
return if param('firstsearch')
@@ -252,8 +262,25 @@ hook before_layout_render => sub {
my ($tokens, $html_ref) = @_;
return unless request_is_api_report or request_is_api_search;
${ $html_ref } =
$tokens->{results} ? (to_json $tokens->{results}) : {};
if (ref {} eq ref $tokens and exists $tokens->{results}) {
${ $html_ref } = to_json $tokens->{results};
}
elsif (ref {} eq ref $tokens) {
map {delete $tokens->{$_}}
grep {not blessed $tokens->{$_} or not $tokens->{$_}->isa('App::Netdisco::DB::ResultSet')}
keys %$tokens;
visit( $tokens, sub {
my ( $key, $valueref ) = @_;
$$valueref = [$$valueref->hri->all]
if blessed $$valueref and $$valueref->isa('App::Netdisco::DB::ResultSet');
});
${ $html_ref } = to_json $tokens;
}
else {
${ $html_ref } = '[]';
}
};
# workaround for Swagger plugin weird response body