Merge pull request #933 from cole-h/boolean-json

Serialize `enabled` and `hidden` columns as boolean
This commit is contained in:
Graham Christensen 2021-04-26 19:44:59 -04:00 committed by GitHub
commit fa5811eabf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 3 deletions

View file

@ -5,6 +5,7 @@ use strict;
use warnings; use warnings;
use base 'DBIx::Class'; use base 'DBIx::Class';
use JSON;
sub TO_JSON { sub TO_JSON {
my $self = shift; my $self = shift;
@ -17,6 +18,14 @@ sub TO_JSON {
$json{$column} = $self->get_column($column); $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;
}
foreach my $relname (keys %{$hint->{relations}}) { foreach my $relname (keys %{$hint->{relations}}) {
my $key = $hint->{relations}->{$relname}; my $key = $hint->{relations}->{$relname};
$json{$relname} = [ map { $_->$key } $self->$relname ]; $json{$relname} = [ map { $_->$key } $self->$relname ];

View file

@ -247,15 +247,17 @@ __PACKAGE__->many_to_many("usernames", "projectmembers", "username");
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:+4yWd9UjCyxxLZYDrVUAxA # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:+4yWd9UjCyxxLZYDrVUAxA
my %hint = ( my %hint = (
columns => [ string_columns => [
"name", "name",
"displayname", "displayname",
"description", "description",
"homepage", "homepage",
"enabled",
"hidden",
"owner" "owner"
], ],
boolean_columns => [
"enabled",
"hidden"
],
relations => { relations => {
jobsets => "name" jobsets => "name"
} }

38
t/Controller/projects.t Normal file
View file

@ -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 => "",
displayname => "",
enabled => JSON::true,
hidden => JSON::false,
homepage => "",
jobsets => [],
name => "tests",
owner => "root"
});
done_testing;