Skip to content

Commit 0af5f66

Browse files
authored
Merge pull request #188 from php-api-clients/restore-LIST-to-working-order
Restore list to working order
2 parents 1a7acf4 + cfa657c commit 0af5f66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1160
-297
lines changed

src/Contract/Voter/AbstractListOperation.php

+12
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@
55
namespace ApiClients\Tools\OpenApiClientGenerator\Contract\Voter;
66

77
use ApiClients\Tools\OpenApiClientGenerator\Representation\Operation;
8+
use ApiClients\Tools\OpenApiClientGenerator\Representation\PropertyType;
9+
use ApiClients\Tools\OpenApiClientGenerator\Representation\Schema;
810

911
use function array_key_exists;
1012

1113
abstract class AbstractListOperation implements ListOperation
1214
{
1315
final public static function list(Operation $operation): bool
1416
{
17+
foreach ($operation->response as $response) {
18+
if ($response->code === 200 && $response->content instanceof Schema) {
19+
return false;
20+
}
21+
22+
if ($response->code === 200 && $response->content instanceof PropertyType && $response->content->type !== 'array') {
23+
return false;
24+
}
25+
}
26+
1527
$match = [];
1628
foreach (static::keys() as $key) {
1729
$match[$key] = false;

src/Gatherer/Operation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function gather(
6868
$parameter->schema->format,
6969
$parameter->in,
7070
$parameter->schema->default,
71-
ExampleData::scalarData(strlen($parameter->name), $parameterType, $parameter->schema->format),
71+
ExampleData::scalarData($parameter->name === 'page' ? 1 : strlen($parameter->name), $parameterType, $parameter->schema->format),
7272
);
7373
}
7474

src/Generator/Client.php

+43-148
Large diffs are not rendered by default.

src/Generator/Client/Routers.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Jawira\CaseConverter\Convert;
1111
use PhpParser\Node;
1212

13+
use function rtrim;
14+
1315
final class Routers
1416
{
1517
/** @var array<string, array<string, array<string, array{nodes: array<Node>, returnType: string, docBlockReturnType: string}>>> $operations */
@@ -58,7 +60,7 @@ public function createClassName(
5860
string $name,
5961
): Router {
6062
return new Router(
61-
'Router\\' . (new Convert($method))->toPascal() . ($group === null ? '' : '\\' . (new Convert($group))->toPascal()),
63+
rtrim('Router\\' . (new Convert($method))->toPascal() . ($group === null ? '' : '\\' . (new Convert($group))->toPascal()), '\\'),
6264
(new Convert($name))->toCamel(),
6365
);
6466
}

src/Generator/OperationTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,14 @@ private static function createCallMethod(BuilderFactory $factory, Representation
342342
),
343343
),
344344
),
345+
...$operation->matchMethod === 'LIST' ?
346+
[
347+
new Node\Stmt\Foreach_(
348+
new Node\Expr\Variable('result'),
349+
new Node\Expr\Variable('item'),
350+
),
351+
] :
352+
[],
345353
]);
346354
}
347355

@@ -519,6 +527,14 @@ private static function createOperationsMethod(BuilderFactory $factory, Represen
519527
}
520528
})(...$response->headers),
521529
] : []),
530+
...$operation->matchMethod === 'LIST' ?
531+
[
532+
new Node\Stmt\Foreach_(
533+
new Node\Expr\Variable('result'),
534+
new Node\Expr\Variable('item'),
535+
),
536+
] :
537+
[],
522538
]);
523539
}
524540

src/Voter/ListOperation/PageAndPerPageInQuery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ final public static function incrementorInitialValue(): int
2222
/** @return array<string> */
2323
final public static function keys(): array
2424
{
25-
return ['per_page', 'page'];
25+
return ['perPage', 'page'];
2626
}
2727
}

tests/app/etc/openapi-client-generator.state

