fixed search attribute generation for nonexistent relationships
Alexander Hartmaier [Fri, 20 Aug 2010 14:57:16 +0000 (16:57 +0200)]
Changes
lib/Catalyst/Controller/DBIC/API/RequestArguments.pm
t/rest/list.t

diff --git a/Changes b/Changes
index d7afe1e..26d09ed 100644 (file)
--- a/Changes
+++ b/Changes
@@ -8,6 +8,7 @@ Revision history for Catalyst-Controller-DBIC-API: {{ $dist->version }}
 - Removed useless RPC index action
 - Fixed static configured page attribute not being used (RT#56226)
 - Test use_json_boolean true
+- Fixed search attribute generation for nonexistent relationships
 
 2.002002  2010-08-03 14:40:50 Europe/Vienna
 
index f490eed..e2b2775 100644 (file)
@@ -516,8 +516,9 @@ generate_column_parameters recursively generates properly aliased parameters for
         # build up condition
         foreach my $column (keys %$param)
         {
-            if($source->has_relationship($column))
+            if ($source->has_relationship($column))
             {
+                # check if the value isn't a hashref
                 unless (ref($param->{$column}) && reftype($param->{$column}) eq 'HASH')
                 {
                     $search_params->{join('.', $base, $column)} = $param->{$column};
@@ -534,10 +535,22 @@ generate_column_parameters recursively generates properly aliased parameters for
                     )
                 }};
             }
-            else
+            elsif ($source->has_column($column))
             {
                 $search_params->{join('.', $base, $column)} = $param->{$column};
             }
+            # might be a sql function instead of a column name
+            # e.g. {colname => {like => '%foo%'}}
+            else
+            {
+                # but only if it's not a hashref
+                unless (ref($param->{$column}) && reftype($param->{$column}) eq 'HASH') {
+                    $search_params->{join('.', $base, $column)} = $param->{$column};
+                }
+                else {
+                    die "$column is neither a relationship nor a column\n";
+                }
+            }
         }
 
         return $search_params;
index 7d8c68f..ab2aa4e 100644 (file)
@@ -73,7 +73,7 @@ my $track_list_url = "$base/api/rest/track";
 
 {
   my $uri = URI->new( $artist_list_url );
-  $uri->query_form({ 'search.cds.title' => 'Forkful of bees' });       
+  $uri->query_form({ 'search.cds.title' => 'Forkful of bees' });
   my $req = GET( $uri, 'Accept' => 'text/x-json' );
   $mech->request($req);
   cmp_ok( $mech->status, '==', 200, 'search related request okay' );
@@ -136,4 +136,24 @@ my $track_list_url = "$base/api/rest/track";
     is_deeply( $response, { list => \@expected_response, success => 'true', totalcount => 15 }, 'correct data returned for static configured paging' );
 }
 
+{
+    my $uri = URI->new( $artist_list_url );
+    $uri->query_form({ 'search.cds.track.title' => 'Suicidal' });
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 400, 'attempt with nonexisting relationship fails' );
+    my $response = JSON::Any->Load( $mech->content);
+    is_deeply( $response->{messages}, ["track is neither a relationship nor a column\n"], 'correct error message returned' );
+}
+
+{
+    my $uri = URI->new( $artist_list_url );
+    $uri->query_form({ 'search.cds.tracks.foo' => 'Bar' });
+    my $req = GET( $uri, 'Accept' => 'text/x-json' );
+    $mech->request($req);
+    cmp_ok( $mech->status, '==', 400, 'attempt with nonexisting column fails' );
+    my $response = JSON::Any->Load( $mech->content);
+    is_deeply( $response->{messages}, ['a database error has occured.'], 'correct error message returned' );
+}
+
 done_testing();