Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find suitable value for oneOf property in parameters. Fix #166 #168

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/PSR7/Validators/SerializedParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
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;

use function count;
use function explode;
use function in_array;
use function is_array;
Expand Down Expand Up @@ -181,6 +183,14 @@ 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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like in this case we should immediately stop validation if we have filled oneOf and none of the schemas matched in findSuitableOneOf.

$suitableSchema = $this->findSuitableOneOf($childSchema, $val);
if ($suitableSchema) {
$childSchema = $suitableSchema;
}
}

if (is_array($val)) {
$val = $this->convertToSerializationStyle($val, $childSchema);
} else {
Expand Down Expand Up @@ -217,4 +227,29 @@ protected function getChildSchema(CebeSchema $schema, string $key): ?CebeSchema

return null;
}

/**
* @param mixed $val
*/
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;
}
}
72 changes: 72 additions & 0 deletions tests/FromCommunity/Issue166Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace League\OpenAPIValidation\Tests\FromCommunity;

use GuzzleHttp\Psr7\ServerRequest;
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidQueryArgs;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use League\OpenAPIValidation\Tests\PSR7\BaseValidatorTest;

use function parse_str;

/**
* @see https://github.com/thephpleague/openapi-psr7-validator/issues/166
*/
final class Issue166Test extends BaseValidatorTest
{
public function testIssue166(): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some negative cases required

{
$yaml = /** @lang yaml */
<<<YAML
openapi: 3.0.0
servers:
- url: 'http://localhost:8000/api'
paths:
/list:
get:
parameters:
- in: query
name: filter
style: deepObject
allowReserved: true
schema:
type: object
properties:
filters:
oneOf:
-
type: object
required:
- max
properties:
max:
type: number
-
type: object
required:
- min
properties:
min:
type: number
YAML;

$validator = (new ValidatorBuilder())->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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not proper way how to deal with exceptions and zero assertions. proper way would be (without any try..catch)

$validator->validate($psrRequest);
$this->addToAssrtionsCount(1);

any exception thrown here should break the test and if validate throws one assertTrue(true) is not called anyway

} catch (InvalidQueryArgs $e) {
self::fail('Should not throw an exception');
}
}
}