From 27249f268d864774d1cc4aaf9ce6fe17da1ea41c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregori=20Pi=C3=B1eres?= Date: Sat, 17 Apr 2021 20:21:02 -0400 Subject: [PATCH 1/3] Fix src/RequestParser.php --- .gitattributes | 1 + src/RequestParser.php | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitattributes b/.gitattributes index b95979f..82e594a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -12,3 +12,4 @@ /docs export-ignore /CHANGELOG.md export-ignore /CONTRIBUTING.md export-ignore +/.php_cs export-ignore diff --git a/src/RequestParser.php b/src/RequestParser.php index 421c798..96207ea 100644 --- a/src/RequestParser.php +++ b/src/RequestParser.php @@ -77,9 +77,9 @@ protected function hasParam(): bool /** * Get the request param value. * - * @return string + * @return mixed */ - protected function getQueryParamValue(): string + protected function getQueryParamValue() { $paramName = $this->getQueryParamName(); From 57920513bb7151b670252cde2d40f4b41aef54fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregori=20Pi=C3=B1eres?= Date: Sat, 17 Apr 2021 20:38:53 -0400 Subject: [PATCH 2/3] Drop support for base64 encodes --- src/RequestParser.php | 41 ++++++++++---------------------- tests/Unit/RequestParserTest.php | 28 ++++------------------ 2 files changed, 17 insertions(+), 52 deletions(-) diff --git a/src/RequestParser.php b/src/RequestParser.php index 96207ea..beea6c5 100644 --- a/src/RequestParser.php +++ b/src/RequestParser.php @@ -2,6 +2,7 @@ namespace Restql; +use Exception; use Illuminate\Http\Request; use Illuminate\Support\Collection; use Restql\Traits\HasConfigService; @@ -45,21 +46,9 @@ public static function filter(Request $request): Collection */ protected function decode(): Collection { - if (strlen($this->getQueryParamName()) && $this->hasParam()) { - /// If the user decides to give a value to the attribute in the - /// "query_param" setting, then it will be assumed that - /// the application accepts base64 encoded queries - /// sent as parameters in the HTTP request. - - /// Similarly, when this happens, the value of the - /// query must be compulsorily encoded in - /// base64. - return $this->decodeParam(); - } + $resources = $this->hasParam() ? $this->getQueryParamValue() : $this->request->all(); - /// By default, restql accepts queries that come in the body - /// of the HTTP request and are not base64 encoded. - return $this->getAllowedResources($this->request->all()); + return $this->getAllowedResources($resources); } /** @@ -77,13 +66,19 @@ protected function hasParam(): bool /** * Get the request param value. * - * @return mixed + * @return array */ - protected function getQueryParamValue() + protected function getQueryParamValue(): array { $paramName = $this->getQueryParamName(); - return $this->request->get($paramName); + $paramValue = $this->request->input($paramName); + + if (!is_array($paramValue)) { + throw new Exception(sprintf('The value of param %s must be array', $paramName)); + } + + return $paramValue; } /** @@ -96,18 +91,6 @@ protected function getQueryParamName(): string return $this->getConfigService()->get('query_param', ''); } - /** - * Decode the parameter. - * - * @return \Illuminate\Support\Collection - */ - protected function decodeParam(): Collection - { - $query = (array) json_decode(base64_decode($this->getQueryParamValue())); - - return $this->getAllowedResources($query); - } - /** * The accepted resources key names (models/resolvers). * diff --git a/tests/Unit/RequestParserTest.php b/tests/Unit/RequestParserTest.php index 8b445e2..6e6debe 100644 --- a/tests/Unit/RequestParserTest.php +++ b/tests/Unit/RequestParserTest.php @@ -3,10 +3,10 @@ namespace Testing\Unit; use Illuminate\Http\Request; -use PHPUnit\Framework\TestCase as FrameworkTestCase; use Restql\RequestParser; +use Testing\TestCase; -class RequestParserTest extends FrameworkTestCase +class RequestParserTest extends TestCase { /** * Fake request body. @@ -19,20 +19,6 @@ class RequestParserTest extends FrameworkTestCase 'unknow_schema_definition' => true ]; - /** - * Create generic encoded request. - * - * @return \Illuminate\Http\Request - */ - protected static function genericEncodedRequest(): Request - { - $body = base64_encode(json_encode(self::$body)); - - return new Request([ - 'query' => $body - ]); - } - /** * Create generic request. * @@ -40,7 +26,9 @@ protected static function genericEncodedRequest(): Request */ protected static function genericRequest(): Request { - return new Request(self::$body); + return new Request([ + 'query' => self::$body + ]); } /** @@ -48,14 +36,8 @@ protected static function genericRequest(): Request */ public function theParserExcludeUnknowResources(): void { - $encoded = RequestParser::filter(self::genericEncodedRequest()); - $decoded = RequestParser::filter(self::genericRequest()); - - $this->assertFalse($encoded->has('unknow_schema_definition')); $this->assertFalse($decoded->has('unknow_schema_definition')); - - $this->assertTrue($encoded->has('authors')); $this->assertTrue($decoded->has('authors')); } } From e6239e6af2cc811aecd4d5fb355834929bc52915 Mon Sep 17 00:00:00 2001 From: gregorip02 Date: Sun, 18 Apr 2021 00:42:03 +0000 Subject: [PATCH 3/3] Apply code style fixes --- src/RequestParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RequestParser.php b/src/RequestParser.php index beea6c5..e551504 100644 --- a/src/RequestParser.php +++ b/src/RequestParser.php @@ -74,7 +74,7 @@ protected function getQueryParamValue(): array $paramValue = $this->request->input($paramName); - if (!is_array($paramValue)) { + if (! is_array($paramValue)) { throw new Exception(sprintf('The value of param %s must be array', $paramName)); }