From 73ef483dab09cb2d2348b7cdf38c0c89859f73de Mon Sep 17 00:00:00 2001 From: Aurelien Dupuis Date: Fri, 24 Sep 2021 11:45:07 +0200 Subject: [PATCH 1/3] test(typeMismatch): Add test on querystring parameters with array values of number --- tests/FromCommunity/Issue140Test.php | 68 ++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/FromCommunity/Issue140Test.php diff --git a/tests/FromCommunity/Issue140Test.php b/tests/FromCommunity/Issue140Test.php new file mode 100644 index 00000000..f9d4fa1a --- /dev/null +++ b/tests/FromCommunity/Issue140Test.php @@ -0,0 +1,68 @@ +fromJson($json)->getServerRequestValidator(); + + $queryString = 'id[]=1&id[]=2'; + $query = null; + parse_str($queryString, $query); + + $psrRequest = (new ServerRequest('get', 'http://localhost:8000/api/list')) + ->withHeader('Content-Type', 'application/json') + ->withQueryParams($query); + + $validator->validate($psrRequest); + + $this->addToAssertionCount(1); + } + +} From 7e2681066227238cecd6982769d87d63791f007a Mon Sep 17 00:00:00 2001 From: Aurelien Dupuis Date: Fri, 24 Sep 2021 12:07:22 +0200 Subject: [PATCH 2/3] fix(castToSchemaTypeOnArray): Fix castToSchemaType to be call when array don't need to be deserialized --- src/PSR7/Validators/SerializedParameter.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/PSR7/Validators/SerializedParameter.php b/src/PSR7/Validators/SerializedParameter.php index 9405ce8c..fe909ae4 100644 --- a/src/PSR7/Validators/SerializedParameter.php +++ b/src/PSR7/Validators/SerializedParameter.php @@ -166,11 +166,10 @@ private function castToSchemaType($value, ?string $type) */ protected function convertToSerializationStyle($value, ?CebeSchema $schema) { - if ( - $this->explode === false - && in_array($this->style, [self::STYLE_FORM, self::STYLE_SPACE_DELIMITED, self::STYLE_PIPE_DELIMITED], true) - ) { - $value = explode(self::STYLE_DELIMITER_MAP[$this->style], $value); + if (in_array($this->style, [self::STYLE_FORM, self::STYLE_SPACE_DELIMITED, self::STYLE_PIPE_DELIMITED], true)) { + if ($this->explode === false) { + $value = explode(self::STYLE_DELIMITER_MAP[$this->style], $value); + } foreach ($value as &$val) { $val = $this->castToSchemaType($val, $schema->items->type ?? null); } From 879b8020c95af17a631fd06009b2808e5e6ebee7 Mon Sep 17 00:00:00 2001 From: Aurelien Dupuis Date: Fri, 24 Sep 2021 13:12:24 +0200 Subject: [PATCH 3/3] Code style --- src/PSR7/Validators/SerializedParameter.php | 1 + tests/FromCommunity/Issue140Test.php | 16 ++++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/PSR7/Validators/SerializedParameter.php b/src/PSR7/Validators/SerializedParameter.php index fe909ae4..1fe17fb2 100644 --- a/src/PSR7/Validators/SerializedParameter.php +++ b/src/PSR7/Validators/SerializedParameter.php @@ -170,6 +170,7 @@ protected function convertToSerializationStyle($value, ?CebeSchema $schema) if ($this->explode === false) { $value = explode(self::STYLE_DELIMITER_MAP[$this->style], $value); } + foreach ($value as &$val) { $val = $this->castToSchemaType($val, $schema->items->type ?? null); } diff --git a/tests/FromCommunity/Issue140Test.php b/tests/FromCommunity/Issue140Test.php index f9d4fa1a..9f3e58cf 100644 --- a/tests/FromCommunity/Issue140Test.php +++ b/tests/FromCommunity/Issue140Test.php @@ -8,12 +8,13 @@ use League\OpenAPIValidation\PSR7\ValidatorBuilder; use League\OpenAPIValidation\Tests\PSR7\BaseValidatorTest; +use function parse_str; + /** * @see https://github.com/thephpleague/openapi-psr7-validator/issues/140 */ final class Issue140Test extends BaseValidatorTest { - public function testIssue140(): void { $json = /** @lang json */ @@ -49,20 +50,19 @@ public function testIssue140(): void } } JSON; - + $validator = (new ValidatorBuilder())->fromJson($json)->getServerRequestValidator(); - + $queryString = 'id[]=1&id[]=2'; - $query = null; + $query = null; parse_str($queryString, $query); - + $psrRequest = (new ServerRequest('get', 'http://localhost:8000/api/list')) ->withHeader('Content-Type', 'application/json') ->withQueryParams($query); - + $validator->validate($psrRequest); - + $this->addToAssertionCount(1); } - }