fix(controller): Fix false booleans in multipart/form-data

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2024-11-27 09:26:26 +01:00
parent 14f7e566c4
commit 1909b981a4
No known key found for this signature in database
GPG key ID: F72FA5B49FFA96B0
2 changed files with 40 additions and 13 deletions

View file

@ -183,16 +183,8 @@ class Dispatcher {
$value = $this->request->getParam($param, $default);
$type = $this->reflector->getType($param);
// if this is submitted using GET or a POST form, 'false' should be
// converted to false
if (($type === 'bool' || $type === 'boolean') &&
$value === 'false' &&
(
$this->request->method === 'GET' ||
str_contains($this->request->getHeader('Content-Type'),
'application/x-www-form-urlencoded')
)
) {
// Converted the string `'false'` to false when the controller wants a boolean
if ($value === 'false' && ($type === 'bool' || $type === 'boolean')) {
$value = false;
} elseif ($value !== null && \in_array($type, $types, true)) {
settype($value, $type);

View file

@ -328,7 +328,7 @@ class DispatcherTest extends \Test\TestCase {
$this->dispatcherPassthrough();
$response = $this->dispatcher->dispatch($controller, 'exec');
$this->assertEquals('[3,true,4,1]', $response[3]);
$this->assertEquals('[3,false,4,1]', $response[3]);
}
@ -361,7 +361,7 @@ class DispatcherTest extends \Test\TestCase {
$this->dispatcherPassthrough();
$response = $this->dispatcher->dispatch($controller, 'exec');
$this->assertEquals('[3,true,4,7]', $response[3]);
$this->assertEquals('[3,false,4,7]', $response[3]);
}
@ -471,6 +471,41 @@ class DispatcherTest extends \Test\TestCase {
$this->assertEquals('{"text":[3,false,4,1]}', $response[3]);
}
public function testResponseTransformedBySendingMultipartFormData(): void {
$this->request = new Request(
[
'post' => [
'int' => '3',
'bool' => 'false',
'double' => 1.2,
],
'server' => [
'HTTP_ACCEPT' => 'application/text, test',
'HTTP_CONTENT_TYPE' => 'multipart/form-data'
],
'method' => 'POST'
],
$this->createMock(IRequestId::class),
$this->createMock(IConfig::class)
);
$this->dispatcher = new Dispatcher(
$this->http, $this->middlewareDispatcher, $this->reflector,
$this->request,
$this->config,
\OC::$server->getDatabaseConnection(),
$this->logger,
$this->eventLogger,
$this->container
);
$controller = new TestController('app', $this->request);
// reflector is supposed to be called once
$this->dispatcherPassthrough();
$response = $this->dispatcher->dispatch($controller, 'exec');
$this->assertEquals('{"text":[3,false,4,1]}', $response[3]);
}
public function testResponsePrimarilyTransformedByParameterFormat(): void {
$this->request = new Request(
@ -506,7 +541,7 @@ class DispatcherTest extends \Test\TestCase {
$this->dispatcherPassthrough();
$response = $this->dispatcher->dispatch($controller, 'exec');
$this->assertEquals('{"text":[3,true,4,1]}', $response[3]);
$this->assertEquals('{"text":[3,false,4,1]}', $response[3]);
}