@@ -7,7 +7,7 @@ class LaravelRequestDocsToOpenApi
77 private array $ openApi = [];
88
99 /**
10- * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
10+ * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
1111 * @return $this
1212 */
1313 public function openApi (array $ docs ): LaravelRequestDocsToOpenApi
@@ -23,18 +23,19 @@ public function openApi(array $docs): LaravelRequestDocsToOpenApi
2323 ];
2424
2525 $ this ->docsToOpenApi ($ docs );
26+ $ this ->appendGlobalSecurityScheme ();
2627 return $ this ;
2728 }
2829
2930 /**
30- * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
31+ * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
3132 * @return void
3233 */
3334 private function docsToOpenApi (array $ docs ): void
3435 {
3536 $ this ->openApi ['paths ' ] = [];
3637 $ deleteWithBody = config ('request-docs.open_api.delete_with_body ' , false );
37- $ excludeHttpMethods = array_map (fn ($ item ) => strtolower ($ item ), config ('request-docs.open_api.exclude_http_methods ' , []));
38+ $ excludeHttpMethods = array_map (fn ($ item ) => strtolower ($ item ), config ('request-docs.open_api.exclude_http_methods ' , []));
3839
3940 foreach ($ docs as $ doc ) {
4041 $ httpMethod = strtolower ($ doc ->getHttpMethod ());
@@ -90,6 +91,7 @@ private function docsToOpenApi(array $docs): void
9091 }
9192 }
9293 }
94+
9395 protected function setAndFilterResponses (Doc $ doc ): array
9496 {
9597 $ docResponses = $ doc ->getResponses ();
@@ -115,12 +117,12 @@ protected function makeQueryParameterItem(string $attribute, $rule): array
115117 $ rule = implode ('| ' , $ rule );
116118 }
117119 $ parameter = [
118- 'name ' => $ attribute ,
120+ 'name ' => $ attribute ,
119121 'description ' => $ rule ,
120- 'in ' => 'query ' ,
121- 'style ' => 'form ' ,
122- 'required ' => str_contains ($ rule , 'required ' ),
123- 'schema ' => [
122+ 'in ' => 'query ' ,
123+ 'style ' => 'form ' ,
124+ 'required ' => str_contains ($ rule , 'required ' ),
125+ 'schema ' => [
124126 'type ' => $ this ->getAttributeType ($ rule ),
125127 ],
126128 ];
@@ -134,12 +136,12 @@ protected function makePathParameterItem(string $attribute, $rule): array
134136 }
135137
136138 $ parameter = [
137- 'name ' => $ attribute ,
139+ 'name ' => $ attribute ,
138140 'description ' => $ rule ,
139- 'in ' => 'path ' ,
140- 'style ' => 'simple ' ,
141- 'required ' => str_contains ($ rule , 'required ' ),
142- 'schema ' => [
141+ 'in ' => 'path ' ,
142+ 'style ' => 'simple ' ,
143+ 'required ' => str_contains ($ rule , 'required ' ),
144+ 'schema ' => [
143145 'type ' => $ this ->getAttributeType ($ rule ),
144146 ],
145147 ];
@@ -150,10 +152,10 @@ protected function makeRequestBodyItem(string $contentType): array
150152 {
151153 $ requestBody = [
152154 'description ' => "Request body " ,
153- 'content ' => [
155+ 'content ' => [
154156 $ contentType => [
155157 'schema ' => [
156- 'type ' => 'object ' ,
158+ 'type ' => 'object ' ,
157159 'properties ' => [],
158160 ],
159161 ],
@@ -167,9 +169,9 @@ protected function makeRequestBodyContentPropertyItem(string $rule): array
167169 $ type = $ this ->getAttributeType ($ rule );
168170
169171 return [
170- 'type ' => $ type ,
172+ 'type ' => $ type ,
171173 'nullable ' => str_contains ($ rule , 'nullable ' ),
172- 'format ' => $ this ->attributeIsFile ($ rule ) ? 'binary ' : $ type ,
174+ 'format ' => $ this ->attributeIsFile ($ rule ) ? 'binary ' : $ type ,
173175 ];
174176 }
175177
@@ -190,6 +192,68 @@ protected function getAttributeType(string $rule): string
190192 return "object " ;
191193 }
192194
195+ protected function appendGlobalSecurityScheme (): void
196+ {
197+ $ securityType = config ('request-docs.open_api.security.type ' );
198+
199+ if ($ securityType == null ) {
200+ return ;
201+ }
202+
203+ switch ($ securityType ) {
204+ case 'bearer ' :
205+ $ this ->openApi ['components ' ]['securitySchemes ' ]['bearerAuth ' ] = [
206+ 'type ' => 'http ' ,
207+ 'name ' => config ('request-docs.open_api.security.name ' , 'Bearer Token ' ),
208+ 'description ' => 'Http Bearer Authorization Token ' ,
209+ 'scheme ' => 'bearer '
210+ ];
211+ $ this ->openApi ['security ' ][] = [
212+ 'bearerAuth ' => []
213+ ];
214+ break ;
215+
216+ case 'basic ' :
217+ $ this ->openApi ['components ' ]['securitySchemes ' ]['basicAuth ' ] = [
218+ 'type ' => 'http ' ,
219+ 'name ' => config ('request-docs.open_api.security.name ' , 'Basic Username and Password ' ),
220+ 'description ' => 'Http Basic Authorization Username and Password ' ,
221+ 'scheme ' => 'basic '
222+ ];
223+ $ this ->openApi ['security ' ][] = [
224+ 'basicAuth ' => []
225+ ];
226+ break ;
227+
228+ case 'apikey ' :
229+ $ this ->openApi ['components ' ]['securitySchemes ' ]['apiKeyAuth ' ] = [
230+ 'type ' => 'apiKey ' ,
231+ 'name ' => config ('request-docs.open_api.security.name ' , 'api_key ' ),
232+ 'in ' => config ('request-docs.open_api.security.position ' , 'header ' ),
233+ 'description ' => config ('app.name ' ).' Provided Authorization Api Key ' ,
234+ ];
235+ $ this ->openApi ['security ' ][] = ['apiKeyAuth ' => []];
236+ break ;
237+
238+ case 'jwt ' :
239+ $ this ->openApi ['components ' ]['securitySchemes ' ]['bearerAuth ' ] = [
240+ 'type ' => 'http ' ,
241+ 'scheme ' => 'bearer ' ,
242+ 'name ' => config ('request-docs.open_api.security.name ' , 'Bearer JWT Token ' ),
243+ 'in ' => config ('request-docs.open_api.security.position ' , 'header ' ),
244+ 'description ' => 'JSON Web Token ' ,
245+ 'bearerFormat ' => 'JWT '
246+ ];
247+ $ this ->openApi ['security ' ][] = [
248+ 'bearerAuth ' => []
249+ ];
250+ break ;
251+
252+ default :
253+ break ;
254+ }
255+ }
256+
193257 /**
194258 * @codeCoverageIgnore
195259 */
0 commit comments