separate request/response possible content-typees fix-accepted-content-types mirror/fix-accepted-content-types
Joel Bernstein [Mon, 25 Apr 2011 17:46:26 +0000 (18:46 +0100)]
lib/Catalyst/Action/SerializeBase.pm
lib/Catalyst/TraitFor/Request/REST.pm

index 1da09ad..3bb3427 100644 (file)
@@ -69,7 +69,12 @@ sub _load_content_plugins {
         push @accepted_types, @$stashed;
     }
     # then content types requested by caller
-    push @accepted_types, @{ $c->request->accepted_content_types };
+    push @accepted_types, 
+        $search_path =~ m{Deserialize} 
+            ?  @{ $c->request->accepted_content_types }
+            :  @{ $c->request->accepted_response_content_types }
+            ; 
+
     # then the default
     push @accepted_types, $config->{'default'} if $config->{'default'};
     # pick the best match that we have a serializer mapping for
index 4a2ca87..1d35710 100644 (file)
@@ -24,14 +24,46 @@ has preferred_content_type => (
     init_arg => undef,
 );
 
-sub _build_accepted_content_types {
-    my $self = shift;
+has accepted_response_content_types => (
+    is       => 'ro',
+    isa      => 'ArrayRef',
+    lazy     => 1,
+    builder  => '_build_accepted_response_content_types',
+    init_arg => undef,
+);
 
-    my %types;
+has preferred_response_content_type => (
+    is       => 'ro',
+    isa      => 'Str',
+    lazy     => 1,
+    builder  => '_build_preferred_response_content_type',
+    init_arg => undef,
+);
+
+sub _accepted_types_sort {
+    my ($self, %types) = @_;
+    [ sort { $types{$b} <=> $types{$a} } keys %types ];
+}
 
+sub _build_accepted_content_types {
+    my $self = shift;
+    my %types = $self->_accepted_response_content_types_inner;
     # First, we use the content type in the HTTP Request.  It wins all.
     $types{ $self->content_type } = 3
         if $self->content_type;
+    $self->_accepted_types_sort(%types);
+}
+
+sub _build_accepted_response_content_types {
+    my $self = shift;
+    my %types = $self->_accepted_response_content_types_inner;
+    $self->_accepted_types_sort(%types);
+}
+
+sub _accepted_response_content_types_inner {
+    my $self = shift;
+
+    my %types;
 
     if ($self->method eq "GET" && $self->param('content-type')) {
         $types{ $self->param('content-type') } = 2;
@@ -66,11 +98,13 @@ sub _build_accepted_content_types {
         }
     }
 
-    [ sort { $types{$b} <=> $types{$a} } keys %types ];
+    %types;
 }
 
 sub _build_preferred_content_type { $_[0]->accepted_content_types->[0] }
 
+sub _build_preferred_response_content_type { $_[0]->accepted_response_content_types->[0] }
+
 sub accepts {
     my $self = shift;
     my $type = shift;