Skip to content
This repository was archived by the owner on Sep 18, 2023. It is now read-only.
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
31 changes: 9 additions & 22 deletions src/OpenApiV3Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@

class OpenApiV3Validator implements OpenApiValidatorInterface
{
/**
* @var OpenApiV3ToJsonSchemaConverter
*/
private $converter;

/**
* @var JsonSchemaValidator
Expand Down Expand Up @@ -55,31 +51,20 @@ class OpenApiV3Validator implements OpenApiValidatorInterface
private $openApiSchemaFileName;

/**
* @param string $openApiSchemaFileName
* @param string $openApiSchemaFileName
* @param OpenApiV3ToJsonSchemaConverter $converter
* @param SchemaStorage $schemaStorage
* @param SchemaStorage $schemaStorage
*/
public function __construct(
string $openApiSchemaFileName,
OpenApiV3ToJsonSchemaConverter $converter,
SchemaStorage $schemaStorage
) {
$this->converter = $converter;
)
{
$this->openApiSchemaFileName = $openApiSchemaFileName;
$this->schemaStorage = $schemaStorage;
$this->setupSchema();
$this->jsonSchemaValidator = new JsonSchemaValidator(new Factory($this->schemaStorage));
}

/**
* Converts OpenApi v3 schema to json schema draft 4, adds it to storage.
*/
private function setupSchema()
{
$openApiSchema = json_decode(file_get_contents($this->openApiSchemaFileName));
$this->schemaStorage->addSchema($this->openApiSchemaFileName, $this->converter->convertDocument($openApiSchema));
}

/**
* Validate a response against the OpenApi schema.
*
Expand All @@ -91,11 +76,12 @@ public function validateResponse(
string $method,
int $responseCode,
string $contentType = 'application/json'
): bool {
): bool
{
if (!$this->emptyResponseExpected($responseCode)) {
$responseSchemaPath = $this->getResponseSchemaPath(preg_replace('/\?.*/', '', $pathName), $method, $responseCode, $contentType);
$responseJson = json_decode($response->getBody());
$this->jsonSchemaValidator->validate($responseJson, (object) ['$ref' => $responseSchemaPath]);
$this->jsonSchemaValidator->validate($responseJson, (object)['$ref' => $responseSchemaPath]);
}

if (!$this->jsonSchemaValidator->isValid()) {
Expand All @@ -108,7 +94,7 @@ public function validateResponse(
/**
* @param string $pathName
* @param string $method
* @param int $responseCode
* @param int $responseCode
* @param string $contentType
*
* @return string
Expand Down Expand Up @@ -252,4 +238,5 @@ private function emptyResponseExpected($responseCode): bool
{
return 204 === $responseCode;
}

}
21 changes: 20 additions & 1 deletion src/OpenApiValidatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ class OpenApiValidatorFactory
*/
public function v3Validator(string $openApiSchemaFileName): OpenApiValidatorInterface
{
return new OpenApiV3Validator($openApiSchemaFileName, new OpenApiV3ToJsonSchemaConverter(), new SchemaStorage());
$converter = new OpenApiV3ToJsonSchemaConverter();
$schemaStorage = new SchemaStorage();
$openApiSchema = json_decode(file_get_contents($openApiSchemaFileName));
$schemaStorage->addSchema($openApiSchemaFileName, $converter->convertDocument($openApiSchema));

return new OpenApiV3Validator($openApiSchemaFileName, $schemaStorage);
}

/**
* @param object $openApiSchema
* @param string $id
* @return OpenApiValidatorInterface
*/
public static function v3ValidatorFromSchema($openApiSchema, string $id = 'file://openapi'): OpenApiValidatorInterface
{
$converter = new OpenApiV3ToJsonSchemaConverter();
$schemaStorage = new SchemaStorage();
$schemaStorage->addSchema($id, $converter->convertDocument($openApiSchema));

return new OpenApiV3Validator($id, $schemaStorage);
}
}
1 change: 0 additions & 1 deletion tests/unit/OpenApiV3ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function setUp()

$this->validator = new OpenApiV3Validator(
'file://'.dirname(__DIR__).'/fixtures/openapiv3-schema.json',
new OpenApiV3ToJsonSchemaConverter(),
new SchemaStorage()
);
}
Expand Down
18 changes: 16 additions & 2 deletions tests/unit/OpenApiValidatorFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

class OpenApiValidatorFactoryTest extends TestCase
{

/**
* @var OpenApiValidatorFactory
*/
private $factory;

public function setUp()
{
parent::setUp();
Expand All @@ -19,7 +25,7 @@ public function testV3ValidatorSuccess()
{
$this->assertInstanceOf(
OpenApiV3Validator::class,
$this->factory->v3Validator('file://'.dirname(__DIR__).'/fixtures/openapiv3-schema.json')
$this->factory->v3Validator('file://' . dirname(__DIR__) . '/fixtures/openapiv3-schema.json')
);
}

Expand All @@ -30,7 +36,15 @@ public function testV3ValidatorFileNotFound()
{
$this->assertInstanceOf(
OpenApiV3Validator::class,
$this->factory->v3Validator('file://'.dirname(__DIR__).'/fixtures/doesnt-exist.json')
$this->factory->v3Validator('file://' . dirname(__DIR__) . '/fixtures/doesnt-exist.json')
);
}

public function testV3ValidatorFromSchemaSuccess()
{
$this->assertInstanceOf(
OpenApiV3Validator::class,
OpenApiValidatorFactory::v3ValidatorFromSchema((object)[])
);
}
}