Skip to content

Commit 92c86ee

Browse files
committed
refactor: disable open api spec validation on main documentation in constructor
1 parent 1ce8737 commit 92c86ee

File tree

3 files changed

+68
-47
lines changed

3 files changed

+68
-47
lines changed

src/Services/SwaggerService.php

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class SwaggerService
3131
public const SWAGGER_VERSION = '2.0';
3232

3333
protected $driver;
34+
protected $openAPIValidator;
3435

3536
protected $data;
3637
protected $config;
@@ -57,6 +58,8 @@ class SwaggerService
5758

5859
public function __construct(Container $container)
5960
{
61+
$this->openAPIValidator = app(SwaggerSpecValidator::class);
62+
6063
$this->initConfig();
6164

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

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

71-
if (!empty($this->data)) {
72-
$this->validateSpec($this->data);
73-
} else {
74+
if (empty($this->data)) {
7475
$this->data = $this->generateEmptyData();
7576

7677
$this->driver->saveTmpData($this->data);
@@ -692,55 +693,20 @@ public function getDocFileContent()
692693
{
693694
$documentation = $this->driver->getDocumentation();
694695

696+
$this->openAPIValidator->validate($documentation);
697+
695698
$additionalDocs = config('auto-doc.additional_paths', []);
696699

697700
foreach ($additionalDocs as $filePath) {
698-
$fullFilePath = base_path($filePath);
699-
700701
try {
701-
if (!file_exists($fullFilePath)) {
702-
throw new DocFileNotExistsException($fullFilePath);
703-
}
704-
705-
$fileContent = json_decode(file_get_contents($fullFilePath), true);
706-
707-
if (empty($fileContent)) {
708-
throw new EmptyDocFileException($fullFilePath);
709-
}
710-
711-
$this->validateSpec($fileContent);
702+
$additionalDocContent = $this->getOpenAPIFileContent(base_path($filePath));
712703
} catch (DocFileNotExistsException|EmptyDocFileException|InvalidSwaggerSpecException $exception) {
713704
report($exception);
714705

715706
continue;
716707
}
717708

718-
$paths = array_keys($fileContent['paths']);
719-
720-
foreach ($paths as $path) {
721-
$additionalDocPath = $fileContent['paths'][$path];
722-
723-
if (empty($documentation['paths'][$path])) {
724-
$documentation['paths'][$path] = $additionalDocPath;
725-
} else {
726-
$methods = array_keys($documentation['paths'][$path]);
727-
$additionalDocMethods = array_keys($additionalDocPath);
728-
729-
foreach ($additionalDocMethods as $method) {
730-
if (!in_array($method, $methods)) {
731-
$documentation['paths'][$path][$method] = $additionalDocPath[$method];
732-
}
733-
}
734-
}
735-
}
736-
737-
$definitions = array_keys($fileContent['definitions']);
738-
739-
foreach ($definitions as $definition) {
740-
if (empty($documentation['definitions'][$definition])) {
741-
$documentation['definitions'][$definition] = $fileContent['definitions'][$definition];
742-
}
743-
}
709+
$this->mergeOpenAPIDocs($documentation, $additionalDocContent);
744710
}
745711

746712
return $documentation;
@@ -871,8 +837,50 @@ protected function prepareInfo(array $info): array
871837
return $info;
872838
}
873839

874-
protected function validateSpec(array $doc): void
840+
protected function getOpenAPIFileContent(string $filePath): array
841+
{
842+
if (!file_exists($filePath)) {
843+
throw new DocFileNotExistsException($filePath);
844+
}
845+
846+
$fileContent = json_decode(file_get_contents($filePath), true);
847+
848+
if (empty($fileContent)) {
849+
throw new EmptyDocFileException($filePath);
850+
}
851+
852+
$this->openAPIValidator->validate($fileContent);
853+
854+
return $fileContent;
855+
}
856+
857+
protected function mergeOpenAPIDocs(array &$documentation, array $additionalDocumentation): void
875858
{
876-
app(SwaggerSpecValidator::class)->validate($doc);
859+
$paths = array_keys($additionalDocumentation['paths']);
860+
861+
foreach ($paths as $path) {
862+
$additionalDocPath = $additionalDocumentation['paths'][$path];
863+
864+
if (empty($documentation['paths'][$path])) {
865+
$documentation['paths'][$path] = $additionalDocPath;
866+
} else {
867+
$methods = array_keys($documentation['paths'][$path]);
868+
$additionalDocMethods = array_keys($additionalDocPath);
869+
870+
foreach ($additionalDocMethods as $method) {
871+
if (!in_array($method, $methods)) {
872+
$documentation['paths'][$path][$method] = $additionalDocPath[$method];
873+
}
874+
}
875+
}
876+
}
877+
878+
$definitions = array_keys($additionalDocumentation['definitions']);
879+
880+
foreach ($definitions as $definition) {
881+
if (empty($documentation['definitions'][$definition])) {
882+
$documentation['definitions'][$definition] = $additionalDocumentation['definitions'][$definition];
883+
}
884+
}
877885
}
878886
}

tests/SwaggerServiceTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,14 @@ public function getConstructorInvalidTmpData(): array
281281
* @param string $exception
282282
* @param string $exceptionMessage
283283
*/
284-
public function testConstructorInvalidTmpData(string $tmpDoc, string $exception, string $exceptionMessage)
284+
public function testGetDocFileContentInvalidTmpData(string $docFilePath, string $exception, string $exceptionMessage)
285285
{
286-
$this->mockDriverGetTpmData($this->getJsonFixture($tmpDoc));
286+
$this->mockDriverGetDocumentation($this->getJsonFixture($docFilePath));
287+
287288
$this->expectException($exception);
288289
$this->expectExceptionMessage($exceptionMessage);
289290

290-
app(SwaggerService::class);
291+
app(SwaggerService::class)->getDocFileContent();
291292
}
292293

293294
public function testEmptyContactEmail()

tests/support/Traits/SwaggerServiceMockTrait.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,16 @@ protected function mockDriverGetTpmData($tmpData, $driverClass = LocalDriver::cl
6060

6161
$this->app->instance($driverClass, $driver);
6262
}
63+
64+
protected function mockDriverGetDocumentation($data, $driverClass = LocalDriver::class)
65+
{
66+
$driver = $this->mockClass($driverClass, ['getDocumentation']);
67+
68+
$driver
69+
->expects($this->exactly(1))
70+
->method('getDocumentation')
71+
->willReturn($data);
72+
73+
$this->app->instance($driverClass, $driver);
74+
}
6375
}

0 commit comments

Comments
 (0)