khrt / Raisin

Raisin - a REST API micro framework for Perl 🐫 🐪
62 stars 31 forks source link

Presenting DBIx::Class::ResultSet does not work correctly #110

Closed bbrtj closed 3 years ago

bbrtj commented 3 years ago

The example from MusicApp with DBIx::Class does not work correctly.

my $artists = $schema->resultset('Artist');

present data => $artists, with => 'MusicApp::Entity::Artist';
present count => $artists->count;

This causes an error for empty resultsets and incorrect output for anything else.

The reason seem to be Raisin::Entity, line 148:

bbrtj commented 3 years ago

I'm not sure why doesn't this work, seen some previous discussion in issues where someone claimed it did actually work, but seems like there been no significant change to this in DBIx::Class recently, and no change in this Raisin code for the last 7 years.

To be precise, I did not run the MusicApp directly, but used its code for my own needs with PostgreSQL. The entities and models are a bit different, but the general way they're used is the same as in MusicApp.

bbrtj commented 3 years ago

I was missing two things:

So the behavior is very weird and needs to be patched / documented because looks like a bug at first glance:

khrt commented 3 years ago

Thanks for reporting! The bug was there from the very beginning and must be fixed now.

The new version is released to CPAN as 0.92.

bbrtj commented 3 years ago

@khrt Thank you, however I still don't think if the change you've introduced is enough, since the chain $data->first->columns_info is erroneous if there are no rows in the result (since it's pretty much undef->columns_info then and there seem to be no checks whether the ResultSet is empty). It would've fixed the non-visible first element in my case, but not the exception with no rows in the database.

khrt commented 3 years ago

@brtastic, right. I skipped that case, let me see what I can do to fix it.

khrt commented 3 years ago

Should be fixed now with the recent change.