Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 3f7b1c9

Browse files
committed
Fixes zendframework#187 - Zend_Rest_Server does not properly handle optional parameters
1 parent e78669d commit 3f7b1c9

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

library/Zend/Rest/Server.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,28 +189,35 @@ public function handle($request = false)
189189

190190
$func_args = $this->_functions[$this->_method]->getParameters();
191191

192+
// calling_args will be a zero-based array of the parameters
192193
$calling_args = array();
193194
$missing_args = array();
194-
foreach ($func_args as $arg) {
195+
foreach ($func_args as $i => $arg) {
195196
if (isset($request[strtolower($arg->getName())])) {
196-
$calling_args[] = $request[strtolower($arg->getName())];
197+
$calling_args[$i] = $request[strtolower($arg->getName())];
197198
} elseif ($arg->isOptional()) {
198-
$calling_args[] = $arg->getDefaultValue();
199+
$calling_args[$i] = $arg->getDefaultValue();
199200
} else {
200201
$missing_args[] = $arg->getName();
201202
}
202203
}
203204

205+
$anonymousArgs = array();
204206
foreach ($request as $key => $value) {
205207
if (substr($key, 0, 3) == 'arg') {
206208
$key = str_replace('arg', '', $key);
207-
$calling_args[$key] = $value;
209+
$anonymousArgs[$key] = $value;
208210
if (($index = array_search($key, $missing_args)) !== false) {
209211
unset($missing_args[$index]);
210212
}
211213
}
212214
}
213215

216+
// re-key the $anonymousArgs to be zero-based, and add in
217+
// any values already set in calling_args (optional defaults)
218+
ksort($anonymousArgs);
219+
$calling_args = array_values($anonymousArgs) + $calling_args;
220+
214221
// Sort arguments by key -- @see ZF-2279
215222
ksort($calling_args);
216223

tests/Zend/Rest/ServerTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,15 +541,15 @@ public function testMissingArgumentsShouldResultInFaultResponse()
541541
* @see ZF-1949
542542
* @group ZF-1949
543543
*/
544-
public function testMissingArgumentsWithDefaultsShouldNotResultInFaultResponse()
544+
public function testMissingAnonymousArgumentsWithDefaultsShouldNotResultInFaultResponse()
545545
{
546546
$server = new Zend_Rest_Server();
547547
$server->setClass('Zend_Rest_Server_Test');
548548
ob_start();
549549
$server->handle(array('method' => 'testFunc7', 'arg1' => "Davey"));
550550
$result = ob_get_clean();
551551
$this->assertContains('<status>success</status>', $result, var_export($result, 1));
552-
$this->assertContains('<response>Hello today, How are you Davey</response>', $result, var_export($result, 1));
552+
$this->assertContains('<response>Hello Davey, How are you today</response>', $result, var_export($result, 1));
553553
}
554554

555555
/**
@@ -577,6 +577,34 @@ public function testCallingNoMethodDoesNotThrowUnknownButSpecificErrorExceptionM
577577
$this->assertContains('<status>failed</status>', $response);
578578
$this->assertNotContains('<message>An unknown error occured. Please try again.</message>', $response);
579579
}
580+
581+
/**
582+
* @group GH-187
583+
*/
584+
public function testMissingZeroBasedAnonymousArgumentsWithDefaultsShouldNotResultInFaultResponse()
585+
{
586+
$server = new Zend_Rest_Server();
587+
$server->setClass('Zend_Rest_Server_Test');
588+
ob_start();
589+
$server->handle(array('method' => 'testFunc7', 'arg0' => "Davey"));
590+
$result = ob_get_clean();
591+
$this->assertContains('<status>success</status>', $result, var_export($result, 1));
592+
$this->assertContains('<response>Hello Davey, How are you today</response>', $result, var_export($result, 1));
593+
}
594+
595+
/**
596+
* @group GH-187
597+
*/
598+
public function testMissingNamesArgumentsWithDefaultsShouldNotResultInFaultResponse()
599+
{
600+
$server = new Zend_Rest_Server();
601+
$server->setClass('Zend_Rest_Server_Test');
602+
ob_start();
603+
$server->handle(array('method' => 'testFunc7', 'who' => "Davey"));
604+
$result = ob_get_clean();
605+
$this->assertContains('<status>success</status>', $result, var_export($result, 1));
606+
$this->assertContains('<response>Hello Davey, How are you today</response>', $result, var_export($result, 1));
607+
}
580608
}
581609

582610
/* Test Functions */

0 commit comments

Comments
 (0)