Skip to content

Comments

use defined#1080

Closed
ambs wants to merge 3 commits intomasterfrom
PR/fix_return_0
Closed

use defined#1080
ambs wants to merge 3 commits intomasterfrom
PR/fix_return_0

Conversation

@ambs
Copy link
Member

@ambs ambs commented Dec 26, 2015

Fixes #1079

@xsawyerx
Copy link
Member

👍

Chance for test? :)

@ambs
Copy link
Member Author

ambs commented Dec 26, 2015

Will try for the first change, at least. Still searching for the current file to add the test in.

@veryrusty
Copy link
Member

This is within an around modifier for a Moo attribute which should allow $response->content to get the current content (if ever needed) but the current logic doesn't handle that well. I'd prefer an approach like the following, but this fails a couple of tests (which I'm chasing).

around content => sub {
    my ( $orig, $self ) = ( shift, shift );

    # called as getter
    @_ or return $self->$orig;

    # no serializer defined; encode content and set attribute
    $self->serializer
        or return $self->$orig( $self->encode_content(@_) );

    # serialize content and set attribute
    my $serialized = $self->serialize(@_);
    $self->is_encoded(1); # All serializers return byte strings
    return $self->$orig( defined $serialized ? $serialized : '' );
};

@ambs
Copy link
Member Author

ambs commented Dec 27, 2015

If you think it can take some time, please merge my PR first for a release, and then fix it correctly later. This is making some plugins to fail (like Dancer2::Plugin::Database).

Merry Christmas (after two days, but better later then never)

@veryrusty
Copy link
Member

And merry Christmas to you @ambs 😄

Git blame indicates that it is the work on allowing before serializer hooks ( #1053 ) that changed the behaviour here (see commit 5697359). My suggestion above almost undoes the changes in the around modifier in that commit, and the test failures I'm seeing were changes as a result of that commit. Looks like I need to write some more test cases first... (ping @mickeyn for thoughts)

@mickeyn
Copy link
Contributor

mickeyn commented Dec 27, 2015

Yeah, my commit didn't take the "0" case into account - so it's good to check the length (we can also check the content-length header to make sure we don't read content unless it's positive.

BTW, I have a local commit waiting to become PR that removes the 'around content' and makes it a nice 'ro' attribute with a writer (cause f*#!@ 'around's).

@veryrusty
Copy link
Member

$response->content gets called as a getter when the psgi response is being constructed. It doesn't make sense to be calling serializer again. Either the around modifier needs to check its being called as a getter, or replaced (best idea!).

@mickeyn did you have any tests for what the changes from #1053 allow ?

@xsawyerx
Copy link
Member

👍 everyone. :)

@mickeyn
Copy link
Contributor

mickeyn commented Dec 27, 2015

The hooks tests were updated to comply with the change (which basically covers the 'always called' behavior).

Do you think we're missing coverage there?

@veryrusty
Copy link
Member

@mickeyn yes: tests for an app with a before_serializer hook sets the content when nothing was returned from a route, but leaves the content to be serialized alone otherwise.

The changes to the around content modifier in commit 5697359 "always serialize", including if called as a getter. When the PSGI response arrayref is constructed within Dancer2::Core::Respose->to_psgi, it calls $self->content which triggers serialization again (actually twice, as there are two calls.). But when called as a getter there is no content so the hook in the suggested test case would replace whatever content was in the response (failing the test).

See https://github.com/PerlDancer/Dancer2/blob/master/lib/Dancer2/Core/Response.pm#L183
I suspect what's needed is the default empty content added there should be serialized if a serializer is defined, plus the around modifier changes to be reverted. But its late and I'm going to sleep on it..

@veryrusty veryrusty mentioned this pull request Dec 28, 2015
@veryrusty
Copy link
Member

Closing this now that #1082 has been merged.

@veryrusty veryrusty closed this Jan 5, 2016
@veryrusty veryrusty deleted the PR/fix_return_0 branch January 5, 2016 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants