Skip to content

Commit 7a24470

Browse files
authored
Merge pull request #27 from ensi-platform/task-106821
#106821 Fetch x-lg-resource-class-name from final object
2 parents 2a5cdfc + 8ae6290 commit 7a24470

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Resource properties are generated relative to field in response, which can be se
113113
You can also specify `response_key` for resource: add `x-lg-resource-response-key: data` in object.
114114
When specifying `response_key`, you can use the "dot" syntax to specify nesting, for example `data.field`
115115
You can exclude resource generation using `x-lg-skip-resource-generation: true` in route.
116-
You can rename resource Class using `x-lg-resource-class-name: FooResource` in object.
116+
You can rename resource Class using `x-lg-resource-class-name: FooResource` in resource object or properties object.
117117
If a resource file already exists it is NOT overridden.
118118
Resource file contains a set of fields according to the specification.
119119
You also need to specify mixin DocBlock to autocomplete resource.

src/Generators/ResourcesGenerator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,19 @@ protected function extractResources(SpecObjectInterface $specObject): array
6767
$responseKeyParts = explode('.', $responseKey);
6868
foreach ($responseKeyParts as $responseKeyPart) {
6969
$flag = false;
70-
do_with_all_of($responseData, function (stdClass $p) use (&$responseData, &$flag, $responseKeyPart) {
70+
do_with_all_of($responseData, function (stdClass $p) use (&$responseData, &$flag, $responseKeyPart, &$className) {
7171
if (std_object_has($p, 'properties')) {
7272
if (std_object_has($p->properties, $responseKeyPart)) {
7373
$responseData = $p->properties->$responseKeyPart;
7474
$flag = true;
75+
76+
if (std_object_has($p->properties->$responseKeyPart, 'x-lg-resource-class-name')) {
77+
$className = $p->properties->$responseKeyPart->{'x-lg-resource-class-name'};
78+
}
7579
}
7680
}
7781
});
82+
7883
if (!$flag) {
7984
$responseData = null;
8085

tests/GenerateServerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
$this->makeFilePath('/app/Http/Resources/Foo/WithDirResource.php'),
6666
$this->makeFilePath('/app/Http/Tests/Foo/TestComponentTest.php'),
6767

68+
$this->makeFilePath('/app/Http/Requests/TestRenameFromKeyRequestRequest.php'),
69+
$this->makeFilePath('/app/Http/Resources/ResourcesDataWithNameResource.php'),
70+
6871
$this->makeFilePath('/app/Http/Controllers/Controller11.php'),
6972
$this->makeFilePath('/app/Http/Controllers/Controller2.php'),
7073
$this->makeFilePath('/app/Http/Controllers/FooItemsController.php'),

tests/ResourceGenerationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@
4343

4444
assertEqualsCanonicalizing(['foo', 'bar'], $resources['ResourcesResource.php']);
4545
assertEqualsCanonicalizing(['foo', 'bar'], $resources['ResourcesDataDataResource.php']);
46+
assertEqualsCanonicalizing(['foo', 'bar'], $resources['ResourcesDataWithNameResource.php']);
4647
assertEqualsCanonicalizing(['data'], $resources['ResourceRootResource.php']);
4748
});

tests/resources/index.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ info:
44
version: 1.0.0
55
description: Тестовый конфиг
66
paths:
7+
/resources:test-rename-from-key-request:
8+
post:
9+
operationId: testRenameFromKeyRequest
10+
x-lg-handler: '\App\Http\Controllers\ResourcesController@testRenameFromKeyRequest'
11+
responses:
12+
"200":
13+
description: Успешный ответ c контекстом
14+
content:
15+
application/json:
16+
schema:
17+
$ref: './schemas/test_resource_generation.yaml#/ResourceDataWithNameResponse'
718
/resources:test-full-generate/{id}:
819
post:
920
operationId: testFullGenerate

tests/resources/schemas/test_resource_generation.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ ResourceForTestResourceGeneration:
44
- $ref: '#/ResourceFillableProperties'
55
- $ref: '#/ResourceRequired'
66

7+
ResourceForTestResourceWithNameGeneration:
8+
x-lg-resource-class-name: ResourcesDataWithNameResource
9+
allOf:
10+
- $ref: '#/ResourceReadOnlyProperties'
11+
- $ref: '#/ResourceFillableProperties'
12+
- $ref: '#/ResourceRequired'
13+
714
ResourceReadOnlyProperties:
815
type: object
916
properties:
@@ -47,6 +54,15 @@ ResourceWithDirResponse:
4754
data:
4855
$ref: '#/ResourceForTestResourceGeneration'
4956

57+
ResourceDataWithNameResponse:
58+
type: object
59+
x-lg-resource-response-key: data.test
60+
properties:
61+
data:
62+
properties:
63+
test:
64+
$ref: '#/ResourceForTestResourceWithNameGeneration'
65+
5066
ResourceRootResponse:
5167
type: object
5268
x-lg-resource-response-key: false

0 commit comments

Comments
 (0)