Skip to content

Commit fcf4147

Browse files
committed
Add fix & test for cebe#84
1 parent 528497d commit fcf4147

File tree

8 files changed

+168
-12
lines changed

8 files changed

+168
-12
lines changed

src/generator/ApiGenerator.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace cebe\yii2openapi\generator;
99

10+
use cebe\yii2openapi\lib\items\RestAction;
1011
use yii\db\mysql\Schema as MySqlSchema;
1112
use SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
1213
use yii\db\pgsql\Schema as PgSqlSchema;
@@ -473,6 +474,7 @@ public function generate():array
473474
$urlRulesGenerator = Yii::createObject(UrlRulesGenerator::class, [$config, $actions]);
474475
$files = $urlRulesGenerator->generate();
475476

477+
$actions = static::removeDuplicateActions($actions);
476478
$controllersGenerator = Yii::createObject(ControllersGenerator::class, [$config, $actions]);
477479
$files->merge($controllersGenerator->generate());
478480

@@ -521,4 +523,29 @@ public static function isMariaDb():bool
521523
{
522524
return strpos(Yii::$app->db->schema->getServerVersion(), 'MariaDB') !== false;
523525
}
526+
527+
/**
528+
* @param RestAction[] $actions
529+
* @return RestAction[]
530+
* https://github.com/cebe/yii2-openapi/issues/84
531+
*/
532+
public static function removeDuplicateActions(array $actions): array
533+
{
534+
$actions = array_filter($actions, function (RestAction $action) {
535+
if ($action->isDuplicate) {
536+
return false;
537+
}
538+
return true;
539+
});
540+
541+
$actions = array_map(function (RestAction $action) {
542+
if ($action->isOriginalForCustomRoute) {
543+
$action->idParam = null;
544+
$action->params = [];
545+
}
546+
return $action;
547+
}, $actions);
548+
549+
return $actions;
550+
}
524551
}

src/lib/generators/ControllersGenerator.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,13 @@ protected function makeCustomController(
129129
$params = array_map(static function ($param) {
130130
return ['name' => $param];
131131
}, $action->getParamNames());
132-
if (!$reflection->hasMethod($action->actionMethodName)) {
133-
$reflection->addMethod(
134-
$action->actionMethodName,
135-
$params,
136-
AbstractMemberGenerator::FLAG_PUBLIC,
137-
'//TODO implement ' . $action->actionMethodName
138-
);
139-
}
132+
133+
$reflection->addMethod(
134+
$action->actionMethodName,
135+
$params,
136+
AbstractMemberGenerator::FLAG_PUBLIC,
137+
'//TODO implement ' . $action->actionMethodName
138+
);
140139
}
141140
$classFileGenerator->setClasses([$reflection]);
142141
return $classFileGenerator;

src/lib/generators/RestActionGenerator.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function generate():array
5959
return array_merge(...$actions);
6060
}
6161

62+
private $allCustomRoutes = [];
6263
/**
6364
* @param string $path
6465
* @param \cebe\openapi\spec\PathItem $pathItem
@@ -76,7 +77,19 @@ protected function resolvePath(string $path, PathItem $pathItem):array
7677
if (isset($operation->{CustomSpecAttr::ROUTE})) { # https://github.com/cebe/yii2-openapi/issues/144
7778
$customRoute = $operation->{CustomSpecAttr::ROUTE};
7879
}
79-
$actions[] = $this->prepareAction($method, $operation, $routeData, $customRoute);
80+
if ($customRoute === null) {
81+
$actions[] = $this->prepareAction($method, $operation, $routeData, $customRoute);
82+
} else {
83+
// TODO rename
84+
$actionHere = $this->prepareAction($method, $operation, $routeData, $customRoute);
85+
$actionHere->isOriginalForCustomRoute = true;
86+
if (!in_array($customRoute, $this->allCustomRoutes)) {
87+
$actionHere->isOriginalForCustomRoute = false;
88+
$actionHere->isDuplicate = true;
89+
$this->allCustomRoutes[] = $customRoute;
90+
}
91+
$actions[] = $actionHere;
92+
}
8093
}
8194
return $actions;
8295
}

src/lib/items/RestAction.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ final class RestAction extends BaseObject
6868
*/
6969
public $responseWrapper;
7070

