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/src/Generators/ResourcesGenerator.php b/src/Generators/ResourcesGenerator.php index d6f67e1..c6ad03a 100644 --- a/src/Generators/ResourcesGenerator.php +++ b/src/Generators/ResourcesGenerator.php @@ -67,14 +67,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 e369e41..df3e159 100644 --- a/tests/GenerateServerTest.php +++ b/tests/GenerateServerTest.php @@ -65,6 +65,9 @@ $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'), + $this->makeFilePath('/app/Http/Controllers/Controller11.php'), $this->makeFilePath('/app/Http/Controllers/Controller2.php'), $this->makeFilePath('/app/Http/Controllers/FooItemsController.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 78d0ab2..b66089a 100644 --- a/tests/resources/index.yaml +++ b/tests/resources/index.yaml @@ -4,6 +4,17 @@ info: version: 1.0.0 description: Тестовый конфиг paths: + /resources:test-rename-from-key-request: + post: + operationId: testRenameFromKeyRequest + x-lg-handler: '\App\Http\Controllers\ResourcesController@testRenameFromKeyRequest' + responses: + "200": + description: Успешный ответ c контекстом + content: + application/json: + schema: + $ref: './schemas/test_resource_generation.yaml#/ResourceDataWithNameResponse' /resources:test-full-generate/{id}: post: operationId: testFullGenerate diff --git a/tests/resources/schemas/test_resource_generation.yaml b/tests/resources/schemas/test_resource_generation.yaml index 2dab254..14c0e4b 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: @@ -47,6 +54,15 @@ ResourceWithDirResponse: 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