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

Add optional chaining to fix NoMethodError #263

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

fdocr
Copy link

@fdocr fdocr commented May 10, 2024

Hi,

For some reason during my use of the gem I ran into this issue when trying to print out some entries while debugging. I can't seem to find an easy way to add spec coverage here but this solves a NoMethodError when properties are nil.

NoMethodError: undefined method `empty?' for nil
from /Users/XXXXXX/.rvm/gems/ruby-3.3.0/gems/contentful-management-3.10.0/lib/contentful/management/resource.rb:96:in `inspect'

Feel free to push specs for coverage or point me out how to do so if needed.

Thanks!

@rubydog
Copy link
Collaborator

rubydog commented May 22, 2024

Hi @fdocr, could you please share the code sample that raised the above error?

@fdocr
Copy link
Author

fdocr commented May 22, 2024

Sure, this is one example that raises the error on the current 3.10.0 version:

# Initial setup
client = Contentful::Management::Client.new(ENV["CONTENTFUL_TOKEN"], dynamic_entries: { ENV["CONTENTFUL_SPACE"] => "staging" })
environment = client.environments(ENV["CONTENTFUL_SPACE"]).find("staging")

# Sample one content type (issue happens with different content types not only the first one)
content_type = environment.content_types.all.first

# User specific query
user_id = "XXXXXXX"
entries = environment.entries.all("content_type" => content_type.id, 'sys.createdBy.sys.id' => user_id)

# Failure
entries.properties[:items].each { |entry| puts "INSPECTED ENTRY: #{entry.inspect}" }

With those few lines on a Rails console I see the following error:

NoMethodError: undefined method `empty?' for nil
from /Users/XXXXXX/.rvm/gems/ruby-3.3.0/gems/contentful-management-3.10.0/lib/contentful/management/resource.rb:96:in `inspect'

mgoudy91
mgoudy91 previously approved these changes May 23, 2024
@mgoudy91
Copy link

looks like a linting issue

image

@rubydog any thoughts on this? I'm not familiar enough with ruby to know

@rubydog
Copy link
Collaborator

rubydog commented May 27, 2024

@fdocr

properties&.empty? will actually do the opposite of what we intend to do. When properties are nil, properties&.empty? returns nil, which causes the else part of the if statement to execute. This is not the intended behavior.

@fdocr
Copy link
Author

fdocr commented May 27, 2024

That makes sense @rubydog. I didn't think that through since it fixed the NoMethodError on nil for me. I replaced it with:

properties_info = properties.nil? || properties.empty? ? '' : " @properties=#{properties.inspect}"

Rubocop seems to be okay with this. Please let me know if you have any alternatives for me to use instead. Thanks!

@rubydog rubydog requested a review from a team as a code owner July 9, 2024 12:27
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