Skip to content

Commit

Permalink
Fix ContentValidationMiddleware deep convert to array.
Browse files Browse the repository at this point in the history
  • Loading branch information
Moln committed Aug 11, 2021
1 parent ed59e7c commit aec448a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/ContentValidationMiddleware.php
Expand Up @@ -124,11 +124,19 @@ function (ValidationError $error) {
], 422);
}

private static function object2Array(object $data): array
/**
* @param object|array $data
* @return mixed
*/
private static function object2Array($data)
{
$data = (array) $data;
foreach ($data as $key => $value) {
if (is_object($value)) {
if (is_object($data)) {
$data = (array) $data;
foreach ($data as $key => $value) {
$data[$key] = self::object2Array($value);
}
} elseif (is_array($data)) {
foreach ($data as $key => $value) {
$data[$key] = self::object2Array($value);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Opis/TransformersParser.php
Expand Up @@ -14,6 +14,7 @@
class TransformersParser extends KeywordParser
{
use VariablesTrait;

private Resolver\ResolverInterface $resolver;
private string $type;

Expand Down
2 changes: 1 addition & 1 deletion test/ContentValidationMiddlewareTest.php
Expand Up @@ -72,7 +72,7 @@ public function invokeProvider(): array
[],
[],
[],
['name' => 'foo', 'age' => '18', 'sub' => ['foo' => '123']]
['name' => 'foo', 'age' => '18', 'sub' => ['foo' => '123'], 'list-obj' => [[['id' => 1]]]]
),
],
'HttpPostInvalid' => [
Expand Down
14 changes: 14 additions & 0 deletions test/test.json
Expand Up @@ -58,6 +58,20 @@
"type": "integer"
}
}
},
"list-obj": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
}
}
}
}
}
},
"required": ["name", "age"],
Expand Down

0 comments on commit aec448a

Please sign in to comment.