diff --git a/src/Jira/Api.php b/src/Jira/Api.php index f384d9c..78307d1 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -768,7 +768,7 @@ public function api( ) { $result = $this->client->sendRequest( $method, - $url, + $this->addQueryParametersToUrl($method, $url, $data), $data, $this->getEndpoint(), $this->authentication, @@ -801,6 +801,34 @@ public function api( return false; } + /** + * Adds the query parameters to the URL. + * + * @param string $method Request method. + * @param string $url URL. + * @param array|string $data Data. + * + * @return string + */ + protected function addQueryParametersToUrl($method, $url, array &$data) + { + if ( !array_key_exists('_query', $data) ) { + return $url; + } + + $query_parameters = $data['_query']; + unset($data['_query']); + + // For GET requests all given parameters end up in Query parameters. + if ( $method === self::REQUEST_GET ) { + $data = $query_parameters + $data; + + return $url; + } + + return $url . (strpos($url, '?') === false ? '?' : ':') . http_build_query($query_parameters); + } + /** * Downloads attachment. * diff --git a/tests/Jira/ApiTest.php b/tests/Jira/ApiTest.php index 51ac969..25646d0 100644 --- a/tests/Jira/ApiTest.php +++ b/tests/Jira/ApiTest.php @@ -392,6 +392,74 @@ public function testDeleteWorkLogWithCustomParams() $this->assertEquals(json_decode($response, true), $actual, 'The response is json-decoded.'); } + public function testQueryParametersHandlingForGetRequestMethod() + { + $this->expectClientCall( + Api::REQUEST_GET, + '/rest/api/2/something', + array( + 'q_p1' => 'q_p1_v', + 'q_p2' => 'q_p2_v', + 'rb_p1' => 'rb_p1_v', + 'rb_p2' => 'rb_p2_v', + 'rb_p3' => 'rb_p3_v', + ), + '{}' + ); + + $this->api->api( + Api::REQUEST_GET, + '/rest/api/2/something', + array( + '_query' => array('q_p1' => 'q_p1_v', 'q_p2' => 'q_p2_v'), + 'rb_p1' => 'rb_p1_v', + 'rb_p2' => 'rb_p2_v', + 'rb_p3' => 'rb_p3_v', + ), + true + ); + } + + /** + * @dataProvider queryParametersHandlingForOtherRequestMethodsDataProvider + * + * @param string $request_method Request method. + */ + public function testQueryParametersHandlingForOtherRequestMethods($request_method) + { + $this->expectClientCall( + $request_method, + '/rest/api/2/something?q_p1=q_p1_v&q_p2=q_p2_v', + array( + 'rb_p1' => 'rb_p1_v', + 'rb_p2' => 'rb_p2_v', + 'rb_p3' => 'rb_p3_v', + ), + '{}' + ); + + $this->api->api( + $request_method, + '/rest/api/2/something', + array( + '_query' => array('q_p1' => 'q_p1_v', 'q_p2' => 'q_p2_v'), + 'rb_p1' => 'rb_p1_v', + 'rb_p2' => 'rb_p2_v', + 'rb_p3' => 'rb_p3_v', + ), + true + ); + } + + public static function queryParametersHandlingForOtherRequestMethodsDataProvider() + { + return array( + 'delete' => array(Api::REQUEST_DELETE), + 'post' => array(Api::REQUEST_POST), + 'put' => array(Api::REQUEST_PUT), + ); + } + /** * Expects a particular client call. *