From 03301b4d12ca41356a3af658873450ad8d0eafef Mon Sep 17 00:00:00 2001 From: Dmitry Demidov Date: Thu, 3 Feb 2022 15:52:42 +0300 Subject: [PATCH 1/4] * fix oneof in parameter --- src/PSR7/Validators/SerializedParameter.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/PSR7/Validators/SerializedParameter.php b/src/PSR7/Validators/SerializedParameter.php index 1fe17fb2..40caa499 100644 --- a/src/PSR7/Validators/SerializedParameter.php +++ b/src/PSR7/Validators/SerializedParameter.php @@ -11,6 +11,7 @@ use League\OpenAPIValidation\Schema\Exception\InvalidSchema; use League\OpenAPIValidation\Schema\Exception\SchemaMismatch; use League\OpenAPIValidation\Schema\Exception\TypeMismatch; +use League\OpenAPIValidation\Schema\SchemaValidator; use Respect\Validation\Exceptions\Exception; use Respect\Validation\Exceptions\ExceptionInterface; use Respect\Validation\Validator; @@ -181,6 +182,26 @@ protected function convertToSerializationStyle($value, ?CebeSchema $schema) if ($schema && $this->style === self::STYLE_DEEP_OBJECT) { foreach ($value as $key => &$val) { $childSchema = $this->getChildSchema($schema, (string) $key); + + if (isset($childSchema->oneOf) && count($childSchema->oneOf)) { + $schemaValidator = new SchemaValidator(SchemaValidator::VALIDATE_AS_REQUEST); + $validSchemas = []; + foreach ($childSchema->oneOf as $schemaCandidate) { + try { + $val1 = $this->convertToSerializationStyle($val, $schemaCandidate); + + $schemaValidator->validate($val1, $schemaCandidate); + $validSchemas[] = $schemaCandidate; + } catch (SchemaMismatch $e) { + // nothintg to do + } + } + + if (count($validSchemas) == 1) { + $childSchema = $validSchemas[0]; + } + } + if (is_array($val)) { $val = $this->convertToSerializationStyle($val, $childSchema); } else { From 47b73c7514e69872a83a94f214748cb411e03742 Mon Sep 17 00:00:00 2001 From: Dmitry Demidov Date: Fri, 13 May 2022 16:42:02 +0300 Subject: [PATCH 2/4] * refactor a little --- src/PSR7/Validators/SerializedParameter.php | 47 ++++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/PSR7/Validators/SerializedParameter.php b/src/PSR7/Validators/SerializedParameter.php index 40caa499..512ac66b 100644 --- a/src/PSR7/Validators/SerializedParameter.php +++ b/src/PSR7/Validators/SerializedParameter.php @@ -183,22 +183,10 @@ protected function convertToSerializationStyle($value, ?CebeSchema $schema) foreach ($value as $key => &$val) { $childSchema = $this->getChildSchema($schema, (string) $key); - if (isset($childSchema->oneOf) && count($childSchema->oneOf)) { - $schemaValidator = new SchemaValidator(SchemaValidator::VALIDATE_AS_REQUEST); - $validSchemas = []; - foreach ($childSchema->oneOf as $schemaCandidate) { - try { - $val1 = $this->convertToSerializationStyle($val, $schemaCandidate); - - $schemaValidator->validate($val1, $schemaCandidate); - $validSchemas[] = $schemaCandidate; - } catch (SchemaMismatch $e) { - // nothintg to do - } - } - - if (count($validSchemas) == 1) { - $childSchema = $validSchemas[0]; + if (isset($childSchema->oneOf)) { + $suitableSchema = $this->findSuitableOneOf($childSchema, $val); + if ($suitableSchema) { + $childSchema = $suitableSchema; } } @@ -238,4 +226,31 @@ protected function getChildSchema(CebeSchema $schema, string $key): ?CebeSchema return null; } + + /** + * @param CebeSchema $schema + * @param mixed $val + * @return CebeSchema|null + */ + private function findSuitableOneOf(CebeSchema $schema, $val): ?CebeSchema + { + if (!count($schema->oneOf)) { + return null; + } + + $schemaValidator = new SchemaValidator(SchemaValidator::VALIDATE_AS_REQUEST); + $validSchemas = []; + foreach ($schema->oneOf as $schemaCandidate) { + try { + $valCandidate = $this->convertToSerializationStyle($val, $schemaCandidate); + + $schemaValidator->validate($valCandidate, $schemaCandidate); + $validSchemas[] = $schemaCandidate; + } catch (SchemaMismatch $e) { + // nothing to do + } + } + + return (count($validSchemas) === 1) ? $validSchemas[0] : null; + } } From 20f89ea048c89e315a57f915ccb8bfe3aae265f5 Mon Sep 17 00:00:00 2001 From: Dmitry Demidov Date: Fri, 13 May 2022 16:52:38 +0300 Subject: [PATCH 3/4] + issue 166 test --- tests/FromCommunity/Issue166Test.php | 73 ++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tests/FromCommunity/Issue166Test.php diff --git a/tests/FromCommunity/Issue166Test.php b/tests/FromCommunity/Issue166Test.php new file mode 100644 index 00000000..98499512 --- /dev/null +++ b/tests/FromCommunity/Issue166Test.php @@ -0,0 +1,73 @@ +fromYaml($yaml)->getServerRequestValidator(); + + $queryString = 'filter[filters][min]=1'; + $query = null; + parse_str($queryString, $query); + + $psrRequest = (new ServerRequest('get', 'http://localhost:8000/api/list')) + ->withHeader('Content-Type', 'application/json') + ->withQueryParams($query); + + try { + $validator->validate($psrRequest); + $this->assertTrue(true); + } catch (InvalidQueryArgs $e) { + + self::fail('Should not throw an exception'); + } + } +} From f644ffba41b08886e6a84c198818b800a0300938 Mon Sep 17 00:00:00 2001 From: Dmitry Demidov Date: Sat, 14 May 2022 08:04:33 +0300 Subject: [PATCH 4/4] * fix cs --- src/PSR7/Validators/SerializedParameter.php | 9 ++++----- tests/FromCommunity/Issue166Test.php | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/PSR7/Validators/SerializedParameter.php b/src/PSR7/Validators/SerializedParameter.php index 512ac66b..812efd29 100644 --- a/src/PSR7/Validators/SerializedParameter.php +++ b/src/PSR7/Validators/SerializedParameter.php @@ -16,6 +16,7 @@ use Respect\Validation\Exceptions\ExceptionInterface; use Respect\Validation\Validator; +use function count; use function explode; use function in_array; use function is_array; @@ -228,18 +229,16 @@ protected function getChildSchema(CebeSchema $schema, string $key): ?CebeSchema } /** - * @param CebeSchema $schema * @param mixed $val - * @return CebeSchema|null */ private function findSuitableOneOf(CebeSchema $schema, $val): ?CebeSchema { - if (!count($schema->oneOf)) { + if (! count($schema->oneOf)) { return null; } $schemaValidator = new SchemaValidator(SchemaValidator::VALIDATE_AS_REQUEST); - $validSchemas = []; + $validSchemas = []; foreach ($schema->oneOf as $schemaCandidate) { try { $valCandidate = $this->convertToSerializationStyle($val, $schemaCandidate); @@ -251,6 +250,6 @@ private function findSuitableOneOf(CebeSchema $schema, $val): ?CebeSchema } } - return (count($validSchemas) === 1) ? $validSchemas[0] : null; + return count($validSchemas) === 1 ? $validSchemas[0] : null; } } diff --git a/tests/FromCommunity/Issue166Test.php b/tests/FromCommunity/Issue166Test.php index 98499512..6733e2fc 100644 --- a/tests/FromCommunity/Issue166Test.php +++ b/tests/FromCommunity/Issue166Test.php @@ -66,7 +66,6 @@ public function testIssue166(): void $validator->validate($psrRequest); $this->assertTrue(true); } catch (InvalidQueryArgs $e) { - self::fail('Should not throw an exception'); } }