-
Notifications
You must be signed in to change notification settings - Fork 96
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
Fix #147: multipart/form-data validation #162
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
108cd70
Add helping method to set values to array recursively
vovochka404 6ac3b54
Correctly support php-style array in request params
vovochka404 9582493
Correctly cast primitives in arrays in multipart/form-data requests
vovochka404 d059525
Add test case for issue 147
vovochka404 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace League\OpenAPIValidation\Tests\Foundation; | ||
|
||
use Generator; | ||
use League\OpenAPIValidation\Foundation\ArrayHelper; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use function count; | ||
use function parse_str; | ||
|
||
class ArrayHelperTest extends TestCase | ||
{ | ||
/** | ||
* @param array<int|string, mixed> $initArr | ||
* @param mixed $value | ||
* @param array<int|string, mixed> $expected | ||
* | ||
* @dataProvider getTestDataForSetRecursive | ||
*/ | ||
public function testSetRecursive(array $initArr, string $key, $value, array $expected): void | ||
{ | ||
$arr = $initArr; | ||
|
||
ArrayHelper::setRecursive($arr, $key, $value); | ||
self::assertEquals($expected, $arr); | ||
|
||
if (count($initArr) !== 0) { | ||
return; | ||
} | ||
|
||
$queryString = "{$key}=$value"; | ||
parse_str($queryString, $arr); | ||
|
||
self::assertEquals($expected, $arr); | ||
} | ||
|
||
public function getTestDataForSetRecursive(): Generator | ||
{ | ||
yield 'set value by key' => [ | ||
[], | ||
'item', | ||
1, | ||
['item' => 1], | ||
]; | ||
|
||
yield 'add value to an empty non-existent list' => [ | ||
[], | ||
'list[]', | ||
1, | ||
['list' => [1]], | ||
]; | ||
|
||
yield 'add value to an empty existent list' => [ | ||
['list' => []], | ||
'list[]', | ||
1, | ||
['list' => [1]], | ||
]; | ||
|
||
yield 'add value to a not empty list' => [ | ||
['list' => [0]], | ||
'list[]', | ||
1, | ||
['list' => [0, 1]], | ||
]; | ||
|
||
yield 'override existing value, if it is not array' => [ | ||
['list' => 0], | ||
'list[]', | ||
1, | ||
['list' => [1]], | ||
]; | ||
|
||
yield 'set values with string keys' => [ | ||
[], | ||
'list[some_key][another_key]', | ||
1, | ||
['list' => ['some_key' => ['another_key' => 1]]], | ||
]; | ||
|
||
yield 'set values to a list of objects' => [ | ||
['list' => [['another_key' => 0]]], | ||
'list[][another_key]', | ||
1, | ||
['list' => [['another_key' => 0], ['another_key' => 1]]], | ||
]; | ||
|
||
yield 'somehow sets value on incorrect key #1' => [ | ||
[], | ||
'list[asd]another_[key]', | ||
1, | ||
['list' => ['asd' => 1]], | ||
]; | ||
|
||
yield 'somehow sets value on incorrect key #2' => [ | ||
[], | ||
'list]asdanother_key', | ||
1, | ||
['list]asdanother_key' => 1], | ||
]; | ||
|
||
yield 'somehow sets value on incorrect key #3' => [ | ||
[], | ||
'list[asdanother_key', | ||
1, | ||
['list_asdanother_key' => 1], | ||
]; | ||
|
||
yield 'somehow sets value on incorrect key #4' => [ | ||
[], | ||
'list]asd[another_key', | ||
1, | ||
['list]asd_another_key' => 1], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace League\OpenAPIValidation\Tests\FromCommunity; | ||
|
||
use GuzzleHttp\Psr7\Message; | ||
use GuzzleHttp\Psr7\ServerRequest; | ||
use League\OpenAPIValidation\PSR7\ValidatorBuilder; | ||
use League\OpenAPIValidation\Tests\PSR7\BaseValidatorTest; | ||
|
||
/** | ||
* @see https://github.com/thephpleague/openapi-psr7-validator/issues/147 | ||
*/ | ||
final class Issue147Test extends BaseValidatorTest | ||
{ | ||
/** @var string */ | ||
private $openApiSpecification; | ||
|
||
public function testIssue147WithServerRequest(): void | ||
{ | ||
$validator = (new ValidatorBuilder())->fromJson($this->openApiSpecification)->getServerRequestValidator(); | ||
|
||
$data = [ | ||
'arrayOfNumbers' => ['1.2', '0', '2.2'], | ||
'arrayOfIntegers' => ['1', '3'], | ||
'arrayOfBooleans' => ['false', 'true'], | ||
]; | ||
|
||
$psrRequest = (new ServerRequest('post', 'http://localhost:8000/api/list')) | ||
->withHeader('Content-Type', 'multipart/form-data') | ||
->withParsedBody($data); | ||
|
||
$validator->validate($psrRequest); | ||
|
||
$this->addToAssertionCount(1); | ||
} | ||
|
||
public function testIssue147WithRawBody(): void | ||
{ | ||
$validator = (new ValidatorBuilder())->fromJson($this->openApiSpecification)->getServerRequestValidator(); | ||
|
||
$body = <<<HTTP | ||
POST /api/list HTTP/1.1 | ||
Content-Length: 554 | ||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryOmz20xyMCkE27rN7 | ||
|
||
------WebKitFormBoundaryOmz20xyMCkE27rN7 | ||
Content-Disposition: form-data; name="arrayOfNumbers[]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://swagger.io/docs/specification/serialization/ shouldn't this work only if deep object serialization specified? this is the only way I see |
||
Content-Type: text/plain | ||
|
||
123.0 | ||
------WebKitFormBoundaryOmz20xyMCkE27rN7 | ||
Content-Disposition: form-data; name="arrayOfNumbers[]" | ||
Content-Type: text/plain | ||
|
||
14 | ||
------WebKitFormBoundaryOmz20xyMCkE27rN7 | ||
Content-Disposition: form-data; name="arrayOfBooleans[]" | ||
Content-Type: text/plain | ||
|
||
true | ||
------WebKitFormBoundaryOmz20xyMCkE27rN7 | ||
Content-Disposition: form-data; name="arrayOfIntegers[]" | ||
Content-Type: text/plain | ||
|
||
456 | ||
------WebKitFormBoundaryOmz20xyMCkE27rN7-- | ||
HTTP; | ||
|
||
$message = Message::parseRequest($body); | ||
$psrRequest = new ServerRequest( | ||
$message->getMethod(), | ||
$message->getUri(), | ||
$message->getHeaders(), | ||
$message->getBody() | ||
); | ||
|
||
$validator->validate($psrRequest); | ||
|
||
$this->addToAssertionCount(1); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); // TODO: Change the autogenerated stub | ||
$this->openApiSpecification = /** @lang json */ | ||
<<<JSON | ||
{ | ||
"openapi": "3.0.0", | ||
"servers": [ | ||
{ | ||
"url": "https://localhost" | ||
} | ||
], | ||
"paths": { | ||
"/api/list": { | ||
"post": { | ||
"requestBody": { | ||
"content": { | ||
"multipart/form-data": { | ||
"schema": { | ||
"type": "object", | ||
"properties": { | ||
"arrayOfNumbers": { | ||
"type": "array", | ||
"items": { | ||
"type": "number" | ||
} | ||
}, | ||
"arrayOfIntegers": { | ||
"type": "array", | ||
"items": { | ||
"type": "integer" | ||
} | ||
}, | ||
"arrayOfBooleans": { | ||
"type": "array", | ||
"items": { | ||
"type": "boolean" | ||
} | ||
} | ||
}, | ||
"required": ["arrayOfNumbers", "arrayOfIntegers", "arrayOfBooleans"] | ||
} | ||
} | ||
} | ||
}, | ||
"responses": {} | ||
} | ||
} | ||
} | ||
} | ||
JSON; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are looks like bugs for me. I expect some kind of exception should be thrown here