Skip to content

Commit 9afd2bf

Browse files
committed
Add relationship in schema generation
1 parent 7a0766e commit 9afd2bf

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

src/Definitions/DefinitionGenerator.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
<?php namespace Mezatsong\SwaggerDocs\Definitions;
1+
<?php
2+
3+
namespace Mezatsong\SwaggerDocs\Definitions;
24

35
use Doctrine\DBAL\Types\Type;
46
use Illuminate\Container\Container;
7+
use Illuminate\Database\Eloquent\Model;
58
use Illuminate\Support\Facades\DB;
69
use Illuminate\Support\Facades\File;
710
use Illuminate\Support\Facades\Schema;
8-
use Illuminate\Database\Eloquent\Model;
911
use Illuminate\Support\Arr;
12+
use Illuminate\Support\Facades\Log;
1013
use Illuminate\Support\Str;
1114
use ReflectionClass;
1215

@@ -78,6 +81,17 @@ function generateSchemas(): array {
7881
$appends = $reflection->getProperty('appends');
7982
$appends->setAccessible(true);
8083

84+
$relations = collect($reflection->getMethods())
85+
->filter(
86+
fn($method) => !empty($method->getReturnType()) &&
87+
str_contains(
88+
$method->getReturnType(),
89+
\Illuminate\Database\Eloquent\Relations::class
90+
)
91+
)
92+
->pluck('name')
93+
->all();
94+
8195
$table = $obj->getTable();
8296
$list = Schema::getColumnListing($table);
8397
$list = array_diff($list, $obj->getHidden());
@@ -126,16 +140,33 @@ function generateSchemas(): array {
126140
}
127141
}
128142

129-
foreach ($with->getValue($obj) as $item) {
130-
$class = get_class($obj->{$item}()->getModel());
131-
$properties[$item] = [
143+
foreach ($relations as $relationName) {
144+
$relatedClass = get_class($obj->{$relationName}()->getRelated());
145+
$refObject = [
132146
'type' => 'object',
133-
'$ref' => '#/components/schemas/' . last(explode('\\', $class)),
147+
'$ref' => '#/components/schemas/' . last(explode('\\', $relatedClass)),
134148
];
149+
150+
$resultsClass = get_class((object) ($obj->{$relationName}()->getResults()));
151+
152+
if (str_contains($resultsClass, \Illuminate\Database\Eloquent\Collection::class)) {
153+
$properties[$relationName] = [
154+
'type' => 'array',
155+
'items'=> $refObject
156+
];
157+
} else {
158+
$properties[$relationName] = $refObject;
159+
}
135160
}
161+
162+
$required = array_merge($required, $with->getValue($obj));
136163

137164
foreach ($appends->getValue($obj) as $item) {
138165
$methodeName = 'get' . ucfirst(Str::camel($item)) . 'Attribute';
166+
if ( ! $reflection->hasMethod($methodeName)) {
167+
Log::warning("[Mezatsong\SwaggerDocs] Method $model::$methodeName not found while parsing '$item' attribute");
168+
continue;
169+
}
139170
$reflectionMethod = $reflection->getMethod($methodeName);
140171
$returnType = $reflectionMethod->getReturnType();
141172

src/SwaggerServiceProvider.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
<?php namespace Mezatsong\SwaggerDocs;
1+
<?php
2+
3+
namespace Mezatsong\SwaggerDocs;
24

35
use Illuminate\Support\Arr;
46
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Support\Facades\Log;
58
use Illuminate\Support\Facades\Validator;
69
use Illuminate\Support\ServiceProvider;
710
use Mezatsong\SwaggerDocs\Commands\GenerateSwaggerDocumentation;
@@ -49,9 +52,12 @@ public function boot(): void {
4952
$this->loadValidationRules();
5053

5154
try {
52-
DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
55+
DB::connection()
56+
->getDoctrineConnection()
57+
->getDatabasePlatform()
58+
->registerDoctrineTypeMapping('enum', 'string');
5359
} catch (\Exception $e) {
54-
error_log('Could not register enum type as string because of connexion error.');
60+
Log::error('[Mezatsong\SwaggerDocs] Could not register enum type as string because of connexion error.');
5561
}
5662

5763
if (file_exists($file = __DIR__ . '/helpers.php')) {

0 commit comments

Comments
 (0)