Skip to content

Commit

Permalink
Merge pull request #14 from zfegg/develop
Browse files Browse the repository at this point in the history
Fix ContentValidationMiddleware deep convert to array.
  • Loading branch information
Moln committed Aug 11, 2021
2 parents e3e14ef + aec448a commit 6608ce9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 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
16 changes: 11 additions & 5 deletions src/Opis/TransformersParser.php
Expand Up @@ -14,17 +14,23 @@
class TransformersParser extends KeywordParser
{
use VariablesTrait;
private Resolver\ResolverInterface $resolver;

public function __construct(Resolver\ResolverInterface $resolver)
{
parent::__construct('$transformers');
private Resolver\ResolverInterface $resolver;
private string $type;

public function __construct(
Resolver\ResolverInterface $resolver,
string $keyword = '$transformers',
string $type = self::TYPE_BEFORE
) {
parent::__construct($keyword);
$this->resolver = $resolver;
$this->type = $type;
}

public function type(): string
{
return self::TYPE_BEFORE;
return $this->type;
}

public function parse(SchemaInfo $info, SchemaParser $parser, object $shared): ?Keyword
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 6608ce9

Please sign in to comment.