Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option expose_nil doesn't work when block is passed #329

Merged
merged 3 commits into from
Feb 17, 2020

Conversation

serbiant
Copy link
Contributor

Greetings,

I've been trying to implement a conditional exposure at one of the entities in my project.
Noticed that in the current implementation, this code

expose :personal_details, expose_nil: false do |address, options|
  if options[:one_option].present?
    Public::Entities::PersonalDetail.represent address.personal_details
  elsif options[another_option].present?
    Public::Entities::PersonalDetailShort.represent address.personal_details
  else
    nil # explicit for the example
  end
end

will eventually expose null if both conditions are not satisfied.

This PR fixes the specified issue.

@serbiant serbiant requested a review from LeFnord January 21, 2020 14:04
@coveralls
Copy link

coveralls commented Feb 17, 2020

Coverage Status

Coverage increased (+0.01%) to 95.869% when pulling 3e2499f on serbiant:master into 54f68b2 on ruby-grape:master.

@LeFnord
Copy link
Member

LeFnord commented Feb 17, 2020

thanks @serbiant

@LeFnord LeFnord merged commit 96afd88 into ruby-grape:master Feb 17, 2020
@Morxander
Copy link

@serbiant I'm using the latest version 0.8.0 which includes your fix but it's still exposing null.

@norydev
Copy link
Contributor

norydev commented Apr 17, 2020

I think you broke something that was working.

Before this change:

class PostEntity < Grape::Entity
  expose :title
end

class CommentEntity < Grape::Entity
  expose :post, expose_nil: false do |object, options|
    PostEntity.represent(object.post, options)
  end
end

Now let's imagine a comment with no post for some reason, in that scenario, PostEntity.represent(object.post, options) is not nil, it's an instance of PostEntity. But it evaluates to nil as object.post is nil

This used to work fine before this change, but now it doesn't as it thinks the block does not return nil. I think you just check nil evaluation in a different place.

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.

5 participants