Skip to content
Merged
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
26 changes: 22 additions & 4 deletions src/Laravel/ValidatesOpenApiSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
trait ValidatesOpenApiSchema
{
use OpenApiSpecResolver;
private static ?OpenApiResponseValidator $cachedValidator = null;
private static ?int $cachedMaxErrors = null;

public static function resetValidatorCache(): void
{
self::$cachedValidator = null;
self::$cachedMaxErrors = null;
}

protected function openApiSpec(): string
{
Expand Down Expand Up @@ -61,10 +69,7 @@ protected function assertResponseMatchesOpenApiSchema(

$contentType = $response->headers->get('Content-Type', '');

$maxErrors = config('openapi-contract-testing.max_errors', 20);
$validator = new OpenApiResponseValidator(
maxErrors: is_numeric($maxErrors) ? (int) $maxErrors : 20,
);
$validator = self::getOrCreateValidator();
$result = $validator->validate(
$specName,
$resolvedMethod,
Expand Down Expand Up @@ -92,6 +97,19 @@ protected function assertResponseMatchesOpenApiSchema(
);
}

private static function getOrCreateValidator(): OpenApiResponseValidator
{
$maxErrors = config('openapi-contract-testing.max_errors', 20);
$resolvedMaxErrors = is_numeric($maxErrors) ? (int) $maxErrors : 20;

if (self::$cachedValidator === null || self::$cachedMaxErrors !== $resolvedMaxErrors) {
self::$cachedValidator = new OpenApiResponseValidator(maxErrors: $resolvedMaxErrors);
self::$cachedMaxErrors = $resolvedMaxErrors;
}

return self::$cachedValidator;
}

/** @return null|array<string, mixed> */
private function extractJsonBody(TestResponse $response, string $content, string $contentType): ?array
{
Expand Down
32 changes: 23 additions & 9 deletions src/OpenApiResponseValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

final class OpenApiResponseValidator
{
/** @var array<string, OpenApiPathMatcher> */
private array $pathMatchers = [];
private Validator $opisValidator;
private ErrorFormatter $errorFormatter;

public function __construct(
private readonly int $maxErrors = 20,
) {
Expand All @@ -31,6 +36,13 @@ public function __construct(
sprintf('maxErrors must be 0 (unlimited) or a positive integer, got %d.', $this->maxErrors),
);
}

$resolvedMaxErrors = $this->maxErrors === 0 ? PHP_INT_MAX : $this->maxErrors;
$this->opisValidator = new Validator(
max_errors: $resolvedMaxErrors,
stop_at_first_error: $resolvedMaxErrors === 1,
);
$this->errorFormatter = new ErrorFormatter();
}

public function validate(
Expand All @@ -47,7 +59,7 @@ public function validate(

/** @var string[] $specPaths */
$specPaths = array_keys($spec['paths'] ?? []);
$matcher = new OpenApiPathMatcher($specPaths, OpenApiSpecLoader::getStripPrefixes());
$matcher = $this->getPathMatcher($specName, $specPaths);
$matchedPath = $matcher->match($requestPath);

if ($matchedPath === null) {
Expand Down Expand Up @@ -135,19 +147,13 @@ public function validate(
$schemaObject = self::toObject($jsonSchema);
$dataObject = self::toObject($responseBody);

$resolvedMaxErrors = $this->maxErrors === 0 ? PHP_INT_MAX : $this->maxErrors;
$validator = new Validator(
max_errors: $resolvedMaxErrors,
stop_at_first_error: $resolvedMaxErrors === 1,
);
$result = $validator->validate($dataObject, $schemaObject);
$result = $this->opisValidator->validate($dataObject, $schemaObject);

if ($result->isValid()) {
return OpenApiValidationResult::success($matchedPath);
}

$formatter = new ErrorFormatter();
$formattedErrors = $formatter->format($result->error());
$formattedErrors = $this->errorFormatter->format($result->error());

$errors = [];
foreach ($formattedErrors as $path => $messages) {
Expand Down Expand Up @@ -187,6 +193,14 @@ private static function toObject(mixed $value): mixed
return $object;
}

/**
* @param string[] $specPaths
*/
private function getPathMatcher(string $specName, array $specPaths): OpenApiPathMatcher
{
return $this->pathMatchers[$specName] ??= new OpenApiPathMatcher($specPaths, OpenApiSpecLoader::getStripPrefixes());
}

/**
* Find the first JSON-compatible content type from the response spec.
*
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/ValidatesOpenApiSchemaAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected function setUp(): void

protected function tearDown(): void
{
self::resetValidatorCache();
OpenApiSpecLoader::reset();
OpenApiCoverageTracker::reset();
parent::tearDown();
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/ValidatesOpenApiSchemaDefaultSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected function setUp(): void

protected function tearDown(): void
{
self::resetValidatorCache();
unset($GLOBALS['__openapi_testing_config']);
OpenApiSpecLoader::reset();
OpenApiCoverageTracker::reset();
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/ValidatesOpenApiSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function setUp(): void

protected function tearDown(): void
{
self::resetValidatorCache();
OpenApiSpecLoader::reset();
OpenApiCoverageTracker::reset();
parent::tearDown();
Expand Down