diff --git a/src/PSR7/Validators/BodyValidator/MultipartValidator.php b/src/PSR7/Validators/BodyValidator/MultipartValidator.php index f880a424..8342ccae 100644 --- a/src/PSR7/Validators/BodyValidator/MultipartValidator.php +++ b/src/PSR7/Validators/BodyValidator/MultipartValidator.php @@ -26,7 +26,6 @@ use Psr\Http\Message\UploadedFileInterface; use Riverline\MultiPartParser\Converters\PSR7; use Riverline\MultiPartParser\StreamedPart; -use RuntimeException; use function array_diff_assoc; use function array_map; @@ -39,7 +38,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; use function strtolower; @@ -114,12 +112,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 @@ -317,7 +309,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]; diff --git a/tests/PSR7/Validators/BodyValidator/MultipartValidatorTest.php b/tests/PSR7/Validators/BodyValidator/MultipartValidatorTest.php index 6aee7da8..e93300c0 100644 --- a/tests/PSR7/Validators/BodyValidator/MultipartValidatorTest.php +++ b/tests/PSR7/Validators/BodyValidator/MultipartValidatorTest.php @@ -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; @@ -259,6 +260,23 @@ public function dataProviderMultipartRed(): array [file content goes there] ------WebKitFormBoundaryWfPNVh4wuWBlyEyQ-- HTTP +, + InvalidBody::class, + ], + // missing required part + [ + <<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 $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); diff --git a/tests/stubs/multipart.yaml b/tests/stubs/multipart.yaml index 10c3197b..b018b4c6 100644 --- a/tests/stubs/multipart.yaml +++ b/tests/stubs/multipart.yaml @@ -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