Skip to content

Commit

Permalink
Merge branch 'master' into task-106821
Browse files Browse the repository at this point in the history
# Conflicts:
#	tests/resources/schemas/test_resource_generation.yaml
  • Loading branch information
valerialukinykh committed Jul 4, 2023
2 parents 5a56809 + 2a5cdfc commit 8ae6290
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/Generators/BaseGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,25 @@ private function formattedGlobalParams(): array

return $params;
}

protected function getActualClassNameAndNamespace(?string $className, ?string $namespace): array
{
$parseClassName = explode('/', $className);

if (count($parseClassName) > 1) {
if (str_contains($namespace, '\Requests')) {
$namespace = substr($namespace, 0, strpos($namespace, '\Requests') + 9);
} elseif (str_contains($namespace, '\Resources')) {
$namespace = substr($namespace, 0, strpos($namespace, '\Resources') + 10);
}

$className = array_pop($parseClassName);
$namespace .= '\\' . implode('\\', $parseClassName);
}

return [
$className,
$namespace,
];
}
}
7 changes: 6 additions & 1 deletion src/Generators/ControllersGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ private function extractControllers(SpecObjectInterface $specObject): array

if ($methodWithRequest && empty($route->{'x-lg-skip-request-generation'})) {
$requestClassName = $route->{'x-lg-request-class-name'} ?? ucfirst($route->operationId) . 'Request';
$controllers[$fqcn]['requestsNamespaces'][] = $this->getReplacedNamespace($handler->namespace, 'Controllers', 'Requests') . '\\' . ucfirst($requestClassName);
$requestNamespace = $this->getReplacedNamespace($handler->namespace, 'Controllers', 'Requests');

list($requestClassName, $requestNamespace) = $this->getActualClassNameAndNamespace($requestClassName, $requestNamespace);
$requestNamespace .= '\\' . ucfirst($requestClassName);

$controllers[$fqcn]['requestsNamespaces'][] = $requestNamespace;
} elseif ($methodWithRequest) {
$controllers[$fqcn]['requestsNamespaces'][] = 'Illuminate\Http\Request';
}
Expand Down
2 changes: 2 additions & 0 deletions src/Generators/RequestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ protected function extractRequests(SpecObjectInterface $specObject, array $names
continue;
}

list($className, $newNamespace) = $this->getActualClassNameAndNamespace($className, $newNamespace);

$validationRules = '//';
$usesEnums = '';
if (std_object_has($route, 'requestBody')) {
Expand Down
2 changes: 2 additions & 0 deletions src/Generators/ResourcesGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ protected function extractResources(SpecObjectInterface $specObject): array
continue;
}

list($className, $namespace) = $this->getActualClassNameAndNamespace($className, $namespace);

if (isset($resources["$namespace\\$className"])) {
continue;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/GenerateServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@
assertEqualsCanonicalizing([
$this->makeFilePath('/app/Http/ApiV1/OpenApiGenerated/routes.php'),

$this->makeFilePath('/app/Http/Controllers/Foo/TestController.php'),
$this->makeFilePath('/app/Http/Controllers/ResourcesController.php'),
$this->makeFilePath('/app/Http/Requests/TestFullGenerateRequest.php'),
$this->makeFilePath('/app/Http/Tests/ResourcesComponentTest.php'),
$this->makeFilePath('/app/Http/Requests/TestFooRenameRequest.php'),

$this->makeFilePath('/app/Http/Requests/WithDirRequests/Request.php'),
$this->makeFilePath('/app/Http/Requests/Foo/TestNamespaceWithDirRequest.php'),
$this->makeFilePath('/app/Http/Requests/LaravelValidationsApplicationJsonRequest.php'),
$this->makeFilePath('/app/Http/Requests/LaravelValidationsMultipartFormDataRequest.php'),
$this->makeFilePath('/app/Http/Requests/LaravelValidationsNonAvailableContentTypeRequest.php'),
Expand All @@ -57,7 +60,10 @@

$this->makeFilePath('/app/Http/Resources/ResourcesResource.php'),
$this->makeFilePath('/app/Http/Resources/ResourcesDataDataResource.php'),
$this->makeFilePath('/app/Http/Resources/Foo/ResourcesDataDataResource.php'),
$this->makeFilePath('/app/Http/Resources/ResourceRootResource.php'),
$this->makeFilePath('/app/Http/Resources/Foo/WithDirResource.php'),
$this->makeFilePath('/app/Http/Tests/Foo/TestComponentTest.php'),

$this->makeFilePath('/app/Http/Requests/TestRenameFromKeyRequestRequest.php'),
$this->makeFilePath('/app/Http/Resources/ResourcesDataWithNameResource.php'),
Expand Down Expand Up @@ -158,6 +164,7 @@
assertStringContainsString(
"use App\Http\Controllers\Controller11;\n".
"use App\Http\Controllers\Controller2;\n".
"use App\Http\Controllers\Foo\TestController;\n" .
"use App\Http\Controllers\FooItemsController;\n".
"use App\Http\Controllers\FoosController;\n",
$routes
Expand Down
23 changes: 23 additions & 0 deletions tests/resources/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,29 @@ paths:
application/json:
schema:
$ref: './schemas/test_resource_generation.yaml#/GenerateResourceWithoutPropertiesResponse'
/resources:test-class-name-with-dir:
post:
operationId: testNamespaceWithDir
x-lg-handler: '\App\Http\Controllers\Foo\TestController@testNamespaceWithDirRequest'
x-lg-request-class-name: 'WithDirRequests/Request'
responses:
"200":
description: Успешный ответ c контекстом
content:
application/json:
schema:
$ref: './schemas/test_resource_generation.yaml#/ResourceDataDataResponse'
/resources:test-resource-class-name-with-dir:
post:
operationId: testNamespaceWithDir
x-lg-handler: '\App\Http\Controllers\Foo\TestController@testNamespaceWithDirResource'
responses:
"200":
description: Успешный ответ c контекстом
content:
application/json:
schema:
$ref: './schemas/test_resource_generation.yaml#/ResourceWithDirResponse'
/policies:test-generate-policy-method-foo:
post:
operationId: generatePolicyMethodFoo
Expand Down
10 changes: 10 additions & 0 deletions tests/resources/schemas/test_resource_generation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ ResourceDataDataResponse:
data:
$ref: '#/ResourceForTestResourceGeneration'

ResourceWithDirResponse:
type: object
x-lg-resource-response-key: data.data
x-lg-resource-class-name: Foo/WithDirResource
properties:
data:
properties:
data:
$ref: '#/ResourceForTestResourceGeneration'

ResourceDataWithNameResponse:
type: object
x-lg-resource-response-key: data.test
Expand Down

0 comments on commit 8ae6290

Please sign in to comment.