Skip to content

Commit 0ad3fd2

Browse files
committed
Make type align with OpenAPI 3.0 spec AND change default docs url to /docs
1 parent 8ea1cc8 commit 0ad3fd2

File tree

9 files changed

+698
-766
lines changed

9 files changed

+698
-766
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
],
1313
"require": {
1414
"php": ">=7.3 || 8.*",
15-
"doctrine/dbal": "*",
1615
"phpdocumentor/reflection-docblock": "^5.2"
1716
},
1817
"require-dev": {

composer.lock

Lines changed: 557 additions & 722 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/swagger.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@
109109
'/_ignition/share-report',
110110
'/_ignition/scripts/{script}',
111111
'/_ignition/styles/{style}',
112-
env('SWAGGER_PATH', '/documentation'),
113-
env('SWAGGER_PATH', '/documentation') . '/content'
112+
env('SWAGGER_PATH', '/docs'),
113+
env('SWAGGER_PATH', '/docs') . '/content'
114114
],
115115

116116
'models' => []

src/Definitions/DefinitionGenerator.php

Lines changed: 103 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace Mezatsong\SwaggerDocs\Definitions;
44

5-
use Doctrine\DBAL\Types\Type;
65
use Illuminate\Container\Container;
76
use Illuminate\Database\Eloquent\Model;
8-
use Illuminate\Support\Facades\DB;
97
use Illuminate\Support\Facades\File;
108
use Illuminate\Support\Facades\Schema;
119
use Illuminate\Support\Arr;
@@ -87,7 +85,8 @@ function generateSchemas(): array {
8785
fn($method) => !empty($method->getReturnType()) &&
8886
str_contains(
8987
$method->getReturnType(),
90-
\Illuminate\Database\Eloquent\Relations::class
88+
"Illuminate\Database\Eloquent\Relations",
89+
// \Illuminate\Database\Eloquent\Relations::class
9190
)
9291
)
9392
->pluck('name')
@@ -110,18 +109,24 @@ function generateSchemas(): array {
110109
}
111110

112111
foreach ($columns as $column) {
112+
$swaggerProps = $this->convertDBTypeToSwaggerType($column['type']);
113+
113114
$description = $column['comment'];
114115
if (!is_null($description)) {
115-
$column['description'] .= ": $description";
116+
$swaggerProps['description'] .= ": $description";
116117
}
117118

118-
$this->addExampleKey($column);
119-
120-
$properties[$column['name']] = $column;
119+
if (isset($column['default']) && $column['default']) {
120+
$swaggerProps['default'] = $column['default'];
121+
} else {
122+
$this->addExampleKey($column);
123+
}
121124

122125
if (!$column['nullable']) {
123126
$required[] = $column['name'];
124127
}
128+
129+
$properties[$column['name']] = $swaggerProps;
125130
}
126131

