sumoheavy / jira-ruby

A Ruby gem for the JIRA REST API
MIT License
656 stars 411 forks source link

TypeError: no implicit conversion of Array into Hash when blank ID is passed to JIRA::Base.find #195

Open adamniedzielski opened 7 years ago

adamniedzielski commented 7 years ago

Thank you for creating this gem!

We accidentally started passing nil to client.Project.find, in other words we did something like:

client.Project.find(nil)

The error message that we got made it quite difficult to debug the issue:

TypeError: no implicit conversion of Array into Hash
  from jira/base.rb:399:in `merge!'
  from jira/base.rb:399:in `set_attrs'
  from jira/base.rb:388:in `set_attrs_from_response'
  from jira/base.rb:340:in `fetch'
  from jira/base.rb:110:in `find'
  from jira/base_factory.rb:33:in `block (2 levels) in delegate_to_target_class'
  from app/models/project_services/jira_service.rb:56:in `block in jira_project'
  from app/models/project_services/jira_service.rb:304:in `jira_request'
  from app/models/project_services/jira_service.rb:56:in `jira_project'

It happens because when key passed to JIRA::Base.find is nil or empty string the constructed URL is /rest/api/2/project or "/rest/api/2/project/" respectively. In this case JIRA API response is an array of projects instead of a hash. This causes TypeError: no implicit conversion of Array into Hash exception in JIRA::Base#set_attrs.

My proposal is to add a check that key is not nil and not an empty string. Would you accept a patch that does that?

adamniedzielski commented 7 years ago

Issue from our issue tracker: https://gitlab.com/gitlab-org/gitlab-ce/issues/24777

SimonMiaou commented 7 years ago

Hey @adamniedzielski Sorry for the delay 😄 We are always happy to receive PRs