Skip to content

Commit

Permalink
Merge pull request #3 from sirn-se/extensions
Browse files Browse the repository at this point in the history
Minor improvements
  • Loading branch information
sirn-se committed Oct 11, 2022
2 parents 9991f2e + c76e897 commit f4770fa
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Phrity/Slim/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private function readSpec($source): OpenApiSpec
return $source; // Already parsed
}
if (is_string($source)) {
$source = realpath($source) ?: $source;
if (!is_readable($source)) {
throw new RuntimeException("Source file {$source} do not exist or is not readable");
}
Expand Down
17 changes: 15 additions & 2 deletions src/Phrity/Slim/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
};
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\App;
use Slim\Exception\{
HttpBadRequestException,
HttpInternalServerErrorException
};
use Throwable;

/**
* Slim OpenApi route instance
Expand Down Expand Up @@ -67,14 +72,22 @@ public function route(App $app): void
}
if ($this->openapi->getSetting('validate_request')) {
$slim_route->add(function (Request $request, RequestHandler $handler) {
$this->validateRequest($request);
try {
$this->validateRequest($request);
} catch (Throwable $t) {
throw new HttpBadRequestException($request, $t->getMessage(), $t);
}
return $handler->handle($request);
});
}
if ($this->openapi->getSetting('validate_response')) {
$slim_route->add(function (Request $request, RequestHandler $handler) {
$response = $handler->handle($request);
$this->validateResponse($response);
try {
$this->validateResponse($response);
} catch (Throwable $t) {
throw new HttpInternalServerErrorException($request, $t->getMessage(), $t);
}
return $response;
});
}
Expand Down
6 changes: 3 additions & 3 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function testMiddlewareRequestValidationFailure(): void
$request = self::createServerRequest('PUT', '/complete/test/1234');
$request = $request->withBody($stream);
// Validation order may change, check for any validation exception
$this->expectException('League\OpenAPIValidation\PSR7\Exception\ValidationFailed');
$this->expectException('Slim\Exception\HttpBadRequestException');
$this->expectExceptionMessage('"X-RequestId" for Request [put /complete/{param1}/{param2}]');
$response = $slim->handle($request);
}
Expand All @@ -169,7 +169,7 @@ public function testMiddlewareResponseValidationFailure(): void
$request = $request->withQueryParams(['limit' => 10, 'filtering' => 'yes']);
$request = $request->withBody($stream);
// Validation order may change, check for any validation exception
$this->expectException('League\OpenAPIValidation\PSR7\Exception\ValidationFailed');
$this->expectException('Slim\Exception\HttpInternalServerErrorException');
$this->expectExceptionMessage('Body does not match schema for content-type "application/json" ');
$response = $slim->handle($request);
}
Expand All @@ -189,7 +189,7 @@ public function testMiddlewareValidationFailures(): void
$stream = self::createStream(json_encode(['invalid' => 'body']));
$request = self::createServerRequest('PUT', '/complete/test/1234');
$request = $request->withBody($stream);
$this->expectException('League\OpenAPIValidation\PSR7\Exception\ValidationFailed');
$this->expectException('Slim\Exception\HttpBadRequestException');
$this->expectExceptionMessage('"X-RequestId" for Request [put /complete/{param1}/{param2}]');
$response = $slim->handle($request);
}
Expand Down
5 changes: 0 additions & 5 deletions tests/schemas/validations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ paths:
required: true
schema:
type: integer
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/SchemaA'
responses:
200:
description: good read
Expand Down

0 comments on commit f4770fa

Please sign in to comment.