From e02397e6b9b84ffd94c122dd04a1e688abebf28b Mon Sep 17 00:00:00 2001 From: Marius BALTEANU Date: Mon, 5 Apr 2021 23:34:20 +0300 Subject: [PATCH] Fix undefined method 'split' when issue to id is numeric --- app/controllers/issue_relations_controller.rb | 9 +++++++- .../api_test/issue_relations_test.rb | 23 ++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/controllers/issue_relations_controller.rb b/app/controllers/issue_relations_controller.rb index bd605dbae..52be512f1 100644 --- a/app/controllers/issue_relations_controller.rb +++ b/app/controllers/issue_relations_controller.rb @@ -107,7 +107,14 @@ class IssueRelationsController < ApplicationController end def relation_issues_to_id - params[:relation].require(:issue_to_id).split(',').reject(&:blank?) + issue_to_id = params[:relation].require(:issue_to_id) + case issue_to_id + when String + issue_to_id = issue_to_id.split(',').reject(&:blank?) + when Integer + issue_to_id = [issue_to_id] + end + issue_to_id rescue ActionController::ParameterMissing => e # We return a empty array just to loop once and return a validation error # ToDo: Find a better method to return an error if the param is missing. diff --git a/test/integration/api_test/issue_relations_test.rb b/test/integration/api_test/issue_relations_test.rb index cc9a9a13b..80caaa9f4 100644 --- a/test/integration/api_test/issue_relations_test.rb +++ b/test/integration/api_test/issue_relations_test.rb @@ -42,7 +42,7 @@ class Redmine::ApiTest::IssueRelationsTest < Redmine::ApiTest::Base assert_difference('IssueRelation.count') do post( '/issues/2/relations.xml', - :params => {:relation => {:issue_to_id => 7, :relation_type => 'relates'}}, + :params => {:relation => {:issue_to_id => "7", :relation_type => 'relates'}}, :headers => credentials('jsmith') ) end @@ -57,6 +57,27 @@ class Redmine::ApiTest::IssueRelationsTest < Redmine::ApiTest::Base assert_select 'relation id', :text => relation.id.to_s end + test "POST /issues/:issue_id/relations.json with numeric issue to id should create the relation" do + assert_difference('IssueRelation.count') do + post( + '/issues/2/relations.json', + :params => {:relation => {:issue_to_id => 7, :relation_type => 'relates'}}, + :headers => credentials('jsmith'), + :as => :json + ) + end + + relation = IssueRelation.order('id DESC').first + assert_equal 2, relation.issue_from_id + assert_equal 7, relation.issue_to_id + assert_equal 'relates', relation.relation_type + + assert_response :created + assert_equal 'application/json', @response.media_type + json = ActiveSupport::JSON.decode(response.body) + assert_equal relation.id, json['relation']['id'] + end + test "POST /issues/:issue_id/relations.xml with failure should return errors" do assert_no_difference('IssueRelation.count') do post( -- 2.22.0