From 21bff3e9d1c69ec84673ff42155c646d452546a4 Mon Sep 17 00:00:00 2001 From: "valeriya.lukinykh" Date: Fri, 28 Apr 2023 20:39:16 +0700 Subject: [PATCH 1/2] #106821 Fetch x-lg-resource-class-name from final object --- src/Generators/ResourcesGenerator.php | 7 +++- tests/GenerateServerTest.php | 1 + tests/ResourceGenerationTest.php | 1 + tests/resources/index.yaml | 36 ++++++++++++------- .../schemas/test_resource_generation.yaml | 16 +++++++++ 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/Generators/ResourcesGenerator.php b/src/Generators/ResourcesGenerator.php index dbebd94..44ba48e 100644 --- a/src/Generators/ResourcesGenerator.php +++ b/src/Generators/ResourcesGenerator.php @@ -65,14 +65,19 @@ protected function extractResources(SpecObjectInterface $specObject): array $responseKeyParts = explode('.', $responseKey); foreach ($responseKeyParts as $responseKeyPart) { $flag = false; - do_with_all_of($responseData, function (stdClass $p) use (&$responseData, &$flag, $responseKeyPart) { + do_with_all_of($responseData, function (stdClass $p) use (&$responseData, &$flag, $responseKeyPart, &$className) { if (std_object_has($p, 'properties')) { if (std_object_has($p->properties, $responseKeyPart)) { $responseData = $p->properties->$responseKeyPart; $flag = true; + + if (std_object_has($p->properties->$responseKeyPart, 'x-lg-resource-class-name')) { + $className = $p->properties->$responseKeyPart->{'x-lg-resource-class-name'}; + } } } }); + if (!$flag) { $responseData = null; diff --git a/tests/GenerateServerTest.php b/tests/GenerateServerTest.php index ef81d36..3d2f77e 100644 --- a/tests/GenerateServerTest.php +++ b/tests/GenerateServerTest.php @@ -57,6 +57,7 @@ $this->makeFilePath('/app/Http/Resources/ResourcesResource.php'), $this->makeFilePath('/app/Http/Resources/ResourcesDataDataResource.php'), + $this->makeFilePath('/app/Http/Resources/ResourcesDataWithNameResource.php'), $this->makeFilePath('/app/Http/Resources/ResourceRootResource.php'), $this->makeFilePath('/app/Http/Controllers/PoliciesController.php'), diff --git a/tests/ResourceGenerationTest.php b/tests/ResourceGenerationTest.php index 7c07543..dc58283 100644 --- a/tests/ResourceGenerationTest.php +++ b/tests/ResourceGenerationTest.php @@ -43,5 +43,6 @@ assertEqualsCanonicalizing(['foo', 'bar'], $resources['ResourcesResource.php']); assertEqualsCanonicalizing(['foo', 'bar'], $resources['ResourcesDataDataResource.php']); + assertEqualsCanonicalizing(['foo', 'bar'], $resources['ResourcesDataWithNameResource.php']); assertEqualsCanonicalizing(['data'], $resources['ResourceRootResource.php']); }); diff --git a/tests/resources/index.yaml b/tests/resources/index.yaml index a8d7687..5e76d40 100644 --- a/tests/resources/index.yaml +++ b/tests/resources/index.yaml @@ -4,6 +4,30 @@ info: version: 1.0.0 description: Тестовый конфиг paths: + /resources:test-empty-rename-request: + post: + operationId: testEmptyRenameRequest + x-lg-handler: '\App\Http\Controllers\ResourcesController@testEmptyRenameRequest' + x-lg-request-class-name: '' + responses: + "200": + description: Успешный ответ c контекстом + content: + application/json: + schema: + $ref: './schemas/test_resource_generation.yaml#/ResourceDataDataResponse' + /resources:test-rename-from-key-request: + post: + operationId: testRenameFromKeyRequest + x-lg-handler: '\App\Http\Controllers\ResourcesController@testRenameFromKeyRequest' + x-lg-request-class-name: '' + responses: + "200": + description: Успешный ответ c контекстом + content: + application/json: + schema: + $ref: './schemas/test_resource_generation.yaml#/ResourceDataWithNameResponse' /resources:test-full-generate/{id}: post: operationId: testFullGenerate @@ -22,18 +46,6 @@ paths: $ref: './schemas/test_resource_generation.yaml#/ResourceForTestResourceGenerationResponse' "500": $ref: '#/components/responses/ServerError' - /resources:test-empty-rename-request: - post: - operationId: testEmptyRenameRequest - x-lg-handler: '\App\Http\Controllers\ResourcesController@testEmptyRenameRequest' - x-lg-request-class-name: '' - responses: - "200": - description: Успешный ответ c контекстом - content: - application/json: - schema: - $ref: './schemas/test_resource_generation.yaml#/ResourceDataDataResponse' /resources:test-rename-request: post: operationId: testRenameRequest diff --git a/tests/resources/schemas/test_resource_generation.yaml b/tests/resources/schemas/test_resource_generation.yaml index 71dd30e..4a0d574 100644 --- a/tests/resources/schemas/test_resource_generation.yaml +++ b/tests/resources/schemas/test_resource_generation.yaml @@ -4,6 +4,13 @@ ResourceForTestResourceGeneration: - $ref: '#/ResourceFillableProperties' - $ref: '#/ResourceRequired' +ResourceForTestResourceWithNameGeneration: + x-lg-resource-class-name: ResourcesDataWithNameResource + allOf: + - $ref: '#/ResourceReadOnlyProperties' + - $ref: '#/ResourceFillableProperties' + - $ref: '#/ResourceRequired' + ResourceReadOnlyProperties: type: object properties: @@ -37,6 +44,15 @@ ResourceDataDataResponse: data: $ref: '#/ResourceForTestResourceGeneration' +ResourceDataWithNameResponse: + type: object + x-lg-resource-response-key: data.test + properties: + data: + properties: + test: + $ref: '#/ResourceForTestResourceWithNameGeneration' + ResourceRootResponse: type: object x-lg-resource-response-key: false From bfb55634302bb1d1a3f2656a6c601e84dca139f2 Mon Sep 17 00:00:00 2001 From: "valeriya.lukinykh" Date: Mon, 15 May 2023 21:52:18 +0700 Subject: [PATCH 2/2] #106821 Fixed test and added info to README --- README.md | 2 +- tests/GenerateServerTest.php | 4 +++- tests/resources/index.yaml | 25 ++++++++++++------------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index db4d02b..b2a99e0 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ Resource properties are generated relative to field in response, which can be se You can also specify `response_key` for resource: add `x-lg-resource-response-key: data` in object. When specifying `response_key`, you can use the "dot" syntax to specify nesting, for example `data.field` You can exclude resource generation using `x-lg-skip-resource-generation: true` in route. -You can rename resource Class using `x-lg-resource-class-name: FooResource` in object. +You can rename resource Class using `x-lg-resource-class-name: FooResource` in resource object or properties object. If a resource file already exists it is NOT overridden. Resource file contains a set of fields according to the specification. You also need to specify mixin DocBlock to autocomplete resource. diff --git a/tests/GenerateServerTest.php b/tests/GenerateServerTest.php index 3d2f77e..41d8cfa 100644 --- a/tests/GenerateServerTest.php +++ b/tests/GenerateServerTest.php @@ -57,9 +57,11 @@ $this->makeFilePath('/app/Http/Resources/ResourcesResource.php'), $this->makeFilePath('/app/Http/Resources/ResourcesDataDataResource.php'), - $this->makeFilePath('/app/Http/Resources/ResourcesDataWithNameResource.php'), $this->makeFilePath('/app/Http/Resources/ResourceRootResource.php'), + $this->makeFilePath('/app/Http/Requests/TestRenameFromKeyRequestRequest.php'), + $this->makeFilePath('/app/Http/Resources/ResourcesDataWithNameResource.php'), + $this->makeFilePath('/app/Http/Controllers/PoliciesController.php'), $this->makeFilePath('/app/Http/Tests/PoliciesComponentTest.php'), $this->makeFilePath('/app/Http/Policies/PoliciesControllerPolicy.php'), diff --git a/tests/resources/index.yaml b/tests/resources/index.yaml index 5e76d40..a47e217 100644 --- a/tests/resources/index.yaml +++ b/tests/resources/index.yaml @@ -4,23 +4,10 @@ info: version: 1.0.0 description: Тестовый конфиг paths: - /resources:test-empty-rename-request: - post: - operationId: testEmptyRenameRequest - x-lg-handler: '\App\Http\Controllers\ResourcesController@testEmptyRenameRequest' - x-lg-request-class-name: '' - responses: - "200": - description: Успешный ответ c контекстом - content: - application/json: - schema: - $ref: './schemas/test_resource_generation.yaml#/ResourceDataDataResponse' /resources:test-rename-from-key-request: post: operationId: testRenameFromKeyRequest x-lg-handler: '\App\Http\Controllers\ResourcesController@testRenameFromKeyRequest' - x-lg-request-class-name: '' responses: "200": description: Успешный ответ c контекстом @@ -46,6 +33,18 @@ paths: $ref: './schemas/test_resource_generation.yaml#/ResourceForTestResourceGenerationResponse' "500": $ref: '#/components/responses/ServerError' + /resources:test-empty-rename-request: + post: + operationId: testEmptyRenameRequest + x-lg-handler: '\App\Http\Controllers\ResourcesController@testEmptyRenameRequest' + x-lg-request-class-name: '' + responses: + "200": + description: Успешный ответ c контекстом + content: + application/json: + schema: + $ref: './schemas/test_resource_generation.yaml#/ResourceDataDataResponse' /resources:test-rename-request: post: operationId: testRenameRequest