127132
foreach ($relations as $relationName) {
@@ -166,7 +171,7 @@ function generateSchemas(): array {
166171
$data['$ref'] = '#/components/schemas/' . last(explode('\\', $type));
167172
}
168173
} else {
169-
$data['type'] = $type;
174+
$data = $this->convertPhpTypeToSwaggerType($type);
170175
$this->addExampleKey($data);
171176
}
172177
}
@@ -218,6 +223,7 @@ private function addExampleKey(array & $property): void {
218223
break;
219224
case 'serial':
220225
case 'integer':
226+
case 'int':
221227
Arr::set($property, 'example', rand(1000000000, 2000000000));
222228
break;
223229
case 'mediumint':
@@ -253,6 +259,7 @@ private function addExampleKey(array & $property): void {
253259
case 'text':
254260
Arr::set($property, 'example', 'a long text');
255261
break;
262+
case 'bool':
256263
case 'boolean':
257264
Arr::set($property, 'example', rand(0,1) == 0);
258265
break;
@@ -267,7 +274,7 @@ private function addExampleKey(array & $property): void {
267274
/**
268275
* @return array array of with 'type' and 'format' as keys
269276
*/
270-
private function convertDBalTypeToSwaggerType(string $type): array {
277+
private function convertDBTypeToSwaggerType(string $type): array {
271278
$lowerType = strtolower($type);
272279
switch ($lowerType) {
273280
case 'bigserial':
@@ -282,7 +289,6 @@ private function convertDBalTypeToSwaggerType(string $type): array {
282289
case 'mediumint':
283290
case 'smallint':
284291
case 'tinyint':
285-
case 'tinyint':
286292
case 'year':
287293
$property = ['type' => 'integer'];
288294
break;
@@ -324,20 +330,103 @@ private function convertDBalTypeToSwaggerType(string $type): array {
324330
'format' => 'binary',
325331
];
326332
break;
327-
case 'time':
328333
case 'string':
329334
case 'text':
330-
case 'char':
335+
case 'mediumtext':
336+
case 'longtext':
331337
case 'varchar':
338+
$property = ['type' => 'string'];
339+
break;
340+
case 'time':
341+
case 'char':
342+
$property = ['type' => 'string', 'description' => $type];
343+
break;
332344
case 'enum':
345+
$property = ['type' => 'string'];
346+
break;
333347
case 'set':
334348
default:
335-
$property = ['type' => 'string'];
349+
$property = [
350+
'type' => 'object',
351+
'nullable' => true,
352+
'additionalProperties' => true,
353+
'description' => $type,
354+
];
336355
break;
337356
}
338357

339-
$property['description'] = $type;
358+
if (!isset($property['format']) && in_array($type, ['boolean', 'enum', 'object']) ) {
359+
$property['format'] = $type;
360+
}
340361

341362
return $property;
342363
}
364+
365+
/**
366+
* @return array array of with 'type' and 'format' as keys
367+
*/
368+
private function convertPhpTypeToSwaggerType(string $phpType): array {
369+
$mapping = [
370+
'int' => [
371+
'type' => 'integer',
372+
'format' => 'int32'
373+
],
374+
'float' => [
375+
'type' => 'number',
376+
'format' => 'float'
377+
],
378+
'string' => [
379+
'type' => 'string'
380+
],
381+
'bool' => [
382+
'type' => 'boolean'
383+
],
384+
'array' => [
385+
'type' => 'array',
386+
'items' => [
387+
'type' => 'object',
388+
'nullable' => true,
389+
'additionalProperties' => true
390+
],
391+
],
392+
'object' => [
393+
'type' => 'object',
394+
'additionalProperties' => true,
395+
],
396+
'?int' => [
397+
'type' => 'integer',
398+
'nullable' => true
399+
],
400+
'?float' => [
401+
'type' => 'number',
402+
'format' => 'float',
403+
'nullable' => true
404+
],
405+
'?string' => [
406+
'type' => 'string',
407+
'nullable' => true
408+
],
409+
'?bool' => [
410+
'type' => 'boolean',
411+
'nullable' => true
412+
],
413+
'?array' => [
414+
'type' => 'array',
415+
'items' => [
416+
'type' => 'object',
417+
'nullable' => true,
418+
],
419+
'nullable' => true,
420+
],
421+
'?object' => [
422+
'type' => 'object',
423+
'nullable' => true,
424+
],
425+
];
426+
427+
return $mapping[strtolower($phpType)] ?? [
428+
'type' => 'object',
429+
'nullable' => true,
430+
];
431+
}
343432
}

src/Http/Controllers/SwaggerController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function api(Request $request): Response {
7676
}
7777
return ResponseFacade::make(view('swagger::index', [
7878
'secure' => swagger_is_connection_secure(),
79-
'urlToDocs' => $url . config('swagger.path', '/documentation') . '/content'
79+
'urlToDocs' => $url . config('swagger.path', '/docs') . '/content'
8080
]), 200);
8181
}
8282

src/Parameters/BodyParametersGenerator.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getParameters(): array {
4747
try {
4848
$parameterRules = $this->splitRules($rule);
4949
$nameTokens = explode('.', $parameter);
50-
$this->addToProperties($properties, $nameTokens, $parameterRules);
50+
$this->addToProperties($properties, $nameTokens, $parameterRules);
5151

5252
if ($this->isParameterRequired($parameterRules)) {
5353
$required[] = $parameter;
@@ -109,24 +109,41 @@ protected function addToProperties(array & $properties, array $nameTokens, array
109109
}
110110

111111
if ($name === '*') {
112-
$name = 0;
113-
}
112+
if (empty($properties)) {
113+
$propertyObject = $this->createNewPropertyObject($type, $rules);
114+
foreach ($propertyObject as $key => $value) {
115+
Arr::set($properties, $key, $value);
116+
}
117+
$extra = $this->getParameterExtra($type, $rules);
118+
foreach($extra as $key => $value) {
119+
Arr::set($properties, $key, $value);
120+
}
121+
} else {
122+
Arr::set($properties, 'type', $type);
123+
}
114124

115-
if (!Arr::has($properties, $name)) {
116-
$propertyObject = $this->createNewPropertyObject($type, $rules);
117-
Arr::set($properties, $name, $propertyObject);
118-
$extra = $this->getParameterExtra($type, $rules);
119-
foreach($extra as $key => $value) {
120-
Arr::set($properties, $name . '.' . $key, $value);
125+
if ($type === 'array') {
126+
$this->addToProperties($properties['items'], $nameTokens, $rules);
127+
} else if ($type === 'object' && isset($properties['properties'])) {
128+
$this->addToProperties($properties['properties'], $nameTokens, $rules);
121129
}
122130
} else {
123-
Arr::set($properties, $name . '.type', $type);
124-
}
131+
if (!Arr::has($properties, $name)) {
132+
$propertyObject = $this->createNewPropertyObject($type, $rules);
133+
Arr::set($properties, $name, $propertyObject);
134+
$extra = $this->getParameterExtra($type, $rules);
135+
foreach($extra as $key => $value) {
136+
Arr::set($properties, $name . '.' . $key, $value);
137+
}
138+
} else {
139+
Arr::set($properties, $name . '.type', $type);
140+
}
125141

126-
if ($type === 'array') {
127-
$this->addToProperties($properties[$name]['items'], $nameTokens, $rules);
128-
} else if ($type === 'object' && isset($properties[$name]['properties'])) {
129-
$this->addToProperties($properties[$name]['properties'], $nameTokens, $rules);
142+
if ($type === 'array') {
143+
$this->addToProperties($properties[$name]['items'], $nameTokens, $rules);
144+
} else if ($type === 'object' && isset($properties[$name]['properties'])) {
145+
$this->addToProperties($properties[$name]['properties'], $nameTokens, $rules);
146+
}
130147
}
131148
}
132149

src/Parameters/QueryParametersGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public function getParameters(): array {
6262
];
6363

6464
if (\count($enums) > 0) {
65-
Arr::set($parameterObject, 'enum', $enums);
65+
Arr::set($parameterObject, 'schema.type', 'string');
66+
Arr::set($parameterObject, 'schema.enum', $enums);
6667
} else {
6768
Arr::set($parameterObject, 'schema.type', $type);
6869
}

src/SwaggerServiceProvider.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
namespace Mezatsong\SwaggerDocs;
44

55
use Illuminate\Support\Arr;
6-
use Illuminate\Support\Facades\DB;
7-
use Illuminate\Support\Facades\Log;
8-
use Illuminate\Support\Facades\Schema;
96
use Illuminate\Support\Facades\Validator;
107
use Illuminate\Support\ServiceProvider;
118
use Mezatsong\SwaggerDocs\Commands\GenerateSwaggerDocumentation;
@@ -52,12 +49,6 @@ public function boot(): void {
5249

5350
$this->loadValidationRules();
5451

55-
try {
56-
Schema::registerEnumMapping('enum', 'string');
57-
} catch (\Exception $e) {
58-
Log::error('[Mezatsong\SwaggerDocs] Could not register enum type as string because of connexion error.');
59-
}
60-
6152
if (file_exists($file = __DIR__ . '/helpers.php')) {
6253
require $file;
6354
}

src/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Mezatsong\SwaggerDocs\Http\Controllers\SwaggerController;
66

77
if (Config::get('swagger.enable', true)) {
8-
Route::prefix(config('swagger.path', '/documentation'))->group(static function() {
8+
Route::prefix(config('swagger.path', '/docs'))->group(static function() {
99
Route::get('', [SwaggerController::class, 'api']);
1010
Route::get('content', [SwaggerController::class, 'documentation']);
1111
});

0 commit comments

Comments
 (0)