From 4aea02e1e152b97242dba77985b70c53a666b012 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Mon, 26 Apr 2021 16:00:57 -0700 Subject: [PATCH 1/4] ToJSON: serialize boolean_columns to JSON boolean --- src/lib/Hydra/Component/ToJSON.pm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib/Hydra/Component/ToJSON.pm b/src/lib/Hydra/Component/ToJSON.pm index ed8d59d9..f6920943 100644 --- a/src/lib/Hydra/Component/ToJSON.pm +++ b/src/lib/Hydra/Component/ToJSON.pm @@ -5,6 +5,7 @@ use strict; use warnings; use base 'DBIx::Class'; +use JSON; sub TO_JSON { my $self = shift; @@ -17,6 +18,10 @@ sub TO_JSON { $json{$column} = $self->get_column($column); } + foreach my $column (@{$hint->{boolean_columns}}) { + $json{$column} = $self->get_column($column) ? JSON::true : JSON::false; + } + foreach my $relname (keys %{$hint->{relations}}) { my $key = $hint->{relations}->{$relname}; $json{$relname} = [ map { $_->$key } $self->$relname ]; From f2b9649bf2741afece17043818b0013796cae8c1 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Mon, 26 Apr 2021 16:01:50 -0700 Subject: [PATCH 2/4] Projects: serialize `enabled` and `hidden` as boolean --- src/lib/Hydra/Schema/Projects.pm | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib/Hydra/Schema/Projects.pm b/src/lib/Hydra/Schema/Projects.pm index 697f1aee..f8828428 100644 --- a/src/lib/Hydra/Schema/Projects.pm +++ b/src/lib/Hydra/Schema/Projects.pm @@ -252,10 +252,12 @@ my %hint = ( "displayname", "description", "homepage", - "enabled", - "hidden", "owner" ], + boolean_columns => [ + "enabled", + "hidden" + ], relations => { jobsets => "name" } From f0ede5f8b8dde66c5dcddec67e3eaebaab4c3738 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Mon, 26 Apr 2021 16:29:04 -0700 Subject: [PATCH 3/4] t: Test Projects JSON serialization and deserialization --- t/Controller/projects.t | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 t/Controller/projects.t diff --git a/t/Controller/projects.t b/t/Controller/projects.t new file mode 100644 index 00000000..719d8dd8 --- /dev/null +++ b/t/Controller/projects.t @@ -0,0 +1,38 @@ +use feature 'unicode_strings'; +use strict; +use Setup; +use JSON qw(decode_json); + +my %ctx = test_init(); + +require Hydra::Schema; +require Hydra::Model::DB; +require Hydra::Helper::Nix; +use HTTP::Request::Common; + +use Test2::V0; +require Catalyst::Test; +Catalyst::Test->import('Hydra'); + +my $db = Hydra::Model::DB->new; +hydra_setup($db); + +my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"}); + +my $projectinfo = request(GET '/project/tests', + Accept => 'application/json', +); + +ok($projectinfo->is_success); +is(decode_json($projectinfo->content), { + description => undef, + displayname => "", + enabled => JSON::true, + hidden => JSON::false, + homepage => undef, + jobsets => [], + name => "tests", + owner => "root" +}); + +done_testing; From 50fab154a40eb963c1855887196d8579b5453d84 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Mon, 26 Apr 2021 16:37:25 -0700 Subject: [PATCH 4/4] ToJSON: serialize string_columns to JSON If the column is undefined, then it should be an empty string according to your API spec. --- src/lib/Hydra/Component/ToJSON.pm | 4 ++++ src/lib/Hydra/Schema/Projects.pm | 2 +- t/Controller/projects.t | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/lib/Hydra/Component/ToJSON.pm b/src/lib/Hydra/Component/ToJSON.pm index f6920943..e8f439de 100644 --- a/src/lib/Hydra/Component/ToJSON.pm +++ b/src/lib/Hydra/Component/ToJSON.pm @@ -18,6 +18,10 @@ sub TO_JSON { $json{$column} = $self->get_column($column); } + foreach my $column (@{$hint->{string_columns}}) { + $json{$column} = $self->get_column($column) // ""; + } + foreach my $column (@{$hint->{boolean_columns}}) { $json{$column} = $self->get_column($column) ? JSON::true : JSON::false; } diff --git a/src/lib/Hydra/Schema/Projects.pm b/src/lib/Hydra/Schema/Projects.pm index f8828428..e9857766 100644 --- a/src/lib/Hydra/Schema/Projects.pm +++ b/src/lib/Hydra/Schema/Projects.pm @@ -247,7 +247,7 @@ __PACKAGE__->many_to_many("usernames", "projectmembers", "username"); # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:+4yWd9UjCyxxLZYDrVUAxA my %hint = ( - columns => [ + string_columns => [ "name", "displayname", "description", diff --git a/t/Controller/projects.t b/t/Controller/projects.t index 719d8dd8..02e60b45 100644 --- a/t/Controller/projects.t +++ b/t/Controller/projects.t @@ -25,11 +25,11 @@ my $projectinfo = request(GET '/project/tests', ok($projectinfo->is_success); is(decode_json($projectinfo->content), { - description => undef, + description => "", displayname => "", enabled => JSON::true, hidden => JSON::false, - homepage => undef, + homepage => "", jobsets => [], name => "tests", owner => "root"