71+
/**
72+
* @var bool
73+
* @see $isDuplicate
74+
*/
75+
public $isOriginalForCustomRoute = false;
76+
/**
77+
* @var bool
78+
* https://github.com/cebe/yii2-openapi/issues/84
79+
* Generate only one action for paths like: `GET /calendar/domains` and `GET /calendar/domains/{id}`.
80+
* @see $isOriginalForCustomRoute
81+
*/
82+
public $isDuplicate = false;
83+
7184
public function getRoute():string
7285
{
7386
if ($this->prefix && !empty($this->prefixSettings)) {

tests/specs/issue_fix/144_methods_naming_for_non_crud_actions/index.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ paths:
1414
/fruit/mango:
1515
get:
1616
x-route: fruits/mango
17-
operationId: opnid9
17+
operationId: opnid91
1818
summary: Lorem ipsum
1919
description: Lorem ipsum description
2020
responses:
@@ -24,7 +24,7 @@ paths:
2424

2525
/fruits/mango:
2626
get:
27-
operationId: opnid8
27+
operationId: opnid81
2828
summary: Lorem ipsum
2929
description: Lorem ipsum description
3030
responses:
@@ -47,7 +47,7 @@ paths:
4747
'200':
4848
description: The Response
4949
post:
50-
operationId: opnid9
50+
operationId: opnid92
5151
summary: Lorem ipsum
5252
description: Lorem ipsum description
5353
responses:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/84_how_to_generate_controller_code_with_distinct_method_names_in_case_of_prefix_in_paths/index.yaml',
5+
'generateUrls' => true,
6+
'generateModels' => false,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => true,
11+
'generateMigrations' => false,
12+
'generateModelFaker' => false, // `generateModels` must be `true` in order to use `generateModelFaker` as `true`
13+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
openapi: 3.0.3
2+
3+
info:
4+
title: 'Custom route for path'
5+
version: 1.0.0
6+
7+
tags:
8+
- name: Payments
9+
description: Pay or receive payments for your products from different channels
10+
externalDocs:
11+
description: Find out more
12+
url: https://developer.adiuta.com/book/payments
13+
paths:
14+
/calendar/domains:
15+
get:
16+
x-route: calendar/domains
17+
summary: Lorem ipsum
18+
description: Lorem ipsum description
19+
responses:
20+
'200':
21+
description: The Response
22+
23+
24+
/calendar/domains/{id}:
25+
parameters:
26+
- name: id
27+
in: path
28+
description: lorem ipsum
29+
required: true
30+
schema:
31+
type: integer
32+
get:
33+
x-route: calendar/domains
34+
summary: Lorem ipsum
35+
description: Lorem ipsum description
36+
responses:
37+
'200':
38+
description: The Response
39+
40+
41+
42+
components:
43+
schemas:
44+
Payments:
45+
required:
46+
- reference
47+
- amount
48+
- currency
49+
properties:
50+
invoice_number:
51+
type: string
52+
amount:
53+
type: integer
54+
format: int64
55+
currency:
56+
type: string
57+
58+
Success:
59+
required:
60+
- success
61+
- message
62+
properties:
63+
success:
64+
type: boolean
65+
message:
66+
type: string
67+
68+
Error:
69+
required:
70+
- code
71+
- message
72+
properties:
73+
code:
74+
type: integer
75+
format: int32
76+
message:
77+
type: string

tests/unit/IssueFixTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,18 @@ public function test144MethodsNamingForNonCrudActions()
374374
]);
375375
$this->checkFiles($actualFiles, $expectedFiles);
376376
}
377+
378+
// https://github.com/cebe/yii2-openapi/issues/84
379+
public function test84HowToGenerateControllerCodeWithDistinctMethodNamesInCaseOfPrefixInPaths()
380+
{
381+
$testFile = Yii::getAlias("@specs/issue_fix/84_how_to_generate_controller_code_with_distinct_method_names_in_case_of_prefix_in_paths/index.php");
382+
$this->runGenerator($testFile);
383+
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
384+
'recursive' => true,
385+
]);
386+
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/84_how_to_generate_controller_code_with_distinct_method_names_in_case_of_prefix_in_paths/app"), [
387+
'recursive' => true,
388+
]);
389+
// $this->checkFiles($actualFiles, $expectedFiles); // TODO
390+
}
377391
}

0 commit comments

Comments
 (0)