diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Open311.pm | 26 | ||||
-rw-r--r-- | t/app/controller/open311.t | 10 |
3 files changed, 23 insertions, 14 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index b16fa561d..788e4bb0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ POST service request. - Allow description in email template with placeholder. - Do not store display-only extra fields on new reports. + - Improve JSON output of controller. * v2.6 (3rd May 2019) - New features: diff --git a/perllib/FixMyStreet/App/Controller/Open311.pm b/perllib/FixMyStreet/App/Controller/Open311.pm index 841330e92..b4b5d5e3a 100644 --- a/perllib/FixMyStreet/App/Controller/Open311.pm +++ b/perllib/FixMyStreet/App/Controller/Open311.pm @@ -111,8 +111,6 @@ sub get_discovery : Private { { 'contact' => ["Send email to $contact_email."], 'changeset' => [$prod_changeset], - # XXX rewrite to match - 'key_service' => ["Read access is open to all according to our \u003Ca href='/open_data' target='_blank'\u003Eopen data license\u003C/a\u003E. For write access either: 1. return the 'guid' cookie on each call (unique to each client) or 2. use an api key from a user account which can be generated here: http://seeclickfix.com/register The unversioned url will always point to the latest supported version."], 'max_requests' => [ $c->config->{OPEN311_LIMIT} || 1000 ], 'endpoints' => [ { @@ -195,9 +193,7 @@ sub get_services : Private { ); } $c->forward( 'format_output', [ { - 'services' => [ { - 'service' => \@services - } ] + 'services' => \@services } ] ); } @@ -291,9 +287,7 @@ sub output_requests : Private { } $c->forward( 'format_output', [ { - 'requests' => [ { - 'request' => \@problemlist - } ] + service_requests => \@problemlist } ] ); } @@ -429,7 +423,21 @@ sub format_output : Private { $c->res->body( encode_json($hashref) ); } elsif ('xml' eq $format) { $c->res->content_type('application/xml; charset=utf-8'); - $c->res->body( XMLout($hashref, RootName => undef, NoAttr => 1 ) ); + my $group_tags = { + services => 'service', + attributes => 'attribute', + values => 'value', + service_requests => 'request', + errors => 'error', + service_request_updates => 'request_update', + }; + $c->res->body( XMLout($hashref, + KeyAttr => {}, + GroupTags => $group_tags, + SuppressEmpty => undef, + RootName => undef, + NoAttr => 1, + ) ); } else { $c->detach( 'error', [ sprintf(_('Invalid format %s specified.'), $format) diff --git a/t/app/controller/open311.t b/t/app/controller/open311.t index 79fe159a3..c052605c7 100644 --- a/t/app/controller/open311.t +++ b/t/app/controller/open311.t @@ -14,7 +14,7 @@ $mech->content_contains('<status>open</status>'); $mech->get_ok('/open311/v2/requests.json?jurisdiction_id=foo&status=open&agency_responsible=2237'); my $json = decode_json($mech->content); -my $problems = $json->{requests}[0]{request}; +my $problems = $json->{service_requests}; is @$problems, 2; like $problems->[0]{description}, qr/Around page Test/; @@ -25,7 +25,7 @@ subtest "non_public reports aren't available" => sub { }); $mech->get_ok('/open311/v2/requests.json?jurisdiction_id=foo'); $json = decode_json($mech->content); - $problems = $json->{requests}[0]{request}; + $problems = $json->{service_requests}; is @$problems, 1; like $problems->[0]{description}, qr/Around page Test/; $mech->content_lacks('This report is now private'); @@ -33,7 +33,7 @@ subtest "non_public reports aren't available" => sub { my $problem_id = $problem1->id; $mech->get_ok("/open311/v2/requests/$problem_id.json?jurisdiction_id=foo"); $json = decode_json($mech->content); - $problems = $json->{requests}[0]{request}; + $problems = $json->{service_requests}; is @$problems, 0; }; @@ -45,7 +45,7 @@ subtest "hidden reports aren't available" => sub { }); $mech->get_ok('/open311/v2/requests.json?jurisdiction_id=foo'); $json = decode_json($mech->content); - $problems = $json->{requests}[0]{request}; + $problems = $json->{service_requests}; is @$problems, 1; like $problems->[0]{description}, qr/Around page Test/; $mech->content_lacks('This report is now hidden'); @@ -53,7 +53,7 @@ subtest "hidden reports aren't available" => sub { my $problem_id = $problem1->id; $mech->get_ok("/open311/v2/requests/$problem_id.json?jurisdiction_id=foo"); $json = decode_json($mech->content); - $problems = $json->{requests}[0]{request}; + $problems = $json->{service_requests}; is @$problems, 0; }; |