Skip to content

Commit ea3c336

Browse files
committed
Fix http method filter
1 parent 65711dd commit ea3c336

File tree

2 files changed

+111
-84
lines changed

2 files changed

+111
-84
lines changed

src/LaravelRequestDocs.php

Lines changed: 10 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function getDocs(
2929
bool $showDelete,
3030
bool $showHead
3131
): array {
32-
$methods = array_filter([
32+
$filteredMethods = array_filter([
3333
Request::METHOD_GET => $showGet,
3434
Request::METHOD_POST => $showPost,
3535
Request::METHOD_PUT => $showPut,
@@ -40,7 +40,10 @@ public function getDocs(
4040
return $method;
4141
});
4242

43-
$docs = $this->getControllersInfo(array_keys($methods));
43+
/** @var string[] $methods */
44+
$methods = array_keys($filteredMethods);
45+
46+
$docs = $this->getControllersInfo($methods);
4447
$docs = $this->appendRequestRules($docs);
4548

4649
return array_filter($docs);
@@ -58,14 +61,10 @@ public function splitByMethods(array $docs): array
5861
$splitDocs = [];
5962

6063
foreach ($docs as $doc) {
61-
if (count($doc->getMethods()) === 1) {
62-
$splitDocs[] = $doc;
63-
continue;
64-
}
65-
6664
foreach ($doc->getMethods() as $method) {
6765
$cloned = $doc->clone();
6866
$cloned->setMethods([$method]);
67+
$cloned->setHttpMethod($method);
6968
$splitDocs[] = $cloned;
7069
}
7170
}
@@ -106,65 +105,6 @@ public function sortDocs(array $docs, ?string $sortBy = 'default'): array
106105
return $sorted->values()->all();
107106
}
108107

109-
/**
110-
* @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
111-
* @return \Rakutentech\LaravelRequestDocs\Doc[]
112-
*/
113-
public function filterByMethods(array $docs, bool $get, bool $post, bool $put, bool $patch, bool $delete, bool $head): array
114-
{
115-
$filtered = [];
116-
117-
foreach ($docs as $doc) {
118-
if ($get && in_array('GET', $doc->getMethods())) {
119-
$clone = clone $doc;
120-
$clone->setMethods(['GET']);
121-
$filtered[] = $clone;
122-
}
123-
}
124-
125-
foreach ($docs as $doc) {
126-
if ($post && in_array('POST', $doc->getMethods())) {
127-
$clone = clone $doc;
128-
$clone->setMethods(['POST']);
129-
$filtered[] = $clone;
130-
}
131-
}
132-
133-
foreach ($docs as $doc) {
134-
if ($put && in_array('PUT', $doc->getMethods())) {
135-
$clone = clone $doc;
136-
$clone->setMethods(['PUT']);
137-
$filtered[] = $clone;
138-
}
139-
}
140-
141-
foreach ($docs as $doc) {
142-
if ($patch && in_array('PATCH', $doc->getMethods())) {
143-
$clone = clone $doc;
144-
$clone->setMethods(['PATCH']);
145-
$filtered[] = $clone;
146-
}
147-
}
148-
149-
foreach ($docs as $doc) {
150-
if ($delete && in_array('DELETE', $doc->getMethods())) {
151-
$clone = clone $doc;
152-
$clone->setMethods(['DELETE']);
153-
$filtered[] = $clone;
154-
}
155-
}
156-
157-
foreach ($docs as $doc) {
158-
if ($head && in_array('HEAD', $doc->getMethods())) {
159-
$clone = clone $doc;
160-
$clone->setMethods(['HEAD']);
161-
$filtered[] = $clone;
162-
}
163-
}
164-
165-
return $filtered;
166-
}
167-
168108
/**
169109
* @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
170110
* @return \Rakutentech\LaravelRequestDocs\Doc[]
@@ -216,15 +156,12 @@ public function getControllersInfo(array $onlyMethods): array
216156
}
217157
}
218158

219-
$httpMethod = $route->methods[0];
159+
$routeMethods = array_intersect($route->methods, $onlyMethods);
220160

221-
if (!in_array($httpMethod, $onlyMethods)) {
161+
if (empty($routeMethods)) {
222162
continue;
223163
}
224164

225-
// Exclude from `$route->methods` which is not in `$onlyMethods`.
226-
$routeMethods = array_intersect($route->methods, $onlyMethods);
227-
228165
$controllerName = '';
229166
$controllerFullPath = '';
230167
$method = '';
@@ -237,21 +174,14 @@ public function getControllersInfo(array $onlyMethods): array
237174
$controllerName = (new ReflectionClass($controllerFullPath))->getShortName();
238175
}
239176

240-
foreach ($docs as $doc) {
241-
if ($doc->getUri() === $route->uri && $doc->getHttpMethod() === $httpMethod) {
242-
// is duplicate
243-
continue 2;
244-
}
245-
}
246-
247177
$doc = new Doc(
248178
$route->uri,
249179
$routeMethods,
250180
config('request-docs.hide_meta_data') ? [] : $route->middleware(),
251181
config('request-docs.hide_meta_data') ? '' : $controllerName,
252182
config('request-docs.hide_meta_data') ? '' : $controllerFullPath,
253183
config('request-docs.hide_meta_data') ? '' : $method,
254-
$httpMethod,
184+
'',
255185
[],
256186
'',
257187
);
@@ -303,10 +233,6 @@ public function appendRequestRules(array $controllersInfo): array
303233
$controllerInfo->mergeRules($this->flattenRules($requestObject->$requestMethod()));
304234
} catch (Throwable $e) {
305235
$controllerInfo->mergeRules($this->rulesByRegex($requestClassName, $requestMethod));
306-
307-
if (config('request-docs.debug')) {
308-
throw $e;
309-
}
310236
}
311237
}
312238
} catch (Throwable $th) {
@@ -389,7 +315,7 @@ public function rulesByRegex(string $requestClassName, string $methodName): arra
389315
// check if line is a comment
390316
$trimmed = trim($lines[$i]);
391317
if (Str::startsWith($trimmed, '//') || Str::startsWith($trimmed, '#')) {
392-
continue;
318+
continue; // @codeCoverageIgnore
393319
}
394320
// check if => in string, only pick up rules that are coded on single line
395321
if (Str::contains($lines[$i], '=>')) {
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace Rakutentech\LaravelRequestDocs\Tests\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Response;
7+
use Illuminate\Support\Facades\Config;
8+
use Rakutentech\LaravelRequestDocs\Tests\TestCase;
9+
10+
class LaravelRequestDocsControllerTest extends TestCase
11+
{
12+
public function testApi()
13+
{
14+
$this->get(route('request-docs.api'))
15+
->assertStatus(Response::HTTP_OK);
16+
}
17+
18+
public function testAbleFetchAllMethods()
19+
{
20+
$response = $this->get(route('request-docs.api'))
21+
->assertStatus(Response::HTTP_OK);
22+
23+
$docs = collect($response->json());
24+
25+
$this->assertSame([
26+
Request::METHOD_DELETE,
27+
Request::METHOD_GET,
28+
Request::METHOD_HEAD,
29+
Request::METHOD_PATCH,
30+
Request::METHOD_POST,
31+
Request::METHOD_PUT,
32+
],
33+
$docs->pluck('http_method')
34+
->flatten()
35+
->unique()
36+
->sort()
37+
->values()
38+
->toArray()
39+
);
40+
}
41+
42+
public function testAbleFilterMethod()
43+
{
44+
$methodMap = [
45+
'showDelete' => Request::METHOD_DELETE,
46+
'showGet' => Request::METHOD_GET,
47+
'showHead' => Request::METHOD_HEAD,
48+
'showPatch' => Request::METHOD_PATCH,
49+
'showPost' => Request::METHOD_POST,
50+
'showPut' => Request::METHOD_PUT,
51+
];
52+
53+
foreach ($methodMap as $request => $method) {
54+
$response = $this->get(route('request-docs.api') . '?' . $request . '=false')
55+
->assertStatus(Response::HTTP_OK);
56+
57+
$docs = collect($response->json());
58+
59+
$expected = array_filter([
60+
Request::METHOD_DELETE,
61+
Request::METHOD_GET,
62+
Request::METHOD_HEAD,
63+
Request::METHOD_PATCH,
64+
Request::METHOD_POST,
65+
Request::METHOD_PUT,
66+
], fn($expectedMethod) => $expectedMethod !== $method);
67+
68+
$expected = array_values($expected);
69+
70+
$this->assertSame(
71+
$expected,
72+
$docs->pluck('http_method')
73+
->flatten()
74+
->unique()
75+
->sort()
76+
->values()
77+
->toArray()
78+
);
79+
}
80+
}
81+
82+
public function testOnlyRouteURIStartWith()
83+
{
84+
Config::set('request-docs.only_route_uri_start_with', 'welcome');
85+
86+
$response = $this->get(route('request-docs.api'))
87+
->assertStatus(Response::HTTP_OK);
88+
89+
$docs = collect($response->json());
90+
91+
foreach ($docs as $doc) {
92+
$this->assertStringStartsWith('welcome', $doc['uri']);
93+
}
94+
}
95+
96+
public function testOpenApi()
97+
{
98+
$response = $this->get(route('request-docs.api') . '?openapi=true')
99+
->assertStatus(Response::HTTP_OK);
100+
}
101+
}

0 commit comments

Comments
 (0)