LP#1350457: Pass caller's session to subrequests called via method_lookup
authorMike Rylander <mrylander@gmail.com>
Wed, 30 Jul 2014 17:29:46 +0000 (13:29 -0400)
committerGalen Charlton <gmc@esilibrary.com>
Thu, 4 Feb 2016 18:10:55 +0000 (13:10 -0500)
In the process of looking up a method for an internal subrequest, we lose
session info. This is a problem when the subrequest makes a remote request,
because then the subrequest can't look up the proper locale, among other
things. The forthcoming branch passes the caller's session to the subrequest.

This patch also teaches OpenSRF object registration how to strip certain
object members -- in particular, the session -- so that introspection
continues to work.

Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>

src/perl/lib/OpenSRF/Application.pm
src/perl/lib/OpenSRF/Utils/JSON.pm

index 5ae98bf..023bb8d 100644 (file)
@@ -171,6 +171,7 @@ sub handler {
 
                if (ref $coderef) {
                        my @args = $app_msg->params;
+                       $coderef->session( $session );
                        my $appreq = OpenSRF::AppRequest->new( $session );
                        $appreq->max_chunk_size( $coderef->max_chunk_size );
                        $appreq->max_chunk_count( $coderef->max_chunk_count );
@@ -436,7 +437,12 @@ sub register_method {
                ($args{object_hint} = $args{package}) =~ s/::/_/go;
        }
 
-       OpenSRF::Utils::JSON->register_class_hint( name => $args{package}, hint => $args{object_hint}, type => "hash" );
+       OpenSRF::Utils::JSON->register_class_hint(
+               strip => ['session'],
+               name => $app,
+               hint => $args{object_hint},
+               type => "hash"
+       );
 
        $_METHODS[$args{api_level}]{$args{api_name}} = bless \%args => $app;
 
@@ -558,6 +564,7 @@ sub method_lookup {
                $meth = $self->method_lookup($method,$proto,1);
        }
 
+       $meth->session($self->session) if $meth && ref($self); # Pass the caller's session
        return $meth;
 }
 
@@ -571,9 +578,7 @@ sub run {
        if ( !UNIVERSAL::isa($req, 'OpenSRF::AppRequest') ) {
                $log->debug("Creating a SubRequest object", DEBUG);
                unshift @params, $req;
-               $req = OpenSRF::AppSubrequest->new;
-               $req->session( $self->session ) if ($self->session);
-
+               $req = OpenSRF::AppSubrequest->new( session => $self->session );
        } else {
                $log->debug("This is a top level request", DEBUG);
        }
index 6411870..4efa3dc 100644 (file)
@@ -212,7 +212,14 @@ sub perl2JSONObject {
 
     if(UNIVERSAL::isa($obj, 'HASH')) {
         $jsonobj = {};
-        $jsonobj->{$_} = $pkg->perl2JSONObject($obj->{$_}) for (keys %$obj);
+        for my $k (keys %$obj) {
+            next if (
+                $ref ne 'HASH'
+                and exists $_class_map{classes}{$ref}{strip}
+                and grep { $k eq $_ } @{$_class_map{classes}{$ref}{strip}}
+            );
+            $jsonobj->{$k} = $pkg->perl2JSONObject($obj->{$k});
+        }
     } elsif(UNIVERSAL::isa($obj, 'ARRAY')) {
         $jsonobj = [];
         $jsonobj->[$_] = $pkg->perl2JSONObject($obj->[$_]) for(0..scalar(@$obj) - 1);