From d5b47115ee3ebf9db53544d55c827f748446dd52 Mon Sep 17 00:00:00 2001 From: Maxime Besson Date: Tue, 25 Aug 2020 16:09:06 +0200 Subject: [PATCH] Prettify json on manager conf export (#2292) --- .../lib/Lemonldap/NG/Common/Conf/RESTServer.pm | 4 ++-- .../lib/Lemonldap/NG/Common/PSGI.pm | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/RESTServer.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/RESTServer.pm index 72a4bb26b..ebc2adb85 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/RESTServer.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/RESTServer.pm @@ -769,8 +769,8 @@ sub metadata { return $self->sendJSONresponse( $req, $self->currentConf, - forceJSON => 1, - headers => [ + pretty => 1, + headers => [ 'Content-Disposition' => "Attachment; filename=lmConf-$c.json" ], ); diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI.pm index 045e400e8..f3959ceb2 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI.pm @@ -125,7 +125,23 @@ sub sendJSONresponse { $args{headers} ||= [ $req->spliceHdrs ]; my $type = 'application/json; charset=utf-8'; if ( ref $j ) { - eval { $j = $_json->encode($j); }; + eval { + if ( $args{pretty} ) { + + # This avoids changing the settings of the $_json reference + $j = to_json( + $j, + { + allow_nonref => 1, + pretty => 1, + canonical => 1 + } + ); + } + else { + $j = $_json->encode($j); + } + }; return $self->sendError( $req, $@ ) if ($@); } return [ $args{code}, [ 'Content-Type' => $type, @{ $args{headers} } ],