Skip to content

Commit ffc3849

Browse files
committed
#101098 Request class name with dir
1 parent 263a643 commit ffc3849

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

src/Generators/BaseGenerator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,17 @@ private function formattedGlobalParams(): array
112112
return $params;
113113
}
114114

115-
protected function getActualClassNameAndNamespace(string $className, string $namespace): array
115+
protected function getActualClassNameAndNamespace(?string $className, ?string $namespace): array
116116
{
117117
$parseClassName = explode('/', $className);
118118
if (count($parseClassName) > 1) {
119+
120+
if (str_contains($namespace, '\Request')) {
121+
$namespace = substr($namespace, 0, strpos($namespace, '\Request') + 8);
122+
} elseif (str_contains($namespace, '\Resources')) {
123+
$namespace = substr($namespace, 0, strpos($namespace, '\Resources') + 10);
124+
}
125+
119126
$className = array_pop($parseClassName);
120127
$namespace .= '\\' . implode('\\', $parseClassName);
121128
}

src/Generators/ResourcesGenerator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ protected function extractResources(SpecObjectInterface $specObject): array
5252
continue;
5353
}
5454

55+
list($className, $namespace) = $this->getActualClassNameAndNamespace($className, $namespace);
56+
5557
if (isset($resources["$namespace\\$className"])) {
5658
continue;
5759
}

tests/GenerateServerTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@
3737
assertEqualsCanonicalizing([
3838
$this->makeFilePath('/app/Http/ApiV1/OpenApiGenerated/routes.php'),
3939

40+
$this->makeFilePath('/app/Http/Controllers/Foo/TestController.php'),
4041
$this->makeFilePath('/app/Http/Controllers/ResourcesController.php'),
4142
$this->makeFilePath('/app/Http/Requests/TestFullGenerateRequest.php'),
4243
$this->makeFilePath('/app/Http/Tests/ResourcesComponentTest.php'),
4344
$this->makeFilePath('/app/Http/Requests/TestFooRenameRequest.php'),
4445

46+
$this->makeFilePath('/app/Http/Request/WithDirRequests/Request.php'),
47+
$this->makeFilePath('/app/Http/Requests/Foo/TestNamespaceWithDirRequest.php'),
4548
$this->makeFilePath('/app/Http/Requests/LaravelValidationsApplicationJsonRequest.php'),
4649
$this->makeFilePath('/app/Http/Requests/LaravelValidationsMultipartFormDataRequest.php'),
4750
$this->makeFilePath('/app/Http/Requests/LaravelValidationsNonAvailableContentTypeRequest.php'),
@@ -57,7 +60,10 @@
5760

5861
$this->makeFilePath('/app/Http/Resources/ResourcesResource.php'),
5962
$this->makeFilePath('/app/Http/Resources/ResourcesDataDataResource.php'),
63+
$this->makeFilePath('/app/Http/Resources/Foo/ResourcesDataDataResource.php'),
6064
$this->makeFilePath('/app/Http/Resources/ResourceRootResource.php'),
65+
$this->makeFilePath('/app/Http/Resources/Foo/WithDirResource.php'),
66+
$this->makeFilePath('/app/Http/Tests/Foo/TestComponentTest.php'),
6167

6268
$this->makeFilePath('/app/Http/Controllers/PoliciesController.php'),
6369
$this->makeFilePath('/app/Http/Tests/PoliciesComponentTest.php'),

tests/resources/index.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,29 @@ paths:
147147
application/json:
148148
schema:
149149
$ref: './schemas/test_resource_generation.yaml#/GenerateResourceWithoutPropertiesResponse'
150+
/resources:test-class-name-with-dir:
151+
post:
152+
operationId: testNamespaceWithDir
153+
x-lg-handler: '\App\Http\Controllers\Foo\TestController@testNamespaceWithDirRequest'
154+
x-lg-request-class-name: 'WithDirRequests/Request'
155+
responses:
156+
"200":
157+
description: Успешный ответ c контекстом
158+
content:
159+
application/json:
160+
schema:
161+
$ref: './schemas/test_resource_generation.yaml#/ResourceDataDataResponse'
162+
/resources:test-resource-class-name-with-dir:
163+
post:
164+
operationId: testNamespaceWithDir
165+
x-lg-handler: '\App\Http\Controllers\Foo\TestController@testNamespaceWithDirResource'
166+
responses:
167+
"200":
168+
description: Успешный ответ c контекстом
169+
content:
170+
application/json:
171+
schema:
172+
$ref: './schemas/test_resource_generation.yaml#/ResourceWithDirResponse'
150173
/policies:test-generate-policy-method-foo:
151174
post:
152175
operationId: generatePolicyMethodFoo

tests/resources/schemas/test_resource_generation.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ ResourceDataDataResponse:
3737
data:
3838
$ref: '#/ResourceForTestResourceGeneration'
3939

40+
ResourceWithDirResponse:
41+
type: object
42+
x-lg-resource-response-key: data.data
43+
x-lg-resource-class-name: Foo/WithDirResource
44+
properties:
45+
data:
46+
properties:
47+
data:
48+
$ref: '#/ResourceForTestResourceGeneration'
49+
4050
ResourceRootResponse:
4151
type: object
4252
x-lg-resource-response-key: false

0 commit comments

Comments
 (0)