Skip to content

Commit 8d6f8e8

Browse files
fix: call form request rules method with laravel container (#34)
1 parent ccf986d commit 8d6f8e8

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@ parameters:
114114
count: 1
115115
path: src/Generator/Mapping/Schema.php
116116

117-
-
118-
message: '#^Method Intermax\\LaravelOpenApi\\Generator\\OperationCreator\:\:create\(\) has parameter \$responses with no value type specified in iterable type cebe\\openapi\\spec\\Responses\.$#'
119-
identifier: missingType.iterableValue
120-
count: 1
121-
path: src/Generator/OperationCreator.php
122-
123117
-
124118
message: '#^PHPDoc tag @param for parameter \$responses with type \(cebe\\openapi\\spec\\Responses&iterable\<cebe\\openapi\\spec\\Response\>\)\|null is not subtype of native type cebe\\openapi\\spec\\Responses\.$#'
125119
identifier: parameter.phpDocType

src/Generator/RequestBodyCreator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
use cebe\openapi\spec\RequestBody;
66
use cebe\openapi\spec\Schema;
77
use Illuminate\Contracts\Config\Repository;
8+
use Illuminate\Foundation\Application;
89
use Illuminate\Foundation\Http\FormRequest;
910
use Illuminate\Validation\Rule;
1011
use Intermax\LaravelOpenApi\Contracts\HasQueryParameters;
1112
use UnhandledMatchError;
1213

1314
class RequestBodyCreator
1415
{
15-
public function __construct(private readonly Repository $config, private ComponentManager $componentManager) {}
16+
public function __construct(
17+
private readonly Repository $config,
18+
private ComponentManager $componentManager,
19+
private Application $app,
20+
) {}
1621

1722
public function create(FormRequest $request, ?string $entityName = null): ?RequestBody
1823
{
@@ -159,7 +164,7 @@ protected function rulesWithoutQueryParameters(FormRequest $request): array
159164
assert(method_exists($request, 'rules'));
160165

161166
return array_filter(
162-
array: $request->rules(),
167+
array: $this->app->call($request->rules(...)), // @phpstan-ignore-line
163168
callback: fn ($name) => ! in_array($name, $queryParameters),
164169
mode: ARRAY_FILTER_USE_KEY
165170
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Intermax\LaravelOpenApi\Tests\QueryParameters\Utilities;
6+
7+
class Dummy {}

tests/QueryParameters/Utilities/ThingCollectionRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function queryParameters(): array
2929
];
3030
}
3131

32-
public function rules(): array
32+
public function rules(Dummy $dummy): array
3333
{
3434
return ['filter.test' => 'string'];
3535
}

tests/RequestBody/RequestBodyCreatorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,15 @@ public function it_omits_query_parameters_from_request_body()
1919

2020
$this->assertNull($output);
2121
}
22+
23+
#[Test]
24+
public function it_does_not_fail_with_exception_when_rules_method_requires_additional_arguments()
25+
{
26+
/** @var RequestBodyCreator $creator */
27+
$creator = $this->app->make(RequestBodyCreator::class);
28+
29+
$output = $creator->create($this->app->make(ThingCollectionRequest::class));
30+
31+
$this->assertNull($output);
32+
}
2233
}

0 commit comments

Comments
 (0)