@@ -29,7 +29,7 @@ class SwaggerService
2929{
3030 use GetDependenciesTrait;
3131
32- public const SWAGGER_VERSION = '2 .0 ' ;
32+ public const string OPEN_API_VERSION = '3.1 .0 ' ;
3333
3434 protected $ driver ;
3535 protected $ openAPIValidator ;
@@ -46,19 +46,19 @@ class SwaggerService
4646 private $ item ;
4747 private $ security ;
4848
49- protected $ ruleToTypeMap = [
49+ protected array $ ruleToTypeMap = [
5050 'array ' => 'object ' ,
5151 'boolean ' => 'boolean ' ,
5252 'date ' => 'date ' ,
5353 'digits ' => 'integer ' ,
5454 'integer ' => 'integer ' ,
5555 'numeric ' => 'double ' ,
5656 'string ' => 'string ' ,
57- 'int ' => 'integer '
57+ 'int ' => 'integer ' ,
5858 ];
5959
6060 protected $ booleanAnnotations = [
61- 'deprecated '
61+ 'deprecated ' ,
6262 ];
6363
6464 public function __construct (Container $ container )
@@ -138,12 +138,14 @@ protected function generateEmptyData(): array
138138 }
139139
140140 $ data = [
141- 'swagger ' => self ::SWAGGER_VERSION ,
142- 'host ' => $ this -> getAppUrl (),
143- ' basePath ' => $ this ->config ['basePath ' ],
144- ' schemes ' => $ this -> config [ ' schemes ' ],
141+ 'openapi ' => self ::OPEN_API_VERSION ,
142+ 'servers ' => [
143+ [ ' url ' => $ this ->getAppUrl () . $ this -> config ['basePath ' ] ],
144+ ],
145145 'paths ' => [],
146- 'definitions ' => $ this ->config ['definitions ' ],
146+ 'components ' => [
147+ 'schemas ' => $ this ->config ['definitions ' ],
148+ ],
147149 'info ' => $ this ->prepareInfo ($ this ->config ['info ' ])
148150 ];
149151
@@ -242,7 +244,9 @@ protected function getPathParams(): array
242244 'name ' => $ key ,
243245 'description ' => $ this ->generatePathDescription ($ key ),
244246 'required ' => true ,
245- 'type ' => 'string '
247+ 'schema ' => [
248+ 'type ' => 'string '
249+ ]
246250 ];
247251 }
248252
@@ -307,7 +311,7 @@ protected function saveResponseSchema(?array $content, string $definition): void
307311 $ this ->saveObjectResponseDefinitions ($ content , $ schemaProperties , $ definition );
308312 }
309313
310- $ this ->data ['definitions ' ][$ definition ] = [
314+ $ this ->data ['components ' ][ ' schemas ' ][$ definition ] = [
311315 'type ' => $ schemaType ,
312316 'properties ' => $ schemaProperties
313317 ];
@@ -329,7 +333,9 @@ protected function saveListResponseDefinitions(array $content, array &$schemaPro
329333
330334 protected function saveObjectResponseDefinitions (array $ content , array &$ schemaProperties , string $ definition ): void
331335 {
332- $ properties = Arr::get ($ this ->data ['definitions ' ], $ definition , []);
336+ $ definitions = (!empty ($ this ->data ['components ' ]['schemas ' ])) ? $ this ->data ['components ' ]['schemas ' ] : [];
337+
338+ $ properties = Arr::get ($ definitions , $ definition , []);
333339
334340 foreach ($ content as $ name => $ value ) {
335341 $ property = Arr::get ($ properties , "properties. {$ name }" , []);
@@ -395,7 +401,7 @@ protected function parseResponse($response)
395401 $ this ->saveResponseSchema ($ content , $ definition );
396402
397403 if (is_array ($ this ->item ['responses ' ][$ code ])) {
398- $ this ->item ['responses ' ][$ code ]['schema ' ]['$ref ' ] = "#/definitions / {$ definition }" ;
404+ $ this ->item ['responses ' ][$ code ]['content ' ][ $ produce ][ ' schema ' ]['$ref ' ] = "#/components/schemas / {$ definition }" ;
399405 }
400406 }
401407
@@ -418,17 +424,23 @@ protected function saveExample($code, $content, $produce)
418424
419425 protected function makeResponseExample ($ content , $ mimeType , $ description = '' ): array
420426 {
421- $ responseExample = ['description ' => $ description ];
427+ $ example = match ($ mimeType ) {
428+ 'application/json ' => json_decode ($ content , true ),
429+ 'application/pdf ' => base64_encode ($ content ),
430+ default => $ content ,
431+ };
422432
423- if ($ mimeType === 'application/json ' ) {
424- $ responseExample ['schema ' ] = ['example ' => json_decode ($ content , true )];
425- } elseif ($ mimeType === 'application/pdf ' ) {
426- $ responseExample ['schema ' ] = ['example ' => base64_encode ($ content )];
427- } else {
428- $ responseExample ['examples ' ]['example ' ] = $ content ;
429- }
430-
431- return $ responseExample ;
433+ return [
434+ 'description ' => $ description ,
435+ 'content ' => [
436+ $ mimeType => [
437+ 'schema ' => [
438+ 'type ' => 'object ' ,
439+ ],
440+ 'example ' => $ example ,
441+ ],
442+ ],
443+ ];
432444 }
433445
434446 protected function saveParameters ($ request , array $ annotations )
@@ -504,7 +516,9 @@ protected function saveGetRequestParameters($rules, array $attributes, array $an
504516 'in ' => 'query ' ,
505517 'name ' => $ parameter ,
506518 'description ' => $ description ,
507- 'type ' => $ this ->getParameterType ($ validation )
519+ 'schema ' => [
520+ 'type ' => $ this ->getParameterType ($ validation ),
521+ ],
508522 ];
509523 if (in_array ('required ' , $ validation )) {
510524 $ parameterDefinition ['required ' ] = true ;
@@ -519,14 +533,18 @@ protected function savePostRequestParameters($actionName, $rules, array $attribu
519533 {
520534 if ($ this ->requestHasMoreProperties ($ actionName )) {
521535 if ($ this ->requestHasBody ()) {
522- $ this ->item ['parameters ' ][] = [
523- 'in ' => 'body ' ,
524- 'name ' => 'body ' ,
536+ $ type = $ this ->request ->header ('Content-Type ' ) ?? 'application/json ' ;
537+
538+ $ this ->item ['requestBody ' ] = [
539+ 'content ' => [
540+ $ type => [
541+ 'schema ' => [
542+ "\$ref " => "#/components/schemas/ {$ actionName }Object " ,
543+ ],
544+ ],
545+ ],
525546 'description ' => '' ,
526547 'required ' => true ,
527- 'schema ' => [
528- "\$ref " => "#/definitions/ {$ actionName }Object "
529- ]
530548 ];
531549 }
532550
@@ -559,7 +577,7 @@ protected function saveDefinitions($objectName, $rules, $attributes, array $anno
559577 }
560578
561579 $ data ['example ' ] = $ this ->generateExample ($ data ['properties ' ]);
562- $ this ->data ['definitions ' ][$ objectName . 'Object ' ] = $ data ;
580+ $ this ->data ['components ' ][ ' schemas ' ][$ objectName . 'Object ' ] = $ data ;
563581 }
564582
565583 protected function getParameterType (array $ validation ): string
@@ -600,8 +618,8 @@ protected function requestHasMoreProperties($actionName): bool
600618 {
601619 $ requestParametersCount = count ($ this ->request ->all ());
602620
603- if (isset ($ this ->data ['definitions ' ][$ actionName . 'Object ' ]['properties ' ])) {
604- $ objectParametersCount = count ($ this ->data ['definitions ' ][$ actionName . 'Object ' ]['properties ' ]);
621+ if (isset ($ this ->data ['components ' ][ ' schemas ' ][$ actionName . 'Object ' ]['properties ' ])) {
622+ $ objectParametersCount = count ($ this ->data ['components ' ][ ' schemas ' ][$ actionName . 'Object ' ]['properties ' ]);
605623 } else {
606624 $ objectParametersCount = 0 ;
607625 }
@@ -979,11 +997,11 @@ protected function mergeOpenAPIDocs(array &$documentation, array $additionalDocu
979997 }
980998 }
981999
982- $ definitions = array_keys ($ additionalDocumentation ['definitions ' ]);
1000+ $ definitions = array_keys ($ additionalDocumentation ['components ' ][ ' schemas ' ]);
9831001
9841002 foreach ($ definitions as $ definition ) {
985- if (empty ($ documentation ['definitions ' ][$ definition ])) {
986- $ documentation ['definitions ' ][$ definition ] = $ additionalDocumentation ['definitions ' ][$ definition ];
1003+ if (empty ($ documentation ['components ' ][ ' schemas ' ][$ definition ])) {
1004+ $ documentation ['components ' ][' schemas ' ][ $ definition ] = $ additionalDocumentation ['components ' ][ ' schemas ' ][$ definition ];
9871005 }
9881006 }
9891007 }
0 commit comments