Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSR-15 Middleware: made response validation optional, enabled by default #237

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ Validation of `\Psr\Http\Message\ResponseInterface` is a bit more complicated
. Because you need not only YAML file and Response itself, but also you need
to know which operation this response belongs to (in terms of OpenAPI).

Reponse validation is optional and is enabled by default. See [Skipping response validation](#skipping-response-validation)

Example:

```php
Expand Down Expand Up @@ -194,6 +196,15 @@ You can use `->setCache($pool, $ttl)` call for both PSR-7 and PSR-15 builder in
If you want take control over the cache key for schema item, or your cache does not support cache key generation by itself
you can `->overrideCacheKey('my_custom_key')` to ensure cache uses key you want.

### Skipping response validation
If you want to skip response validation and only validate requests, you can call `->shouldValidateResponse(false)` method on the builder like this:
```php
$psr15Middleware = (new \OpenAPIValidation\PSR15\ValidationMiddlewareBuilder)
->fromYamlFile($yamlFile)
->shouldValidateResponse(false)
->getValidationMiddleware();
```

### Standalone OpenAPI Validator
The package contains a standalone validator which can validate any data
against an OpenAPI schema like this:
Expand Down
16 changes: 9 additions & 7 deletions src/PSR15/ValidationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ final class ValidationMiddleware implements MiddlewareInterface
{
/** @var ServerRequestValidator */
private $requestValidator;
/** @var ResponseValidator */
/** @var ResponseValidator|null */
private $responseValidator;

public function __construct(ServerRequestValidator $requestValidator, ResponseValidator $responseValidator)
public function __construct(ServerRequestValidator $requestValidator, ?ResponseValidator $responseValidator = null)
{
$this->requestValidator = $requestValidator;
$this->responseValidator = $responseValidator;
Expand All @@ -46,11 +46,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
// 2. Process request
$response = $handler->handle($request);

// 3. Validate response
try {
$this->responseValidator->validate($matchedOASOperation, $response);
} catch (ValidationFailed $e) {
throw InvalidResponseMessage::because($e);
if ($this->responseValidator !== null) {
// 3. Validate response
try {
$this->responseValidator->validate($matchedOASOperation, $response);
} catch (ValidationFailed $e) {
throw InvalidResponseMessage::because($e);
}
}

return $response;
Expand Down
15 changes: 14 additions & 1 deletion src/PSR15/ValidationMiddlewareBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,24 @@

class ValidationMiddlewareBuilder extends ValidatorBuilder
{
/** @var bool */
protected $shouldValidateResponse = true;

/**
* @return $this
*/
public function shouldValidateResponse(bool $value): self
{
$this->shouldValidateResponse = $value;

return $this;
}

public function getValidationMiddleware(): MiddlewareInterface
{
return new ValidationMiddleware(
$this->getServerRequestValidator(),
$this->getResponseValidator()
$this->shouldValidateResponse ? $this->getResponseValidator() : null
);
}
}
20 changes: 20 additions & 0 deletions tests/PSR15/ValidationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,24 @@ public function testItReturnsExpectedException(
$this->expectException($expectedExceptionType);
$middleware->process($serverRequest, $handler);
}

public function testItDoesNotValidateResponseIfNoValidatorConfigured(): void
{
$this->expectNotToPerformAssertions();

$builder = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile);

$middleware = new ValidationMiddleware(
$builder->getServerRequestValidator(),
null
);

$handler = $this->createMock(RequestHandlerInterface::class);
$handler
->method('handle')
->willReturn(new Response());

// no exception thrown
$middleware->process($this->makeGoodServerRequest('/read', 'get'), $handler);
}
}