sumoheavy / jira-ruby

A Ruby gem for the JIRA REST API
MIT License
654 stars 412 forks source link

Load Active Support's Object extensions #435

Closed jcouball closed 4 months ago

jcouball commented 4 months ago

If active_support/core_ext/object was not already loaded and there is an http error, the following undefined method error would be raised:

/home/xxxx/.rvm/gems/ruby-3.2.2/gems/jira-ruby-2.3.0/lib/jira/http_error.rb:11:in `initialize': undefined method `presence' for "Bad Request":String (NoMethodError)

      @message = response.try(:message).presence || response.try(:body)
                                       ^^^^^^^^^

The presence method is defined by Active Support's Object extensions.

To reproduce this issue, in the root of the project repository, save the following code to a file (say test.rb):

require 'jira-ruby'

response = Struct.new(:message, :body).new('my message', 'my body')

JIRA::HTTPError.new(response)

and then run with the following command run in the repository's root folder:

ruby -Ilib test.rb

This results in the following error:

/Users/couballj/SynologyDrive/Documents/Projects/jira-ruby/lib/jira/http_error.rb:11:in `initialize': undefined method `presence' for an instance of String (NoMethodError)

      @message = response.try(:message).presence || response.try(:body)
                                       ^^^^^^^^^

This PR adds the require statement to load these Active Support extensions.

After this change, there is no error when the above script is run.

jcouball commented 4 months ago

@bobbrodie this PR is ready for review.

bobbrodie commented 4 months ago

Fantastic, thanks @jcouball -- I'll get this merged along with a few others for the upcoming release

bobbrodie commented 4 months ago

@jcouball just a heads up, this wasn't caught in CI because we haven't yet merged the preparation for v3.0.0 which runs on pull_request, but this introduces failure:

  1) JIRA::Base converts to json
     Failure/Error: expect(h.to_json).to eq(h_attrs.to_json)

       expected: "{\"key\":{\"foo\":\"bar\",\"dead\":\"beef\"}}"
            got: "{\"key\":{\"client\":{\"__expired\":false,\"name\":\"client\"},\"attrs\":{\"foo\":\"bar\",\"dead\":\"beef\"},\"expanded\":false,\"deleted\":false}}"

Requiring active_support/core_ext/object on HTTPError is loading it across the project and changes the behavior of to_json.

I updated our test here: https://github.com/sumoheavy/jira-ruby/pull/436 to get it to pass. Note that Ruby 2.4 through 2.6 won't pass, but that's expected. We're planning to drop support for those in v3.0.0 since they're EOL.

jcouball commented 4 months ago

Thanks, @bobbrodie, for the info.