2323use RonasIT \AutoDoc \Traits \GetDependenciesTrait ;
2424use RonasIT \AutoDoc \Validators \SwaggerSpecValidator ;
2525use Symfony \Component \HttpFoundation \Response ;
26+ use Exception ;
2627
2728/**
2829 * @property SwaggerDriverContract $driver
@@ -131,14 +132,18 @@ protected function setDriver()
131132 }
132133 }
133134
134- protected function generateEmptyData (): array
135+ protected function generateEmptyData (? string $ view = null , array $ viewData = [], array $ license = [] ): array
135136 {
136137 // client must enter at least `contact.email` to generate a default `info` block
137138 // otherwise an exception will be called
138139 if (!empty ($ this ->config ['info ' ]) && !Arr::get ($ this ->config , 'info.contact.email ' )) {
139140 throw new EmptyContactEmailException ();
140141 }
141142
143+ if (empty ($ view ) && !empty ($ this ->config ['info ' ])) {
144+ $ view = $ this ->config ['info ' ]['description ' ];
145+ }
146+
142147 $ data = [
143148 'openapi ' => self ::OPEN_API_VERSION ,
144149 'servers ' => [
@@ -148,7 +153,7 @@ protected function generateEmptyData(): array
148153 'components ' => [
149154 'schemas ' => $ this ->config ['definitions ' ],
150155 ],
151- 'info ' => $ this ->prepareInfo ($ this -> config [ ' info ' ])
156+ 'info ' => $ this ->prepareInfo ($ view , $ viewData , $ license ),
152157 ];
153158
154159 $ securityDefinitions = $ this ->generateSecurityDefinition ();
@@ -260,7 +265,7 @@ protected function generatePathDescription(string $key): string
260265
261266 foreach ($ exploded as $ value ) {
262267 if (!preg_match ('/^[a-zA-Z0-9\.]+$/ ' , $ value )) {
263- return "regexp: {$ expression }" ;
268+ return "regexp: {$ expression }" ;
264269 }
265270 }
266271
@@ -596,8 +601,13 @@ protected function saveParameterType(&$data, $parameter, $parameterType)
596601 ];
597602 }
598603
599- protected function saveParameterDescription (&$ data , $ parameter , array $ rulesArray , array $ attributes , array $ annotations )
600- {
604+ protected function saveParameterDescription (
605+ array &$ data ,
606+ string $ parameter ,
607+ array $ rulesArray ,
608+ array $ attributes ,
609+ array $ annotations
610+ ) {
601611 $ description = Arr::get ($ annotations , $ parameter );
602612
603613 if (empty ($ description )) {
@@ -803,7 +813,7 @@ public function saveProductionData()
803813 if (ParallelTesting::token ()) {
804814 $ this ->driver ->appendProcessDataToTmpFile (function (array $ sharedTmpData ) {
805815 $ resultDocContent = (empty ($ sharedTmpData ))
806- ? $ this ->generateEmptyData ()
816+ ? $ this ->generateEmptyData ($ this -> config [ ' info ' ][ ' description ' ] )
807817 : $ sharedTmpData ;
808818
809819 $ this ->mergeOpenAPIDocs ($ resultDocContent , $ this ->data );
@@ -817,9 +827,13 @@ public function saveProductionData()
817827
818828 public function getDocFileContent ()
819829 {
820- $ documentation = $ this ->driver ->getDocumentation ();
830+ try {
831+ $ documentation = $ this ->driver ->getDocumentation ();
821832
822- $ this ->openAPIValidator ->validate ($ documentation );
833+ $ this ->openAPIValidator ->validate ($ documentation );
834+ } catch (Exception $ exception ) {
835+ return $ this ->generateEmptyData ($ this ->config ['defaults ' ]['error ' ], ['message ' => $ exception ->getMessage ()]);
836+ }
823837
824838 $ additionalDocs = config ('auto-doc.additional_paths ' , []);
825839
@@ -946,27 +960,21 @@ protected function getDefaultValueByType($type)
946960 return $ values [$ type ];
947961 }
948962
949- protected function prepareInfo (array $ info ): array
963+ protected function prepareInfo (? string $ view = null , array $ viewData = [], array $ license = [] ): array
950964 {
951- if (empty ($ info )) {
952- return $ info ;
953- }
965+ $ info = [];
954966
955- foreach ($ info ['license ' ] as $ key => $ value ) {
956- if (empty ($ value )) {
957- unset($ info ['license ' ][$ key ]);
958- }
959- }
967+ $ license = array_filter ($ license );
960968
961- if (empty ($ info [ ' license ' ] )) {
962- unset( $ info ['license ' ]) ;
969+ if (! empty ($ license )) {
970+ $ info ['license ' ] = $ license ;
963971 }
964972
965- if (!empty ($ info [ ' description ' ] )) {
966- $ info ['description ' ] = view ($ info [ ' description ' ] )->render ();
973+ if (!empty ($ view )) {
974+ $ info ['description ' ] = view ($ view , $ viewData )->render ();
967975 }
968-
969- return $ info ;
976+
977+ return array_merge ( $ this -> config [ ' info ' ], $ info ) ;
970978 }
971979
972980 protected function getOpenAPIFileContent (string $ filePath ): array
0 commit comments