@@ -31,6 +31,7 @@ class SwaggerService
3131 public const SWAGGER_VERSION = '2.0 ' ;
3232
3333 protected $ driver ;
34+ protected $ openAPIValidator ;
3435
3536 protected $ data ;
3637 protected $ config ;
@@ -57,6 +58,8 @@ class SwaggerService
5758
5859 public function __construct (Container $ container )
5960 {
61+ $ this ->openAPIValidator = app (SwaggerSpecValidator::class);
62+
6063 $ this ->initConfig ();
6164
6265 $ this ->setDriver ();
@@ -68,9 +71,7 @@ public function __construct(Container $container)
6871
6972 $ this ->data = $ this ->driver ->getTmpData ();
7073
71- if (!empty ($ this ->data )) {
72- $ this ->validateSpec ($ this ->data );
73- } else {
74+ if (empty ($ this ->data )) {
7475 $ this ->data = $ this ->generateEmptyData ();
7576
7677 $ this ->driver ->saveTmpData ($ this ->data );
@@ -692,55 +693,20 @@ public function getDocFileContent()
692693 {
693694 $ documentation = $ this ->driver ->getDocumentation ();
694695
696+ $ this ->openAPIValidator ->validate ($ documentation );
697+
695698 $ additionalDocs = config ('auto-doc.additional_paths ' , []);
696699
697700 foreach ($ additionalDocs as $ filePath ) {
698- $ fullFilePath = base_path ($ filePath );
699-
700701 try {
701- if (!file_exists ($ fullFilePath )) {
702- throw new DocFileNotExistsException ($ fullFilePath );
703- }
704-
705- $ fileContent = json_decode (file_get_contents ($ fullFilePath ), true );
706-
707- if (empty ($ fileContent )) {
708- throw new EmptyDocFileException ($ fullFilePath );
709- }
710-
711- $ this ->validateSpec ($ fileContent );
702+ $ additionalDocContent = $ this ->getOpenAPIFileContent (base_path ($ filePath ));
712703 } catch (DocFileNotExistsException |EmptyDocFileException |InvalidSwaggerSpecException $ exception ) {
713704 report ($ exception );
714705
715706 continue ;
716707 }
717708
718- $ paths = array_keys ($ fileContent ['paths ' ]);
719-
720- foreach ($ paths as $ path ) {
721- $ additionalDocPath = $ fileContent ['paths ' ][$ path ];
722-
723- if (empty ($ documentation ['paths ' ][$ path ])) {
724- $ documentation ['paths ' ][$ path ] = $ additionalDocPath ;
725- } else {
726- $ methods = array_keys ($ documentation ['paths ' ][$ path ]);
727- $ additionalDocMethods = array_keys ($ additionalDocPath );
728-
729- foreach ($ additionalDocMethods as $ method ) {
730- if (!in_array ($ method , $ methods )) {
731- $ documentation ['paths ' ][$ path ][$ method ] = $ additionalDocPath [$ method ];
732- }
733- }
734- }
735- }
736-
737- $ definitions = array_keys ($ fileContent ['definitions ' ]);
738-
739- foreach ($ definitions as $ definition ) {
740- if (empty ($ documentation ['definitions ' ][$ definition ])) {
741- $ documentation ['definitions ' ][$ definition ] = $ fileContent ['definitions ' ][$ definition ];
742- }
743- }
709+ $ this ->mergeOpenAPIDocs ($ documentation , $ additionalDocContent );
744710 }
745711
746712 return $ documentation ;
@@ -871,8 +837,50 @@ protected function prepareInfo(array $info): array
871837 return $ info ;
872838 }
873839
874- protected function validateSpec (array $ doc ): void
840+ protected function getOpenAPIFileContent (string $ filePath ): array
841+ {
842+ if (!file_exists ($ filePath )) {
843+ throw new DocFileNotExistsException ($ filePath );
844+ }
845+
846+ $ fileContent = json_decode (file_get_contents ($ filePath ), true );
847+
848+ if (empty ($ fileContent )) {
849+ throw new EmptyDocFileException ($ filePath );
850+ }
851+
852+ $ this ->openAPIValidator ->validate ($ fileContent );
853+
854+ return $ fileContent ;
855+ }
856+
857+ protected function mergeOpenAPIDocs (array &$ documentation , array $ additionalDocumentation ): void
875858 {
876- app (SwaggerSpecValidator::class)->validate ($ doc );
859+ $ paths = array_keys ($ additionalDocumentation ['paths ' ]);
860+
861+ foreach ($ paths as $ path ) {
862+ $ additionalDocPath = $ additionalDocumentation ['paths ' ][$ path ];
863+
864+ if (empty ($ documentation ['paths ' ][$ path ])) {
865+ $ documentation ['paths ' ][$ path ] = $ additionalDocPath ;
866+ } else {
867+ $ methods = array_keys ($ documentation ['paths ' ][$ path ]);
868+ $ additionalDocMethods = array_keys ($ additionalDocPath );
869+
870+ foreach ($ additionalDocMethods as $ method ) {
871+ if (!in_array ($ method , $ methods )) {
872+ $ documentation ['paths ' ][$ path ][$ method ] = $ additionalDocPath [$ method ];
873+ }
874+ }
875+ }
876+ }
877+
878+ $ definitions = array_keys ($ additionalDocumentation ['definitions ' ]);
879+
880+ foreach ($ definitions as $ definition ) {
881+ if (empty ($ documentation ['definitions ' ][$ definition ])) {
882+ $ documentation ['definitions ' ][$ definition ] = $ additionalDocumentation ['definitions ' ][$ definition ];
883+ }
884+ }
877885 }
878886}
0 commit comments