Skip to content

Commit b765ad7

Browse files
committed
[Slim] Sort operations in supporting files data to avoid shadowing static routes.
1 parent 00840e2 commit b765ad7

File tree

5 files changed

+120
-86
lines changed

5 files changed

+120
-86
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSlimServerCodegen.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import java.util.HashMap;
3838
import java.util.HashSet;
3939
import java.util.regex.Matcher;
40+
import java.util.Comparator;
41+
import java.util.Collections;
4042

4143
public class PhpSlimServerCodegen extends DefaultCodegen implements CodegenConfig {
4244
private static final Logger LOGGER = LoggerFactory.getLogger(PhpSlimServerCodegen.class);
@@ -344,4 +346,26 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
344346
return objs;
345347
}
346348

349+
@Override
350+
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
351+
Map<String, Object> apiInfo = (Map<String, Object>) objs.get("apiInfo");
352+
List<HashMap<String, Object>> apiList = (List<HashMap<String, Object>>) apiInfo.get("apis");
353+
for (HashMap<String, Object> api : apiList) {
354+
HashMap<String, Object> operations = (HashMap<String, Object>) api.get("operations");
355+
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
356+
357+
// Sort operations to avoid static routes shadowing
358+
// ref: https://github.com/nikic/FastRoute/blob/master/src/DataGenerator/RegexBasedAbstract.php#L92-L101
359+
Collections.sort(operationList, new Comparator<CodegenOperation>() {
360+
@Override
361+
public int compare(CodegenOperation one, CodegenOperation another) {
362+
if (one.getHasPathParams() && !another.getHasPathParams()) return 1;
363+
if (!one.getHasPathParams() && another.getHasPathParams()) return -1;
364+
return 0;
365+
}
366+
});
367+
}
368+
return objs;
369+
}
370+
347371
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.0-SNAPSHOT
1+
3.0.3-SNAPSHOT

samples/server/petstore-security-test/slim/index.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616

1717
*/
1818
$app->PUT('/fake', function($request, $response, $args) {
19-
20-
21-
22-
$body = $request->getParsedBody();
23-
$response->write('How about implementing testCodeInject */ &#39; &quot; &#x3D;end \r\n \n \r as a PUT method ?');
24-
return $response;
25-
});
19+
$body = $request->getParsedBody();
20+
$response->write('How about implementing testCodeInject */ &#39; &quot; &#x3D;end \r\n \n \r as a PUT method ?');
21+
return $response;
22+
});
2623

2724

2825

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/*
3+
* ModelReturn
4+
*/
5+
namespace \Models;
6+
7+
/*
8+
* ModelReturn
9+
*/
10+
class ModelReturn {
11+
/* @var int $return property description &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r */
12+
private $return;
13+
}

samples/server/petstore/php-slim/index.php

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -219,22 +219,6 @@
219219
});
220220

221221

