|
1 | | -<?php namespace Mezatsong\SwaggerDocs\Definitions; |
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Mezatsong\SwaggerDocs\Definitions; |
2 | 4 |
|
3 | 5 | use Doctrine\DBAL\Types\Type; |
4 | 6 | use Illuminate\Container\Container; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
5 | 8 | use Illuminate\Support\Facades\DB; |
6 | 9 | use Illuminate\Support\Facades\File; |
7 | 10 | use Illuminate\Support\Facades\Schema; |
8 | | -use Illuminate\Database\Eloquent\Model; |
9 | 11 | use Illuminate\Support\Arr; |
| 12 | +use Illuminate\Support\Facades\Log; |
10 | 13 | use Illuminate\Support\Str; |
11 | 14 | use ReflectionClass; |
12 | 15 |
|
@@ -78,6 +81,17 @@ function generateSchemas(): array { |
78 | 81 | $appends = $reflection->getProperty('appends'); |
79 | 82 | $appends->setAccessible(true); |
80 | 83 |
|
| 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 | + |
81 | 95 | $table = $obj->getTable(); |
82 | 96 | $list = Schema::getColumnListing($table); |
83 | 97 | $list = array_diff($list, $obj->getHidden()); |
@@ -126,16 +140,33 @@ function generateSchemas(): array { |
126 | 140 | } |
127 | 141 | } |
128 | 142 |
|
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 = [ |
132 | 146 | 'type' => 'object', |
133 | | - '$ref' => '#/components/schemas/' . last(explode('\\', $class)), |
| 147 | + '$ref' => '#/components/schemas/' . last(explode('\\', $relatedClass)), |
134 | 148 | ]; |
| 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 | + } |
135 | 160 | } |
| 161 | + |
| 162 | + $required = array_merge($required, $with->getValue($obj)); |
136 | 163 |
|
137 | 164 | foreach ($appends->getValue($obj) as $item) { |
138 | 165 | $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 | + } |
139 | 170 | $reflectionMethod = $reflection->getMethod($methodeName); |
140 | 171 | $returnType = $reflectionMethod->getReturnType(); |
141 | 172 |
|
|
0 commit comments