Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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: 10 additions & 1 deletion config/auto-doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@
'204' => 'Operation successfully done',
'404' => 'This entity not found',
],

/*
|--------------------------------------------------------------------------
| Error Template
|--------------------------------------------------------------------------
|
| You can use your custom description view for errors.
*/
'error' => 'auto-doc::error',
],

/*
Expand Down Expand Up @@ -211,5 +220,5 @@
],
],

'config_version' => '2.9',
'config_version' => '2.10',
];
1 change: 1 addition & 0 deletions resources/views/error.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ $message }}
1 change: 1 addition & 0 deletions src/AutoDocServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function boot()

$this->publishes([
__DIR__ . '/../resources/views/swagger-description.blade.php' => resource_path('views/vendor/auto-doc/swagger-description.blade.php'),
__DIR__ . '/../resources/views/error.blade.php' => resource_path('views/vendor/auto-doc/error.blade.php'),
], 'view');

if (!$this->app->routesAreCached()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Drivers/LocalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RonasIT\AutoDoc\Drivers;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\MissedProductionFilePathException;

class LocalDriver extends BaseDriver
Expand Down Expand Up @@ -30,7 +30,7 @@ public function saveData(): void
public function getDocumentation(): array
{
if (!file_exists($this->prodFilePath)) {
throw new FileNotFoundException();
throw new FileNotFoundException($this->prodFilePath);
}

$fileContent = file_get_contents($this->prodFilePath);
Expand Down
4 changes: 2 additions & 2 deletions src/Drivers/RemoteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RonasIT\AutoDoc\Drivers;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\MissedRemoteDocumentationUrlException;

class RemoteDriver extends BaseDriver
Expand Down Expand Up @@ -36,7 +36,7 @@ public function getDocumentation(): array
list($content, $statusCode) = $this->makeHttpRequest('get', $this->getUrl());

if (empty($content) || $statusCode !== 200) {
throw new FileNotFoundException();
throw new FileNotFoundException('Documentation file not found.');
}

return json_decode($content, true);
Expand Down
4 changes: 2 additions & 2 deletions src/Drivers/StorageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RonasIT\AutoDoc\Drivers;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\FileNotFoundException;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Storage;
use RonasIT\AutoDoc\Exceptions\MissedProductionFilePathException;
Expand Down Expand Up @@ -34,7 +34,7 @@ public function saveData(): void
public function getDocumentation(): array
{
if (!$this->disk->exists($this->prodFilePath)) {
throw new FileNotFoundException();
throw new FileNotFoundException($this->prodFilePath);
}

$fileContent = $this->disk->get($this->prodFilePath);
Expand Down
17 changes: 17 additions & 0 deletions src/Exceptions/FileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace RonasIT\AutoDoc\Exceptions;

use \Illuminate\Contracts\Filesystem\FileNotFoundException as BaseException;

class FileNotFoundException extends BaseException
{
public function __construct(string $filePath = null)
{
$message = empty($filePath)
? 'Documentation file not found'
: "Documentation file not found: {$filePath}";

parent::__construct($message);
}
}
37 changes: 31 additions & 6 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use RonasIT\AutoDoc\Traits\GetDependenciesTrait;
use RonasIT\AutoDoc\Validators\SwaggerSpecValidator;
use Symfony\Component\HttpFoundation\Response;
use Exception;

/**
* @property SwaggerDriverContract $driver
Expand Down Expand Up @@ -131,8 +132,11 @@ protected function setDriver()
}
}

protected function generateEmptyData(): array
protected function generateEmptyData(array $info = [], $decriptionData = []): array
{
if(empty($info)){
$info = $this->config['info'];
}
// client must enter at least `contact.email` to generate a default `info` block
// otherwise an exception will be called
if (!empty($this->config['info']) && !Arr::get($this->config, 'info.contact.email')) {
Expand All @@ -148,7 +152,7 @@ protected function generateEmptyData(): array
'components' => [
'schemas' => $this->config['definitions'],
],
'info' => $this->prepareInfo($this->config['info'])
'info' => $this->prepareInfo($info, $decriptionData),
];

$securityDefinitions = $this->generateSecurityDefinition();
Expand All @@ -160,6 +164,20 @@ protected function generateEmptyData(): array
return $data;
}

protected function generateBaseDataObject(): array
{
return [
'openapi' => self::OPEN_API_VERSION,
'servers' => [
['url' => URL::query($this->config['basePath'])],
],
'paths' => [],
'components' => [
'schemas' => $this->config['definitions'],
],
];
}

protected function generateSecurityDefinition(): ?array
{
if (empty($this->security)) {
Expand Down Expand Up @@ -817,9 +835,16 @@ public function saveProductionData()

public function getDocFileContent()
{
$documentation = $this->driver->getDocumentation();
try {
$documentation = $this->driver->getDocumentation();

$this->openAPIValidator->validate($documentation);
$this->openAPIValidator->validate($documentation);
} catch (Exception $exception) {
$infoConfig = $this->config['info'];
$infoConfig['description'] = Arr::get($this->config, 'defaults.error');

return $this->generateEmptyData($infoConfig, ['message' => $exception->getMessage()]);
}

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

Expand Down Expand Up @@ -946,7 +971,7 @@ protected function getDefaultValueByType($type)
return $values[$type];
}

protected function prepareInfo(array $info): array
protected function prepareInfo(array $info, array $descriptionData = []): array
{
if (empty($info)) {
return $info;
Expand All @@ -963,7 +988,7 @@ protected function prepareInfo(array $info): array
}

if (!empty($info['description'])) {
$info['description'] = view($info['description'])->render();
$info['description'] = view($info['description'], $descriptionData)->render();
}

return $info;
Expand Down
Loading