Skip to content

Commit

Permalink
Merge pull request #16 from zfegg/develop
Browse files Browse the repository at this point in the history
Fix get schema from route options.
  • Loading branch information
Moln committed Aug 26, 2021
2 parents 8e2f0b3 + 378fb28 commit 3a84edd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/ContentValidationMiddleware.php
Expand Up @@ -43,22 +43,21 @@ public function __construct(
private function getSchema(ServerRequestInterface $request)
{
$withMethod = $this->routeNameWithMethod ? ":{$request->getMethod()}" : '';
if (($schema = $request->getAttribute(self::SCHEMA)) ||
($schema = $request->getAttribute(self::SCHEMA . $withMethod))
) {
$schema = $request->getAttribute(self::SCHEMA . $withMethod, $request->getAttribute(self::SCHEMA));
if ($schema) {
return $schema;
}

// Set Mezzio route name or slim route name
if ($route = $request->getAttribute('Mezzio\Router\RouteResult')) {
/** @var \Mezzio\Router\RouteResult $route */
$options = $route->getMatchedRoute()->getOptions();
return $options[self::SCHEMA . $withMethod] ?? null;
return $options[self::SCHEMA . $withMethod] ?? $options[self::SCHEMA] ?? null;
} elseif ($route = $request->getAttribute('route')) {
/** @var \Slim\Routing\Route $route */
return $route->getArgument(
self::SCHEMA . $withMethod,
$route->getArgument(self::SCHEMA . $withMethod)
$route->getArgument(self::SCHEMA)
);
}

Expand Down
18 changes: 18 additions & 0 deletions test/ContentValidationMiddlewareTest.php
Expand Up @@ -138,6 +138,24 @@ public function handle(ServerRequestInterface $request): ResponseInterface
}

public function testMezzio(): void
{
$schema = (object) [
'type' => 'object',
'properties' => (object) [
'age' => (object) [
'type' => 'integer'
]
],
'required' => ['age']
];
$route = $this->createMock(Route::class);
$route->method('getOptions')->willReturn(['schema' => $schema]);
$this->routeTest([
RouteResult::class => RouteResult::fromRoute($route)
]);
}

public function testMezzioOptionsWithMethod(): void
{
$schema = (object) [
'type' => 'object',
Expand Down

0 comments on commit 3a84edd

Please sign in to comment.