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

Allow specifying encoding for optional parts of multipart data #192

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 1 addition & 9 deletions src/PSR7/Validators/BodyValidator/MultipartValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Psr\Http\Message\UploadedFileInterface;
use Riverline\MultiPartParser\Converters\PSR7;
use Riverline\MultiPartParser\StreamedPart;
use RuntimeException;

use function array_replace;
use function in_array;
Expand All @@ -35,7 +34,6 @@
use function json_last_error;
use function json_last_error_msg;
use function preg_match;
use function sprintf;
use function str_replace;
use function strpos;

Expand Down Expand Up @@ -108,12 +106,6 @@ private function validatePlainBodyMultipart(

foreach ($encodings as $partName => $encoding) {
$parts = $document->getPartsByName($partName); // multiple parts share a name?
if (! $parts) {
throw new RuntimeException(sprintf(
'Specified body part %s is not found',
$partName
));
}

foreach ($parts as $part) {
// 2.1 parts encoding
Expand Down Expand Up @@ -253,7 +245,7 @@ private function validateServerRequestMultipart(

foreach ($encodings as $partName => $encoding) {
if (! isset($body[$partName])) {
throw new RuntimeException(sprintf('Specified body part %s is not found', $partName));
continue;
}

$part = $body[$partName];
Expand Down
77 changes: 63 additions & 14 deletions tests/PSR7/Validators/BodyValidator/MultipartValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidHeaders;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UploadedFileInterface;

use function filesize;
use function GuzzleHttp\Psr7\parse_request;
Expand Down Expand Up @@ -211,6 +212,23 @@ public function dataProviderMultipartRed(): array
[file content goes there]
------WebKitFormBoundaryWfPNVh4wuWBlyEyQ--
HTTP
,
InvalidBody::class,
],
// missing required part
[
<<<HTTP
POST /multipart/encoding HTTP/1.1
Content-Length: 428
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryOmz20xyMCkE27rN7

------WebKitFormBoundaryOmz20xyMCkE27rN7
Content-Disposition: form-data; name="description"
Content-Type: text/plain

123
------WebKitFormBoundaryOmz20xyMCkE27rN7--
HTTP
,
InvalidBody::class,
],
Expand Down Expand Up @@ -322,25 +340,56 @@ public function testValidateMultipartRed(string $message, string $expectedExcept
$validator->validate($serverRequest);
}

public function testValidateMultipartServerRequestGreen(): void
/**
* @return mixed[][]
*/
public function dataProviderMultipartServerRequestGreen(): array
{
$specFile = __DIR__ . '/../../../stubs/multipart.yaml';

$imagePath = __DIR__ . '/../../../stubs/image.jpg';
$imageSize = filesize($imagePath);

$serverRequest = (new ServerRequest('post', new Uri('/multipart')))
->withHeader('Content-Type', 'multipart/form-data')
->withParsedBody([
'id' => 'bc8e1430-a963-11e9-a2a3-2a2ae2dbcce4',
'address' => [
'street' => 'Some street',
'city' => 'some city',
return [
// Normal multipart message
[
'post',
'/multipart',
[
'id' => 'bc8e1430-a963-11e9-a2a3-2a2ae2dbcce4',
'address' => [
'street' => 'Some street',
'city' => 'some city',
],
],
[
'profileImage' => new UploadedFile($imagePath, $imageSize, 0),
],
])
->withUploadedFiles([
'profileImage' => new UploadedFile($imagePath, $imageSize, 0),
]);
],
// Missing optional field with defined encoding
[
'post',
'/multipart/encoding',
[],
[
'image' => new UploadedFile($imagePath, $imageSize, 0),
],
],
];
}

/**
* @param string[] $body
* @param array<string, UploadedFileInterface> $files
*
* @dataProvider dataProviderMultipartServerRequestGreen
*/
public function testValidateMultipartServerRequestGreen(string $method, string $uri, array $body = [], array $files = []): void
{
$specFile = __DIR__ . '/../../../stubs/multipart.yaml';

$serverRequest = (new ServerRequest($method, new Uri($uri)))
->withHeader('Content-Type', 'multipart/form-data')
->withParsedBody($body)
->withUploadedFiles($files);

$validator = (new ValidatorBuilder())->fromYamlFile($specFile)->getServerRequestValidator();
$validator->validate($serverRequest);
Expand Down
6 changes: 6 additions & 0 deletions tests/stubs/multipart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,19 @@ paths:
multipart/form-data:
schema:
type: object
required:
- image
properties:
image:
type: string
format: binary
description:
type: string
encoding:
image:
contentType: specific/type
description:
contentType: text/plain
responses:
204:
description: good post
Expand Down