Skip to content

Commit

Permalink
Merge pull request #27 from ensi-platform/task-106821
Browse files Browse the repository at this point in the history
#106821 Fetch x-lg-resource-class-name from final object
  • Loading branch information
MsNatali authored Jul 5, 2023
2 parents 2a5cdfc + 8ae6290 commit 7a24470
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion src/Generators/ResourcesGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions tests/GenerateServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
1 change: 1 addition & 0 deletions tests/ResourceGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
11 changes: 11 additions & 0 deletions tests/resources/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/resources/schemas/test_resource_generation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7a24470

Please sign in to comment.