From d8e5d8ef254a7b537185036b2ee86e5206d62940 Mon Sep 17 00:00:00 2001 From: Flaviu Mates Date: Wed, 30 Oct 2019 17:02:11 +0200 Subject: [PATCH] TT#69423 - Implement yaml support for API swagger * Add yaml format response when accessing '/api/?swagger=yml' Change-Id: I2dae15ae8de9424020d11bc9682ef51350e3fdab (cherry picked from commit a87b41ce23d31c168dd632bbe4ea0d60d94b3dfd) --- lib/NGCP/Panel/Controller/API/Root.pm | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/Root.pm b/lib/NGCP/Panel/Controller/API/Root.pm index 52c9232a4b..f53b0e2888 100644 --- a/lib/NGCP/Panel/Controller/API/Root.pm +++ b/lib/NGCP/Panel/Controller/API/Root.pm @@ -9,7 +9,8 @@ use HTTP::Headers qw(); use HTTP::Response qw(); use HTTP::Status qw(:constants); use File::Find::Rule; -use JSON qw(to_json); +use JSON qw(to_json encode_json); +use YAML::XS qw/Dump/; use Safe::Isa qw($_isa); use NGCP::Panel::Utils::API; use parent qw/Catalyst::Controller NGCP::Panel::Role::API/; @@ -292,13 +293,18 @@ sub swagger :Private { $user_role, ); - use JSON qw/encode_json/; - - $c->response->headers(HTTP::Headers->new( - Content_Language => 'en', - Content_Type => 'application/json', - )); - $c->response->body(encode_json($result)); + my $headers = HTTP::Headers->new(Content_Language => 'en'); + my $response; + if ($c->req->params->{swagger} eq 'yml') { + $headers->header(Content_Type => 'application/x-yaml'); + $response = Dump($result); + } + else { + $headers->header(Content_Type => 'application/json'); + $response = encode_json($result); + } + $c->response->headers($headers); + $c->response->body($response); $c->response->code(200); return; }