Skip to content

Commit

Permalink
refactor: disable open api spec validation on main documentation in c…
Browse files Browse the repository at this point in the history
…onstructor
  • Loading branch information
DenTray committed Oct 28, 2023
1 parent 1ce8737 commit 92c86ee
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 47 deletions.
96 changes: 52 additions & 44 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SwaggerService
public const SWAGGER_VERSION = '2.0';

protected $driver;
protected $openAPIValidator;

protected $data;
protected $config;
Expand All @@ -57,6 +58,8 @@ class SwaggerService

public function __construct(Container $container)
{
$this->openAPIValidator = app(SwaggerSpecValidator::class);

$this->initConfig();

$this->setDriver();
Expand All @@ -68,9 +71,7 @@ public function __construct(Container $container)

$this->data = $this->driver->getTmpData();

if (!empty($this->data)) {
$this->validateSpec($this->data);
} else {
if (empty($this->data)) {
$this->data = $this->generateEmptyData();

$this->driver->saveTmpData($this->data);
Expand Down Expand Up @@ -692,55 +693,20 @@ public function getDocFileContent()
{
$documentation = $this->driver->getDocumentation();

$this->openAPIValidator->validate($documentation);

$additionalDocs = config('auto-doc.additional_paths', []);

foreach ($additionalDocs as $filePath) {
$fullFilePath = base_path($filePath);

try {
if (!file_exists($fullFilePath)) {
throw new DocFileNotExistsException($fullFilePath);
}

$fileContent = json_decode(file_get_contents($fullFilePath), true);

if (empty($fileContent)) {
throw new EmptyDocFileException($fullFilePath);
}

$this->validateSpec($fileContent);
$additionalDocContent = $this->getOpenAPIFileContent(base_path($filePath));
} catch (DocFileNotExistsException|EmptyDocFileException|InvalidSwaggerSpecException $exception) {
report($exception);

continue;
}

$paths = array_keys($fileContent['paths']);

foreach ($paths as $path) {
$additionalDocPath = $fileContent['paths'][$path];

if (empty($documentation['paths'][$path])) {
$documentation['paths'][$path] = $additionalDocPath;
} else {
$methods = array_keys($documentation['paths'][$path]);
$additionalDocMethods = array_keys($additionalDocPath);

foreach ($additionalDocMethods as $method) {
if (!in_array($method, $methods)) {
$documentation['paths'][$path][$method] = $additionalDocPath[$method];
}
}
}
}

$definitions = array_keys($fileContent['definitions']);

foreach ($definitions as $definition) {
if (empty($documentation['definitions'][$definition])) {
$documentation['definitions'][$definition] = $fileContent['definitions'][$definition];
}
}
$this->mergeOpenAPIDocs($documentation, $additionalDocContent);
}

return $documentation;
Expand Down Expand Up @@ -871,8 +837,50 @@ protected function prepareInfo(array $info): array
return $info;
}

protected function validateSpec(array $doc): void
protected function getOpenAPIFileContent(string $filePath): array
{
if (!file_exists($filePath)) {
throw new DocFileNotExistsException($filePath);
}

$fileContent = json_decode(file_get_contents($filePath), true);

if (empty($fileContent)) {
throw new EmptyDocFileException($filePath);
}

$this->openAPIValidator->validate($fileContent);

return $fileContent;
}

protected function mergeOpenAPIDocs(array &$documentation, array $additionalDocumentation): void
{
app(SwaggerSpecValidator::class)->validate($doc);
$paths = array_keys($additionalDocumentation['paths']);

foreach ($paths as $path) {
$additionalDocPath = $additionalDocumentation['paths'][$path];

if (empty($documentation['paths'][$path])) {
$documentation['paths'][$path] = $additionalDocPath;
} else {
$methods = array_keys($documentation['paths'][$path]);
$additionalDocMethods = array_keys($additionalDocPath);

foreach ($additionalDocMethods as $method) {
if (!in_array($method, $methods)) {
$documentation['paths'][$path][$method] = $additionalDocPath[$method];
}
}
}
}

$definitions = array_keys($additionalDocumentation['definitions']);

foreach ($definitions as $definition) {
if (empty($documentation['definitions'][$definition])) {
$documentation['definitions'][$definition] = $additionalDocumentation['definitions'][$definition];
}
}
}
}
7 changes: 4 additions & 3 deletions tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,14 @@ public function getConstructorInvalidTmpData(): array
* @param string $exception
* @param string $exceptionMessage
*/
public function testConstructorInvalidTmpData(string $tmpDoc, string $exception, string $exceptionMessage)
public function testGetDocFileContentInvalidTmpData(string $docFilePath, string $exception, string $exceptionMessage)
{
$this->mockDriverGetTpmData($this->getJsonFixture($tmpDoc));
$this->mockDriverGetDocumentation($this->getJsonFixture($docFilePath));

$this->expectException($exception);
$this->expectExceptionMessage($exceptionMessage);

app(SwaggerService::class);
app(SwaggerService::class)->getDocFileContent();
}

public function testEmptyContactEmail()
Expand Down
12 changes: 12 additions & 0 deletions tests/support/Traits/SwaggerServiceMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ protected function mockDriverGetTpmData($tmpData, $driverClass = LocalDriver::cl

$this->app->instance($driverClass, $driver);
}

protected function mockDriverGetDocumentation($data, $driverClass = LocalDriver::class)
{
$driver = $this->mockClass($driverClass, ['getDocumentation']);

$driver
->expects($this->exactly(1))
->method('getDocumentation')
->willReturn($data);

$this->app->instance($driverClass, $driver);
}
}

0 comments on commit 92c86ee

Please sign in to comment.