@@ -7,7 +7,7 @@ class LaravelRequestDocsToOpenApi
7
7
private array $ openApi = [];
8
8
9
9
/**
10
- * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
10
+ * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
11
11
* @return $this
12
12
*/
13
13
public function openApi (array $ docs ): LaravelRequestDocsToOpenApi
@@ -23,18 +23,19 @@ public function openApi(array $docs): LaravelRequestDocsToOpenApi
23
23
];
24
24
25
25
$ this ->docsToOpenApi ($ docs );
26
+ $ this ->appendGlobalSecurityScheme ();
26
27
return $ this ;
27
28
}
28
29
29
30
/**
30
- * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
31
+ * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
31
32
* @return void
32
33
*/
33
34
private function docsToOpenApi (array $ docs ): void
34
35
{
35
36
$ this ->openApi ['paths ' ] = [];
36
37
$ 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 ' , []));
38
39
39
40
foreach ($ docs as $ doc ) {
40
41
$ httpMethod = strtolower ($ doc ->getHttpMethod ());
@@ -90,6 +91,7 @@ private function docsToOpenApi(array $docs): void
90
91
}
91
92
}
92
93
}
94
+
93
95
protected function setAndFilterResponses (Doc $ doc ): array
94
96
{
95
97
$ docResponses = $ doc ->getResponses ();
@@ -115,12 +117,12 @@ protected function makeQueryParameterItem(string $attribute, $rule): array
115
117
$ rule = implode ('| ' , $ rule );
116
118
}
117
119
$ parameter = [
118
- 'name ' => $ attribute ,
120
+ 'name ' => $ attribute ,
119
121
'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 ' => [
124
126
'type ' => $ this ->getAttributeType ($ rule ),
125
127
],
126
128
];
@@ -134,12 +136,12 @@ protected function makePathParameterItem(string $attribute, $rule): array
134
136
}
135
137
136
138
$ parameter = [
137
- 'name ' => $ attribute ,
139
+ 'name ' => $ attribute ,
138
140
'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 ' => [
143
145
'type ' => $ this ->getAttributeType ($ rule ),
144
146
],
145
147
];
@@ -150,10 +152,10 @@ protected function makeRequestBodyItem(string $contentType): array
150
152
{
151
153
$ requestBody = [
152
154
'description ' => "Request body " ,
153
- 'content ' => [
155
+ 'content ' => [
154
156
$ contentType => [
155
157
'schema ' => [
156
- 'type ' => 'object ' ,
158
+ 'type ' => 'object ' ,
157
159
'properties ' => [],
158
160
],
159
161
],
@@ -167,9 +169,9 @@ protected function makeRequestBodyContentPropertyItem(string $rule): array
167
169
$ type = $ this ->getAttributeType ($ rule );
168
170
169
171
return [
170
- 'type ' => $ type ,
172
+ 'type ' => $ type ,
171
173
'nullable ' => str_contains ($ rule , 'nullable ' ),
172
- 'format ' => $ this ->attributeIsFile ($ rule ) ? 'binary ' : $ type ,
174
+ 'format ' => $ this ->attributeIsFile ($ rule ) ? 'binary ' : $ type ,
173
175
];
174
176
}
175
177
@@ -190,6 +192,68 @@ protected function getAttributeType(string $rule): string
190
192
return "object " ;
191
193
}
192
194
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
+
193
257
/**
194
258
* @codeCoverageIgnore
195
259
*/
0 commit comments