fotinakis / jsonapi-serializers

Pure Ruby readonly serializers for the JSON:API spec.
MIT License
414 stars 91 forks source link

Passing number to id fails on empty? #107

Closed uhrohraggy closed 7 years ago

uhrohraggy commented 7 years ago

Regarding https://github.com/fotinakis/jsonapi-serializers/blob/master/lib/jsonapi-serializers/serializer.rb#L413

if you override and pass a numeric id to the serializer, it fails on the ! empty? "undefined method `empty?' for 1234:Fixnum"

I think it should be written simply as: data['id'] = serializer.id.to_s if serializer.id.present?

I bring it up as an issue in case I don't fully understand the context of why it was checked against !serializer.id.empty?

fotinakis commented 7 years ago

present? is a Rails construct, not available to us here. A fix for this could be:

data['id'] = serializer.id.to_s if serializer.id && !serializer.id.to_s.empty?
uhrohraggy commented 7 years ago

thanks @fotinakis for the clarification! I thought my assumptions were off... I agree, I think that would solve it, and can't think of a case where that would lead to false positives.

For now, I fixed it by calling to_s in the method definition that overrode id. thanks again!