Skip to content

Commit 5423164

Browse files
committed
Fix this issue
1 parent 7799c8e commit 5423164

File tree

12 files changed

+220
-9
lines changed

12 files changed

+220
-9
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ Provide custom database table column name in case of relationship column. This w
309309
- x-fk-column-name: redelivery_of # this will create `redelivery_of` column instead of `redelivery_of_id`
310310
```
311311
312+
### `x-route`
313+
314+
https://github.com/cebe/yii2-openapi/issues/144
315+
TODO
316+
312317
## Many-to-Many relation definition
313318

314319
There are two ways for define many-to-many relations:

src/lib/CustomSpecAttr.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ class CustomSpecAttr
4040
* Foreign key column name. See README for usage docs
4141
*/
4242
public const FK_COLUMN_NAME = 'x-fk-column-name';
43+
44+
/**
45+
* Custom route (controller ID/action ID) instead of auto-generated. See README for usage docs. https://github.com/cebe/yii2-openapi/issues/144
46+
*/
47+
public const ROUTE = 'x-route';
4348
}

src/lib/generators/RestActionGenerator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use cebe\openapi\spec\PathItem;
1212
use cebe\openapi\spec\Reference;
1313
use cebe\yii2openapi\lib\Config;
14+
use cebe\yii2openapi\lib\CustomSpecAttr;
1415
use cebe\yii2openapi\lib\items\RestAction;
1516
use cebe\yii2openapi\lib\items\RouteData;
1617
use cebe\yii2openapi\lib\openapi\ResponseSchema;
@@ -44,6 +45,11 @@ public function generate():array
4445
{
4546
$actions = [];
4647
foreach ($this->config->getOpenApi()->paths as $path => $pathItem) {
48+
$customRoute = null;
49+
if (isset($pathItem->{CustomSpecAttr::ROUTE})) { # https://github.com/cebe/yii2-openapi/issues/144
50+
$customRoute = $pathItem->{CustomSpecAttr::ROUTE};
51+
}
52+
4753
if ($path[0] !== '/') {
4854
throw new InvalidConfigException('Path must begin with /');
4955
}
@@ -53,7 +59,7 @@ public function generate():array
5359
if ($pathItem instanceof Reference) {
5460
$pathItem = $pathItem->resolve();
5561
}
56-
$actions[] = $this->resolvePath($path, $pathItem);
62+
$actions[] = $this->resolvePath(!empty($customRoute) ? '/' . $customRoute : $path, $pathItem);
5763
}
5864
return array_merge(...$actions);
5965
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* OpenAPI UrlRules
4+
*
5+
* This file is auto generated.
6+
*/
7+
return [
8+
'POST payments/invoice/<invoice:\d+>' => 'payments/create-invoice',
9+
'GET payments/invoice-payment' => 'payment/invoice-payment',
10+
'GET abc/xyz' => 'abc/xyz',
11+
'POST abc/xyz' => 'abc/create-xyz',
12+
'payments/invoice/<invoice:\d+>' => 'payments/options',
13+
'payments/invoice-payment' => 'payment/options',
14+
'abc/xyz' => 'abc/options',
15+
];
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace app\controllers;
4+
5+
class AbcController extends \app\controllers\base\AbcController
6+
{
7+
8+
public function checkAccess($action, $model = null, $params = [])
9+
{
10+
//TODO implement checkAccess
11+
}
12+
13+
public function actionXyz()
14+
{
15+
//TODO implement actionXyz
16+
}
17+
18+
public function actionCreateXyz()
19+
{
20+
//TODO implement actionCreateXyz
21+
}
22+
23+
24+
}
25+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace app\controllers;
4+
5+
class PaymentController extends \app\controllers\base\PaymentController
6+
{
7+
8+
public function checkAccess($action, $model = null, $params = [])
9+
{
10+
//TODO implement checkAccess
11+
}
12+
13+
public function actionInvoicePayment()
14+
{
15+
//TODO implement actionInvoicePayment
16+
}
17+
18+
19+
}
20+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace app\controllers;
4+
5+
class PaymentsController extends \app\controllers\base\PaymentsController
6+
{
7+
8+
public function checkAccess($action, $model = null, $params = [])
9+
{
10+
//TODO implement checkAccess
11+
}
12+
13+
public function actionCreateInvoice($invoice)
14+
{
15+
//TODO implement actionCreateInvoice
16+
}
17+
18+
19+
}
20+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace app\controllers\base;
4+
5+
abstract class AbcController extends \yii\rest\Controller
6+
{
7+
public function actions()
8+
{
9+
return [
10+
'options' => [
11+
'class' => \yii\rest\OptionsAction::class,
12+
],
13+
];
14+
}
15+
16+
/**
17+
* Checks the privilege of the current user.
18+
*
19+
* This method checks whether the current user has the privilege
20+
* to run the specified action against the specified data model.
21+
* If the user does not have access, a [[ForbiddenHttpException]] should be thrown.
22+
*
23+
* @param string $action the ID of the action to be executed
24+
* @param object $model the model to be accessed. If null, it means no specific model is being accessed.
25+
* @param array $params additional parameters
26+
* @throws \yii\web\ForbiddenHttpException if the user does not have access
27+
*/
28+
abstract public function checkAccess($action, $model = null, $params = []);
29+
30+
abstract public function actionXyz();
31+
32+
abstract public function actionCreateXyz();
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace app\controllers\base;
4+
5+
abstract class PaymentController extends \yii\rest\Controller
6+
{
7+
public function actions()
8+
{
9+
return [
10+
'options' => [
11+
'class' => \yii\rest\OptionsAction::class,
12+
],
13+
];
14+
}
15+
16+
/**
17+
* Checks the privilege of the current user.
18+
*
19+
* This method checks whether the current user has the privilege
20+
* to run the specified action against the specified data model.
21+
* If the user does not have access, a [[ForbiddenHttpException]] should be thrown.
22+
*
23+
* @param string $action the ID of the action to be executed
24+
* @param object $model the model to be accessed. If null, it means no specific model is being accessed.
25+
* @param array $params additional parameters
26+
* @throws \yii\web\ForbiddenHttpException if the user does not have access
27+
*/
28+
abstract public function checkAccess($action, $model = null, $params = []);
29+
30+
abstract public function actionInvoicePayment();
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace app\controllers\base;
4+
5+
abstract class PaymentsController extends \yii\rest\Controller
6+
{
7+
public function actions()
8+
{
9+
return [
10+
'options' => [
11+
'class' => \yii\rest\OptionsAction::class,
12+
],
13+
];
14+
}
15+
16+
/**
17+
* Checks the privilege of the current user.
18+
*
19+
* This method checks whether the current user has the privilege
20+
* to run the specified action against the specified data model.
21+
* If the user does not have access, a [[ForbiddenHttpException]] should be thrown.
22+
*
23+
* @param string $action the ID of the action to be executed
24+
* @param object $model the model to be accessed. If null, it means no specific model is being accessed.
25+
* @param array $params additional parameters
26+
* @throws \yii\web\ForbiddenHttpException if the user does not have access
27+
*/
28+
abstract public function checkAccess($action, $model = null, $params = []);
29+
30+
abstract public function actionCreateInvoice($invoice);
31+
32+
}

0 commit comments

Comments
 (0)