222-
/**
223-
* DELETE deletePet
224-
* Summary: Deletes a pet
225-
* Notes:
226-
227-
*/
228-
$app->DELETE('/v2/pet/{petId}', function($request, $response, $args) {
229-
$headers = $request->getHeaders();
230-
231-
232-
233-
$response->write('How about implementing deletePet as a DELETE method ?');
234-
return $response;
235-
});
236-
237-
238222
/**
239223
* GET findPetsByStatus
240224
* Summary: Finds Pets by status
@@ -270,33 +254,49 @@
270254

271255

272256
/**
273-
* GET getPetById
274-
* Summary: Find pet by ID
275-
* Notes: Returns a single pet
276-
* Output-Formats: [application/xml, application/json]
257+
* PUT updatePet
258+
* Summary: Update an existing pet
259+
* Notes:
260+
277261
*/
278-
$app->GET('/v2/pet/{petId}', function($request, $response, $args) {
279-
262+
$app->PUT('/v2/pet', function($request, $response, $args) {
280263

281264

282265

283-
$response->write('How about implementing getPetById as a GET method ?');
266+
$body = $request->getParsedBody();
267+
$response->write('How about implementing updatePet as a PUT method ?');
284268
return $response;
285269
});
286270

287271

288272
/**
289-
* PUT updatePet
290-
* Summary: Update an existing pet
273+
* DELETE deletePet
274+
* Summary: Deletes a pet
291275
* Notes:
292276
293277
*/
294-
$app->PUT('/v2/pet', function($request, $response, $args) {
278+
$app->DELETE('/v2/pet/{petId}', function($request, $response, $args) {
279+
$headers = $request->getHeaders();
295280

296281

297282

298-
$body = $request->getParsedBody();
299-
$response->write('How about implementing updatePet as a PUT method ?');
283+
$response->write('How about implementing deletePet as a DELETE method ?');
284+
return $response;
285+
});
286+
287+
288+
/**
289+
* GET getPetById
290+
* Summary: Find pet by ID
291+
* Notes: Returns a single pet
292+
* Output-Formats: [application/xml, application/json]
293+
*/
294+
$app->GET('/v2/pet/{petId}', function($request, $response, $args) {
295+
296+
297+
298+
299+
$response->write('How about implementing getPetById as a GET method ?');
300300
return $response;
301301
});
302302

@@ -334,65 +334,65 @@
334334

335335

336336
/**
337-
* DELETE deleteOrder
338-
* Summary: Delete purchase order by ID
339-
* Notes: For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
340-
337+
* GET getInventory
338+
* Summary: Returns pet inventories by status
339+
* Notes: Returns a map of status codes to quantities
340+
* Output-Formats: [application/json]
341341
*/
342-
$app->DELETE('/v2/store/order/{order_id}', function($request, $response, $args) {
342+
$app->GET('/v2/store/inventory', function($request, $response, $args) {
343343

344344

345345

346346

347-
$response->write('How about implementing deleteOrder as a DELETE method ?');
347+
$response->write('How about implementing getInventory as a GET method ?');
348348
return $response;
349349
});
350350

351351

352352
/**
353-
* GET getInventory
354-
* Summary: Returns pet inventories by status
355-
* Notes: Returns a map of status codes to quantities
356-
* Output-Formats: [application/json]
353+
* POST placeOrder
354+
* Summary: Place an order for a pet
355+
* Notes:
356+
* Output-Formats: [application/xml, application/json]
357357
*/
358-
$app->GET('/v2/store/inventory', function($request, $response, $args) {
359-
358+
$app->POST('/v2/store/order', function($request, $response, $args) {
360359

361360

362361

363-
$response->write('How about implementing getInventory as a GET method ?');
362+
$body = $request->getParsedBody();
363+
$response->write('How about implementing placeOrder as a POST method ?');
364364
return $response;
365365
});
366366

367367

368368
/**
369-
* GET getOrderById
370-
* Summary: Find purchase order by ID
371-
* Notes: For valid response try integer IDs with value &lt;&#x3D; 5 or &gt; 10. Other values will generated exceptions
372-
* Output-Formats: [application/xml, application/json]
369+
* DELETE deleteOrder
370+
* Summary: Delete purchase order by ID
371+
* Notes: For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
372+
373373
*/
374-
$app->GET('/v2/store/order/{order_id}', function($request, $response, $args) {
374+
$app->DELETE('/v2/store/order/{order_id}', function($request, $response, $args) {
375375

376376

377377

378378

379-
$response->write('How about implementing getOrderById as a GET method ?');
379+
$response->write('How about implementing deleteOrder as a DELETE method ?');
380380
return $response;
381381
});
382382

383383

384384
/**
385-
* POST placeOrder
386-
* Summary: Place an order for a pet
387-
* Notes:
385+
* GET getOrderById
386+
* Summary: Find purchase order by ID
387+
* Notes: For valid response try integer IDs with value &lt;&#x3D; 5 or &gt; 10. Other values will generated exceptions
388388
* Output-Formats: [application/xml, application/json]
389389
*/
390-
$app->POST('/v2/store/order', function($request, $response, $args) {
390+
$app->GET('/v2/store/order/{order_id}', function($request, $response, $args) {
391391

392392

393393

394-
$body = $request->getParsedBody();
395-
$response->write('How about implementing placeOrder as a POST method ?');
394+
395+
$response->write('How about implementing getOrderById as a GET method ?');
396396
return $response;
397397
});
398398

@@ -446,66 +446,66 @@
446446

447447

448448
/**
449-
* DELETE deleteUser
450-
* Summary: Delete user
451-
* Notes: This can only be done by the logged in user.
452-
449+
* GET loginUser
450+
* Summary: Logs user into the system
451+
* Notes:
452+
* Output-Formats: [application/xml, application/json]
453453
*/
454-
$app->DELETE('/v2/user/{username}', function($request, $response, $args) {
455-
454+
$app->GET('/v2/user/login', function($request, $response, $args) {
456455

456+
$queryParams = $request->getQueryParams();
457+
$username = $queryParams['username']; $password = $queryParams['password'];
457458

458459

459-
$response->write('How about implementing deleteUser as a DELETE method ?');
460+
$response->write('How about implementing loginUser as a GET method ?');
460461
return $response;
461462
});
462463

463464

464465
/**
465-
* GET getUserByName
466-
* Summary: Get user by user name
466+
* GET logoutUser
467+
* Summary: Logs out current logged in user session
467468
* Notes:
468-
* Output-Formats: [application/xml, application/json]
469+
469470
*/
470-
$app->GET('/v2/user/{username}', function($request, $response, $args) {
471+
$app->GET('/v2/user/logout', function($request, $response, $args) {
471472

472473

473474

474475

475-
$response->write('How about implementing getUserByName as a GET method ?');
476+
$response->write('How about implementing logoutUser as a GET method ?');
476477
return $response;
477478
});
478479

479480

480481
/**
481-
* GET loginUser
482-
* Summary: Logs user into the system
483-
* Notes:
484-
* Output-Formats: [application/xml, application/json]
482+
* DELETE deleteUser
483+
* Summary: Delete user
484+
* Notes: This can only be done by the logged in user.
485+
485486
*/
486-
$app->GET('/v2/user/login', function($request, $response, $args) {
487+
$app->DELETE('/v2/user/{username}', function($request, $response, $args) {
487488

488-
$queryParams = $request->getQueryParams();
489-
$username = $queryParams['username']; $password = $queryParams['password'];
490489

491490

492-
$response->write('How about implementing loginUser as a GET method ?');
491+
492+
$response->write('How about implementing deleteUser as a DELETE method ?');
493493
return $response;
494494
});
495495

496496

497497
/**
498-
* GET logoutUser
499-
* Summary: Logs out current logged in user session
498+
* GET getUserByName
499+
* Summary: Get user by user name
500500
* Notes:
501-
501+
* Output-Formats: [application/xml, application/json]
502502
*/
503-
$app->GET('/v2/user/logout', function($request, $response, $args) {
503+
$app->GET('/v2/user/{username}', function($request, $response, $args) {
504504

505505

506506

507507

508-
$response->write('How about implementing logoutUser as a GET method ?');
508+
$response->write('How about implementing getUserByName as a GET method ?');
509509
return $response;
510510
});
511511

0 commit comments

Comments
 (0)