22
33namespace Mezatsong \SwaggerDocs \Definitions ;
44
5- use Doctrine \DBAL \Types \Type ;
65use Illuminate \Container \Container ;
76use Illuminate \Database \Eloquent \Model ;
8- use Illuminate \Support \Facades \DB ;
97use Illuminate \Support \Facades \File ;
108use Illuminate \Support \Facades \Schema ;
119use 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}
0 commit comments