From e8d734269576bde2612dcd60cdffff9c69e25d93 Mon Sep 17 00:00:00 2001 From: Irina Peshinskaya Date: Fri, 17 Feb 2017 07:34:23 +0200 Subject: [PATCH] TT#9309 Use one root attribute to control array representation Change-Id: I8a4a8ba3c020e2266c01c8468a37045a68e84a31 --- lib/Data/HAL.pm | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/Data/HAL.pm b/lib/Data/HAL.pm index 696fabb..e852d7b 100644 --- a/lib/Data/HAL.pm +++ b/lib/Data/HAL.pm @@ -31,8 +31,8 @@ has('resource', is => 'rw', isa => HashRef, default => sub {return {};}); has('relation', is => 'rw', isa => InstanceOf['Data::HAL::URI'], coerce => $uri_from_str); has('_nsmap', is => 'rw', isa => InstanceOf['Data::HAL::URI::NamespaceMap']); has('_recursing', is => 'ro', isa => Bool); - -has('_forcearray', is => 'rw', isa => Bool, default => 0); #array of embedded items, even if only one +has('_forcearray', is => 'rw', isa => Bool, default => 0 ); #array of items, even if only one. Used on the child level. +has('_forcearray_underneath', is => 'rw', isa => HashRef ); #{ all => 1 } means all sub BUILD { my ($self) = @_; @@ -175,11 +175,30 @@ sub _to_nested { push @{ $hal->{"_$prop"}{$r} }, $attr, $nested; } } else { - if ($p->$_can('_forcearray') and $p->_forcearray) { - $hal->{"_$prop"}{$r} = [ $nested ]; - } else { - $hal->{"_$prop"}{$r} = $nested; - } + my $forcearray; + if ( $p->$_can('_forcearray') && $p->_forcearray ){ + $forcearray = 1; + } elsif ( $p->$_can('forcearray_policy') ) { + $forcearray = $p->forcearray_policy($root,$prop,$r); + } elsif ( $root->$_can('forcearray_policy') ) { + $forcearray = $root->forcearray_policy($prop,$r,$p); + } elsif ( $root->_forcearray_underneath ) { + if( $forcearray = $root->_forcearray_underneath->{all} ){ + $forcearray = 1; + } elsif ( exists $root->_forcearray_underneath->{$prop} ){ + if ( 'HASH' eq ref $root->_forcearray_underneath->{$prop} ){ + #order of ifs allows to deny particular resource arraify + if ( exists $root->_forcearray_underneath->{$prop}->{$r} ){ + $forcearray = $root->_forcearray_underneath->{$prop}->{$r}; + } elsif ( exists $root->_forcearray_underneath->{$prop}->{all} ){ + $forcearray = 1; + } + } else { + $forcearray = $root->_forcearray_underneath->{$prop}; + } + } + } + $hal->{"_$prop"}{$r} = $forcearray ? [ $nested ] : $nested; } } } @@ -217,12 +236,12 @@ sub http_headers { } else { push @headers, 'Content-Type' => 'application/hal+json; charset=utf-8'; } - unless(exists $params{skip_links} && $params{skip_links}) { - push @headers, - map { (Link => $_->as_http_link_value) } - grep { 'curies' ne $_->relation->as_string } - @{ $self->links }; - } + unless(exists $params{skip_links} && $params{skip_links}) { + push @headers, + map { (Link => $_->as_http_link_value) } + grep { 'curies' ne $_->relation->as_string } + @{ $self->links }; + } } return @headers; }