Skip to content

Commit

Permalink
Fix parse body schema (#95)
Browse files Browse the repository at this point in the history
* Fix parse body scheme in case of complex request

* Fix empty array should not be cast to object

* Fix style

* Use array_walk_recursive

* Fix style
  • Loading branch information
bastien-phi authored Feb 21, 2022
1 parent 92d0dec commit 06bdf2c
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/Validation/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use cebe\openapi\spec\PathItem;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Opis\JsonSchema\ValidationResult;
use Opis\JsonSchema\Validator;
use Spectator\Exceptions\RequestValidationException;
Expand Down Expand Up @@ -196,12 +197,13 @@ protected function operation(): Operation

protected function parseBodySchema(): object
{
$body = array_merge_recursive(
$this->request->request->all(),
array_map(function (UploadedFile $file) {
return $file->get();
}, $this->request->allFiles())
);
$body = $this->request->all();

array_walk_recursive($body, function (&$value) {
if ($value instanceof UploadedFile) {
$value = $value->get();
}
});

return $this->toObject($body);
}
Expand All @@ -210,10 +212,10 @@ private function toObject($data)
{
if (! is_array($data)) {
return $data;
} elseif (is_numeric(key($data))) {
return array_map([$this, 'toObject'], $data);
} else {
} elseif (Arr::isAssoc($data)) {
return (object) array_map([$this, 'toObject'], $data);
} else {
return array_map([$this, 'toObject'], $data);
}
}
}
75 changes: 75 additions & 0 deletions tests/Fixtures/BinaryString.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,81 @@
}
}
}
},
"/users/multiple-files": {
"post": {
"summary": "Send multiple files in structure",
"tags": [],
"responses": {
"201": {
"description": "Created"
},
"422": {
"description": "Unprocessable Entity"
}
},
"operationId": "post-users-multiple-files",
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"picture": {
"type": "string",
"format": "binary",
"example": "SGVsbG8gV29ybGQ="
},
"files": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"example": "file.txt"
},
"file": {
"type": "string",
"format": "binary",
"example": "SGVsbG8gV29ybGQ="
},
"required": [
"name",
"file"
]
}
}
},
"resume": {
"type": "object",
"properties": {
"name": {
"type": "string",
"example": "file.txt"
},
"file": {
"type": "string",
"format": "binary",
"example": "SGVsbG8gV29ybGQ="
},
"required": [
"name",
"file"
]
}
}
},
"required": [
"picture",
"files",
"resume"
]
}
}
}
}
}
}
},
"components": {
Expand Down
40 changes: 40 additions & 0 deletions tests/RequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,46 @@ public function test_handles_form_data()
)
->assertValidRequest();
}

public function test_handles_form_data_with_multiple_files()
{
Spectator::using('BinaryString.v1.json');

Route::post('/users/multiple-files', function () {
return [];
})->middleware(Middleware::class);

$this->post(
'/users/multiple-files',
[
'picture' => UploadedFile::fake()->image('test.jpg'),
'files' => [
['name' => 'test.jpg', 'file' => UploadedFile::fake()->image('test.jpg')],
['name' => 'test.jpg', 'file' => UploadedFile::fake()->image('test.jpg')],
],
'resume' => [
'name' => 'test.pdf',
'file' => UploadedFile::fake()->create('test.pdf'),
],
],
['Content-Type' => 'multipart/form-data']
)
->assertValidRequest();

$this->withoutExceptionHandling()->post(
'/users/multiple-files',
[
'picture' => UploadedFile::fake()->image('test.jpg'),
'files' => [],
'resume' => [
'name' => 'test.pdf',
'file' => UploadedFile::fake()->create('test.pdf'),
],
],
['Content-Type' => 'multipart/form-data']
)
->assertValidRequest();
}
}

class TestUser extends Model
Expand Down

0 comments on commit 06bdf2c

Please sign in to comment.