khrt / Raisin

Raisin - a REST API micro framework for Perl 🐫 🐪
61 stars 30 forks source link

Inconsistent behavior of present when no rows are found #47

Closed davel closed 5 years ago

davel commented 5 years ago

If the result of a DBIx::Class query given to present returns no rows, the generated JSON is undef, rather than an empty array. Can the behaviour be changed to make it return [], so as to make the behaviour consistent?

Many thanks, Dave

khrt commented 5 years ago

Could you tell me what is the actual value of the result prior you pass it to present?

davel commented 5 years ago

The object passed to present is a DBIx::Class::ResultSet.

I've added a search feature to your music demo app to demonstrate this, see my fork at https://github.com/davel/Raisin

Multiple results, array returned as expected,

curl -H "Accept: text/json" http://0:5000/artists?name=Green%
{"count":1,"data":[{"hash":"20","artist":"Green Day","id":2,"albums":[{"title":"39/Smooth","id":4,"year":1990},{"title":"Kerplunk","id":5,"year":1992},{"title":"Dookie","id":6,"year":1994},{"title":"Insomniac","id":7,"year":1995},{"title":"Nimrod","id":8,"year":1997},{"title":"Warning","id":9,"year":2000},{"title":"American Idiot","id":10,"year":2004},{"title":"21st Century Breakdown","id":11,"year":2009}]}]}

No results, null returned rather than [],

curl -H "Accept: text/json" http://0:5000/artists?name=asdf
{"count":0,"data":null}
khrt commented 5 years ago

I guess that happens because DBIx::Class's search returns undef.

I'll take a look at it later, or if you can dump the $artists after https://github.com/davel/Raisin/blob/master/examples/music-app/script/music_app_dbix.psgi#L38 it would also be very helpful.

davel commented 5 years ago

I thought that might be the case too. However, if $artists was undef, the call to $artists->count on line 41 would fail. ref($artists) confirms that it is a DBIx::Class::ResultSet.

khrt commented 5 years ago

@davel could you verify #48 fixes your issue?

davel commented 5 years ago

Yes, that fixes my problem, thank you.