+91-39
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
{
2-
"specHash": "0cbac51eea01585be386a55b74935ee4",
2+
"specHash": "b34763a4d9c5c8f8174783e23496f785",
33
"generatedFiles": {
44
"files": [
55
{
66
"name": ".\/tests\/app\/src\/\/Operation\/Pets\/List_.php",
7-
"hash": "8db699569a529a68cb68d85c8f13d9ff"
7+
"hash": "1b52986d9a38fcc0651182fcbd11ffa9"
88
},
99
{
1010
"name": ".\/tests\/app\/src\/\/Operator\/Pets\/List_.php",
11-
"hash": "f686f421cc5f2ed7c3bf632b48d33ea9"
11+
"hash": "d5e06764de2e2b183370a5aaafb9f120"
1212
},
1313
{
1414
"name": ".\/tests\/app\/tests\/\/Operation\/Pets\/List_Test.php",
15-
"hash": "84dab6ff98123001ff62ca2e17880c25"
15+
"hash": "1de84e3dd5e9cbe8865c352a4b419c19"
16+
},
17+
{
18+
"name": ".\/tests\/app\/src\/\/Operation\/Pets\/ListListing.php",
19+
"hash": "5f0b74e0153cbbcc42e609113df3ad5a"
20+
},
21+
{
22+
"name": ".\/tests\/app\/src\/\/Operator\/Pets\/ListListing.php",
23+
"hash": "e19f34c5c2a982fdbc3435c068378003"
24+
},
25+
{
26+
"name": ".\/tests\/app\/tests\/\/Operation\/Pets\/ListListingTest.php",
27+
"hash": "e84630af94b23ee2f976d817e0f2230b"
1628
},
1729
{
1830
"name": ".\/tests\/app\/src\/\/Operation\/Pets\/Create.php",
@@ -26,29 +38,65 @@
2638
"name": ".\/tests\/app\/tests\/\/Operation\/Pets\/CreateTest.php",
2739
"hash": "0f886fdac1ee9dcc0c65b6d44141f89c"
2840
},
41+
{
42+
"name": ".\/tests\/app\/src\/\/Operation\/Pets\/List_\/Gatos.php",
43+
"hash": "9eb1793cc61df8d3e2be2a2779e986a2"
44+
},
45+
{
46+
"name": ".\/tests\/app\/src\/\/Operator\/Pets\/List_\/Gatos.php",
47+
"hash": "17afd48cbf5aeb2828985ec1ef1f0a63"
48+
},
49+
{
50+
"name": ".\/tests\/app\/tests\/\/Operation\/Pets\/List_\/GatosTest.php",
51+
"hash": "55a2796c02d53a51fa395d79d2b82d47"
52+
},
53+
{
54+
"name": ".\/tests\/app\/src\/\/Operation\/Pets\/List_\/GatosListing.php",
55+
"hash": "ea926a83bcf00491d43f846d16990d12"
56+
},
57+
{
58+
"name": ".\/tests\/app\/src\/\/Operator\/Pets\/List_\/GatosListing.php",
59+
"hash": "84c5ee194ac9cfc0f1d2cac4fccfe97f"
60+
},
61+
{
62+
"name": ".\/tests\/app\/tests\/\/Operation\/Pets\/List_\/GatosListingTest.php",
63+
"hash": "364ef98360b60ab9076e41074f756894"
64+
},
2965
{
3066
"name": ".\/tests\/app\/src\/\/Operation\/Pets\/Names.php",
31-
"hash": "2477c71e0b14cd1f9848760737882801"
67+
"hash": "8de6b145ebcab2abf13a1cc5c2ffaff3"
3268
},
3369
{
3470
"name": ".\/tests\/app\/src\/\/Operator\/Pets\/Names.php",
35-
"hash": "578678c646883827a32ffbcbdd30c412"
71+
"hash": "b42a70d3644f4b22f0c520a3219ff9a6"
3672
},
3773
{
3874
"name": ".\/tests\/app\/tests\/\/Operation\/Pets\/NamesTest.php",
39-
"hash": "4c5db2bd996818af897c88c0f936edb9"
75+
"hash": "6ece3ee6680ee062a39d319c32d2910a"
76+
},
77+
{
78+
"name": ".\/tests\/app\/src\/\/Operation\/Pets\/NamesListing.php",
79+
"hash": "273115f7ac162b7b4f7b56cb775b50e4"
80+
},
81+
{
82+
"name": ".\/tests\/app\/src\/\/Operator\/Pets\/NamesListing.php",
83+
"hash": "669429923285f44f2f62c058e27eafbb"
84+
},
85+
{
86+
"name": ".\/tests\/app\/tests\/\/Operation\/Pets\/NamesListingTest.php",
87+
"hash": "ba4ec7277343ddf577a023b10d9cdc18"
4088
},
4189
{
4290
"name": ".\/tests\/app\/src\/\/Operation\/ShowPetById.php",
43-
"hash": "035ef166b47b8a4a86c7820e25e877bc"
91+
"hash": "7bf1df8e08fcf4bb8b340f0f15abd8d8"
4492
},
4593
{
4694
"name": ".\/tests\/app\/src\/\/Operator\/ShowPetById.php",
47-
"hash": "0a780f79b149907fac41168909502326"
95+
"hash": "d0f7ed4e44e0d4c854615a5f34aec9ae"
4896
},
4997
{
5098
"name": ".\/tests\/app\/tests\/\/Operation\/ShowPetByIdTest.php",
51-
"hash": "8aa0b602af15d96587d87e8dc7e15d29"
99+
"hash": "7c2ebed4bc0f0c5cb6518cb47e9c6c32"
52100
},
53101
{
54102
"name": ".\/tests\/app\/src\/\/Attribute\/CastUnionToType\/Schema\/Cat\/Eyes.php",
@@ -152,47 +200,67 @@
152200
},
153201
{
154202
"name": ".\/tests\/app\/src\/\/ClientInterface.php",
155-
"hash": "2efa88864cf28066291d4f5549afdf1f"
203+
"hash": "31a880377bfd1045c8073884486a7f11"
156204
},
157205
{
158206
"name": ".\/tests\/app\/src\/\/Client.php",
159-
"hash": "e425c30925345d36e20699214e594511"
207+
"hash": "9293562fe84f77ab4ea129c82fd1e43f"
160208
},
161209
{
162210
"name": ".\/tests\/app\/src\/\/Router\/Get\/Pets.php",
163-
"hash": "87d2ee2c70ee515745f6a6966f0fb658"
211+
"hash": "1fd2d2789f74f5cc247877c9a7076641"
212+
},
213+
{
214+
"name": ".\/tests\/app\/src\/\/Router\/Get\/PetsList.php",
215+
"hash": "3c6413f1a6bdd8f509ed0b27bb3405af"
164216
},
165217
{
166-
"name": ".\/tests\/app\/src\/\/Router\/Get\/.php",
167-
"hash": "b5171072ce8b007fd95fe95f74a21e0b"
218+
"name": ".\/tests\/app\/src\/\/Router\/List\/Pets.php",
219+
"hash": "aa71bff5fc6f1a06f10075ca6de99741"
220+
},
221+
{
222+
"name": ".\/tests\/app\/src\/\/Router\/List\/PetsList.php",
223+
"hash": "7094cde20b9f6de276d1dbbb4dbd2baf"
168224
},
169225
{
170226
"name": ".\/tests\/app\/src\/\/Router\/Post\/Pets.php",
171227
"hash": "51b996123dc285755f528f680d5d75d3"
172228
},
173229
{
174230
"name": ".\/tests\/app\/src\/\/Router\/Get\/Two.php",
175-
"hash": "c36843852038af218952791bd94a65b4"
231+
"hash": "5d568f67586671f63d3be287c88fceb8"
176232
},
177233
{
178234
"name": ".\/tests\/app\/src\/\/Router\/Get\/Three.php",
179-
"hash": "04bccb170a1687700ce416050fafb666"
235+
"hash": "1b533c4496a7a58b1c3626110e7455b5"
236+
},
237+
{
238+
"name": ".\/tests\/app\/src\/\/Router\/List\/Two.php",
239+
"hash": "0a13bba21af357741a0a0cfbb3eb2da1"
240+
},
241+
{
242+
"name": ".\/tests\/app\/src\/\/Router\/List\/Three.php",
243+
"hash": "68b44592ee664f942e50253a7ba18640"
180244
},
181245
{
182246
"name": ".\/tests\/app\/src\/\/Router\/Post\/Two.php",
183-
"hash": "5b70ce35ec18d7fb4b978cdc0c8920b7"
247+
"hash": "8b6bf0563afd6c79e6df2398463a411d"
184248
},
185249
{
186250
"name": ".\/tests\/app\/src\/\/OperationsInterface.php",
187-
"hash": "646169ea2e0f48eb285fa2dd30bc0eae"
251+
"hash": "e4ea2aaf111f619a3a7f42c59d5e755a"
188252
},
189253
{
190254
"name": ".\/tests\/app\/src\/\/Operation\/Pets.php",
191-
"hash": "d97cce58f2eb97ece6508cf27c25da39"
255+
"hash": "31cc4b8df3a1effea19139d3863ffac4"
256+
},
257+
{
258+
"name": ".\/tests\/app\/src\/\/Operation\/PetsList.php",
259+
"hash": "4e4ef678bd6485e98380a3b60f9279ad"
192260
},
193261
{
194262
"name": ".\/tests\/app\/src\/\/Operations.php",
195-
"hash": "10a69c72f12efb200062276cf5c8f9fa"
263+
"hash": "1fb22bae634b423443e062dd653d4904"
196264
},
197265
{
198266
"name": ".\/tests\/app\/src\/\/Hydrator\/Operation\/Pets.php",
@@ -215,24 +283,8 @@
215283
"hash": "6dbea9f466671e1b304df02d18b5d627"
216284
},
217285
{
218-
"name": ".\/tests\/app\/src\/\/Operation\/Pets\/List_\/Gatos.php",
219-
"hash": "c67d1b4ae68ccf194f167089ee70b680"
220-
},
221-
{
222-
"name": ".\/tests\/app\/src\/\/Operator\/Pets\/List_\/Gatos.php",
223-
"hash": "527d1d53905fe23b9432a8c6894f28a1"
224-
},
225-
{
226-
"name": ".\/tests\/app\/tests\/\/Operation\/Pets\/List_\/GatosTest.php",
227-
"hash": "793f4462e599ff18e80a06c4c7bc314b"
228-
},
229-
{
230-
"name": ".\/tests\/app\/src\/\/Router\/Get\/PetsList.php",
231-
"hash": "1ed2f5cb812f0bdd001575e1f20e09e8"
232-
},
233-
{
234-
"name": ".\/tests\/app\/src\/\/Operation\/PetsList.php",
235-
"hash": "46b100a300cad8d6bc8855b31b04254b"
286+
"name": ".\/tests\/app\/src\/\/Router\/Get.php",
287+
"hash": "4acce87dd62158f7e04dfc3f3c525c33"
236288
}
237289
]
238290
},

tests/app/src/Client.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(\ApiClients\Contracts\HTTP\Headers\AuthenticationInt
3333
}
3434
// phpcs:disable
3535
/**
36-
* @return ($call is Operation\Pets\List_::OPERATION_MATCH ? iterable<(Schema\Cat | Schema\Dog | Schema\Bird | Schema\Fish)> : ($call is Operation\Pets\Create::OPERATION_MATCH ? array{code: int} : ($call is Operation\Pets\List_\Gatos::OPERATION_MATCH ? iterable<Schema\Cat> : ($call is Operation\Pets\Names::OPERATION_MATCH ? iterable<string> : (Schema\Cat | Schema\Dog | Schema\Bird | Schema\Fish))))))
36+
* @return ($call is Operation\Pets\List_::OPERATION_MATCH ? iterable<(Schema\Cat | Schema\Dog | Schema\Bird | Schema\Fish)> : ($call is Operation\Pets\ListListing::OPERATION_MATCH ? iterable<(Schema\Cat | Schema\Dog | Schema\Bird | Schema\Fish)> : ($call is Operation\Pets\Create::OPERATION_MATCH ? array{code: int} : ($call is Operation\Pets\List_\Gatos::OPERATION_MATCH ? iterable<Schema\Cat> : ($call is Operation\Pets\List_\GatosListing::OPERATION_MATCH ? iterable<Schema\Cat> : ($call is Operation\Pets\Names::OPERATION_MATCH ? iterable<string> : ($call is Operation\Pets\NamesListing::OPERATION_MATCH ? iterable<string> : (Schema\Cat | Schema\Dog | Schema\Bird | Schema\Fish)))))))))
3737
*/
3838
// phpcs:enable
3939
public function call(string $call, array $params = array()) : iterable|\ApiClients\Client\PetStore\Schema\Cat|\ApiClients\Client\PetStore\Schema\Dog|\ApiClients\Client\PetStore\Schema\Bird|\ApiClients\Client\PetStore\Schema\Fish
@@ -56,6 +56,20 @@ public function call(string $call, array $params = array()) : iterable|\ApiClien
5656
}
5757
return $this->router[Router\Get\Three::class]->call($call, $params, $pathChunks);
5858
}
59+
} elseif ($method === 'LIST') {
60+
if ($pathChunksCount === 2) {
61+
$matched = true;
62+
if (\array_key_exists(Router\List\Two::class, $this->router) == false) {
63+
$this->router[Router\List\Two::class] = new Router\List\Two(browser: $this->browser, authentication: $this->authentication, requestSchemaValidator: $this->requestSchemaValidator, responseSchemaValidator: $this->responseSchemaValidator, hydrators: $this->hydrators);
64+
}
65+
return $this->router[Router\List\Two::class]->call($call, $params, $pathChunks);
66+
} elseif ($pathChunksCount === 3) {
67+
$matched = true;
68+
if (\array_key_exists(Router\List\Three::class, $this->router) == false) {
69+
$this->router[Router\List\Three::class] = new Router\List\Three(browser: $this->browser, authentication: $this->authentication, requestSchemaValidator: $this->requestSchemaValidator, responseSchemaValidator: $this->responseSchemaValidator, hydrators: $this->hydrators);
70+
}
71+
return $this->router[Router\List\Three::class]->call($call, $params, $pathChunks);
72+
}
5973
} elseif ($method === 'POST') {
6074
if ($pathChunksCount === 2) {
6175
$matched = true;

tests/app/src/ClientInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface ClientInterface
1717
{
1818
// phpcs:disable
1919
/**
20-
* @return ($call is Operation\Pets\List_::OPERATION_MATCH ? iterable<(Schema\Cat | Schema\Dog | Schema\Bird | Schema\Fish)> : ($call is Operation\Pets\Create::OPERATION_MATCH ? array{code: int} : ($call is Operation\Pets\List_\Gatos::OPERATION_MATCH ? iterable<Schema\Cat> : ($call is Operation\Pets\Names::OPERATION_MATCH ? iterable<string> : (Schema\Cat | Schema\Dog | Schema\Bird | Schema\Fish))))))
20+
* @return ($call is Operation\Pets\List_::OPERATION_MATCH ? iterable<(Schema\Cat | Schema\Dog | Schema\Bird | Schema\Fish)> : ($call is Operation\Pets\ListListing::OPERATION_MATCH ? iterable<(Schema\Cat | Schema\Dog | Schema\Bird | Schema\Fish)> : ($call is Operation\Pets\Create::OPERATION_MATCH ? array{code: int} : ($call is Operation\Pets\List_\Gatos::OPERATION_MATCH ? iterable<Schema\Cat> : ($call is Operation\Pets\List_\GatosListing::OPERATION_MATCH ? iterable<Schema\Cat> : ($call is Operation\Pets\Names::OPERATION_MATCH ? iterable<string> : ($call is Operation\Pets\NamesListing::OPERATION_MATCH ? iterable<string> : (Schema\Cat | Schema\Dog | Schema\Bird | Schema\Fish)))))))))
2121
*/
2222
// phpcs:enable
2323
public function call(string $call, array $params = array()) : iterable|\ApiClients\Client\PetStore\Schema\Cat|\ApiClients\Client\PetStore\Schema\Dog|\ApiClients\Client\PetStore\Schema\Bird|\ApiClients\Client\PetStore\Schema\Fish;

tests/app/src/Operation/Pets.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@ final class Pets
1919
public function __construct(private \React\Http\Browser $browser, private \ApiClients\Contracts\HTTP\Headers\AuthenticationInterface $authentication, private \League\OpenAPIValidation\Schema\SchemaValidator $requestSchemaValidator, private \League\OpenAPIValidation\Schema\SchemaValidator $responseSchemaValidator, private \ApiClients\Client\PetStore\Hydrators $hydrators)
2020
{
2121
}
22-
public function list(int $limit) : Schema\Operations\Pets\List_\Response\ApplicationJson\Ok
22+
public function list(int $perPage, int $page) : Schema\Operations\Pets\List_\Response\ApplicationJson\Ok
2323
{
2424
if (\array_key_exists(Operator\Pets\List_::class, $this->operator) == false) {
2525
$this->operator[Operator\Pets\List_::class] = new Operator\Pets\List_($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrators->getObjectMapperOperation🌀Pets());
2626
}
27-
return $this->operator[Operator\Pets\List_::class]->call($limit);
27+
return $this->operator[Operator\Pets\List_::class]->call($perPage, $page);
28+
}
29+
public function listListing(int $perPage, int $page) : Schema\Operations\Pets\List_\Response\ApplicationJson\Ok
30+
{
31+
if (\array_key_exists(Operator\Pets\ListListing::class, $this->operator) == false) {
32+
$this->operator[Operator\Pets\ListListing::class] = new Operator\Pets\ListListing($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrators->getObjectMapperOperation🌀Pets());
33+
}
34+
return $this->operator[Operator\Pets\ListListing::class]->call($perPage, $page);
2835
}
2936
public function create(array $params) : \Psr\Http\Message\ResponseInterface
3037
{
@@ -33,11 +40,18 @@ public function create(array $params) : \Psr\Http\Message\ResponseInterface
3340
}
3441
return $this->operator[Operator\Pets\Create::class]->call($params);
3542
}
36-
public function names(int $limit) : Schema\Operations\Pets\Names\Response\ApplicationJson\Ok
43+
public function names(int $perPage, int $page) : Schema\Operations\Pets\Names\Response\ApplicationJson\Ok
3744
{
3845
if (\array_key_exists(Operator\Pets\Names::class, $this->operator) == false) {
3946
$this->operator[Operator\Pets\Names::class] = new Operator\Pets\Names($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrators->getObjectMapperOperation🌀Pets🌀Names());
4047
}
41-
return $this->operator[Operator\Pets\Names::class]->call($limit);
48+
return $this->operator[Operator\Pets\Names::class]->call($perPage, $page);
49+
}
50+
public function namesListing(int $perPage, int $page) : Schema\Operations\Pets\Names\Response\ApplicationJson\Ok
51+
{
52+
if (\array_key_exists(Operator\Pets\NamesListing::class, $this->operator) == false) {
53+
$this->operator[Operator\Pets\NamesListing::class] = new Operator\Pets\NamesListing($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrators->getObjectMapperOperation🌀Pets🌀Names());
54+
}
55+
return $this->operator[Operator\Pets\NamesListing::class]->call($perPage, $page);
4256
}
4357
}

0 commit comments

Comments
 (0)