Skip to content

Commit 1ce997f

Browse files
committed
Merge branches 'master' and '63-just-column-name-rename' of github.com:php-openapi/yii2-openapi into 63-just-column-name-rename
2 parents 36c576e + ca23577 commit 1ce997f

File tree

22 files changed

+655
-41
lines changed

22 files changed

+655
-41
lines changed

src/lib/ColumnToCode.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -220,25 +220,18 @@ public function getCode(bool $quoted = false):string
220220
array_unshift($parts, '$this');
221221
return implode('->', array_filter(array_map('trim', $parts)));
222222
}
223-
if ($this->rawParts['default'] === null) {
224-
$default = '';
225-
} elseif (ApiGenerator::isPostgres() && $this->isEnum()) {
226-
$default =
227-
$this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : '';
228-
} else {
229-
$default = $this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : '';
223+
224+
if (ApiGenerator::isPostgres() && $this->alterByXDbType) {
225+
return $quoted ? VarDumper::export($this->rawParts['type']) : $this->rawParts['type'];
230226
}
231227

228+
$default = $this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : '';
232229
$code = $this->rawParts['type'] . ' ' . $this->rawParts['nullable'] . $default;
233230

234231
if ((ApiGenerator::isMysql() || ApiGenerator::isMariaDb())) {
235232
$code .= $this->rawParts['position'] ? ' ' . $this->rawParts['position'] : '';
236233
$code .= $this->rawParts['comment'] ? ' '.$this->rawParts['comment'] : '';
237234
}
238-
239-
if (ApiGenerator::isPostgres() && $this->alterByXDbType) {
240-
return $quoted ? VarDumper::export($this->rawParts['type']) : $this->rawParts['type'];
241-
}
242235
return $quoted ? VarDumper::export($code) : $code;
243236
}
244237

@@ -407,8 +400,6 @@ private function getIsBuiltinType($type, $dbType)
407400
private function resolveEnumType():void
408401
{
409402
if (ApiGenerator::isPostgres()) {
410-
// $rawTableName = $this->dbSchema->getRawTableName($this->tableAlias);
411-
// $this->rawParts['type'] = '"enum_'.$rawTableName.'_' . $this->column->name.'"';
412403
$this->rawParts['type'] = '"'.$this->column->dbType.'"';
413404
return;
414405
}

src/lib/items/FractalAction.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
*/
3333
final class FractalAction extends BaseObject
3434
{
35+
use OptionsRoutesTrait;
36+
3537
/**@var string* */
3638
public $id;
3739

@@ -102,16 +104,6 @@ public function getRoute():string
102104
return $this->controllerId.'/'.$this->id;
103105
}
104106

105-
public function getOptionsRoute():string
106-
{
107-
//@TODO: re-check
108-
if ($this->prefix && !empty($this->prefixSettings)) {
109-
$prefix = $this->prefixSettings['module'] ?? $this->prefix;
110-
return trim($prefix, '/').'/'.$this->controllerId.'/options';
111-
}
112-
return $this->controllerId.'/options';
113-
}
114-
115107
public function getBaseModelName():string
116108
{
117109
return $this->modelFqn ? StringHelper::basename($this->modelFqn) : '';

src/lib/items/OptionsRoutesTrait.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (c) 2018 Carsten Brandt <mail@cebe.cc> and contributors
5+
* @license https://github.com/cebe/yii2-openapi/blob/master/LICENSE
6+
*/
7+
8+
namespace cebe\yii2openapi\lib\items;
9+
10+
trait OptionsRoutesTrait
11+
{
12+
public function getOptionsRoute():string
13+
{
14+
if ($this->prefix && !empty($this->prefixSettings)) {
15+
if (isset($this->prefixSettings['module'])) {
16+
$prefix = $this->prefixSettings['module'];
17+
return static::finalOptionsRoute($prefix, $this->controllerId);
18+
} elseif (isset($this->prefixSettings['namespace']) && str_contains($this->prefixSettings['namespace'], '\modules\\')) { # if `module` not present then check in namespace and then in path
19+
$prefix = static::computeModule('\\', $this->prefixSettings['namespace']);
20+
if ($prefix) {
21+
return static::finalOptionsRoute($prefix, $this->controllerId);
22+
}
23+
} elseif (isset($this->prefixSettings['path']) && str_contains($this->prefixSettings['path'], '/modules/')) {
24+
$prefix = static::computeModule('/', $this->prefixSettings['path']);
25+
if ($prefix) {
26+
return static::finalOptionsRoute($prefix, $this->controllerId);
27+
}
28+
}
29+
}
30+
return $this->controllerId.'/options';
31+
}
32+
33+
/**
34+
* @param string $separator
35+
* @param string $entity path or namespace
36+
* @return void
37+
*/
38+
public static function computeModule(string $separator, string $entity): ?string
39+
{
40+
$parts = explode($separator . 'modules' . $separator, $entity); # /app/modules/forum/controllers => /forum/controllers
41+
if (empty($parts[1])) {
42+
return null;
43+
}
44+
if (str_contains($parts[1], 'controller')) {
45+
$result = explode($separator . 'controller', $parts[1]); // compute everything in between "modules" and "controllers" e.g. api/v1
46+
$result = array_map(function ($val) {
47+
return str_replace('\\', '/', $val);
48+
}, $result);
49+
} else {
50+
$result = explode($separator, $parts[1]); # forum/controllers => forum
51+
}
52+
if (empty($result[0])) {
53+
return null;
54+
}
55+
return $result[0];
56+
}
57+
58+
public static function finalOptionsRoute(string $prefix, string $controllerId): string
59+
{
60+
return trim($prefix, '/') . '/' . $controllerId . '/options';
61+
}
62+
}

src/lib/items/RestAction.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
*/
3232
final class RestAction extends BaseObject
3333
{
34+
use OptionsRoutesTrait;
35+
3436
/**@var string* */
3537
public $id;
3638

@@ -96,16 +98,6 @@ public function getRoute():string
9698
return $this->controllerId . '/' . $this->id;
9799
}
98100

99-
public function getOptionsRoute():string
100-
{
101-
//@TODO: re-check
102-
if ($this->prefix && !empty($this->prefixSettings)) {
103-
$prefix = $this->prefixSettings['module'] ?? $this->prefix;
104-
return trim($prefix, '/').'/'.$this->controllerId.'/options';
105-
}
106-
return $this->controllerId.'/options';
107-
}
108-
109101
public function getBaseModelName():string
110102
{
111103
return $this->modelFqn ? StringHelper::basename($this->modelFqn) : '';

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
3434
foreach ($changed as $attr) {
3535
$newColumn->$attr = $desired->$attr;
3636
}
37-
if (static::isEnum($newColumn)) {
38-
$newColumn->dbType = 'enum'; // TODO this is concretely not correct
39-
}
4037
$this->migration->addUpCode($this->recordBuilder->alterColumn($this->model->getTableAlias(), $newColumn, $positionDesired))
4138
->addDownCode($this->recordBuilder->alterColumn($this->model->getTableAlias(), $current, $positionCurrent));
4239
}
@@ -131,13 +128,12 @@ protected function findTableIndexes():array
131128

132129
public static function getColumnSchemaBuilderClass(): string
133130
{
134-
if (ApiGenerator::isMysql()) {
135-
return \yii\db\mysql\ColumnSchemaBuilder::class;
136-
} elseif (ApiGenerator::isMariaDb()) {
131+
if (ApiGenerator::isMariaDb()) {
137132
return \SamIT\Yii2\MariaDb\ColumnSchemaBuilder::class;
138133
} else {
139134
throw new \Exception('Unknown database');
140135
}
136+
return \yii\db\mysql\ColumnSchemaBuilder::class;
141137
}
142138

143139
public function modifyCurrent(ColumnSchema $current): void
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.yaml',
5+
'generateUrls' => true,
6+
'generateModels' => false,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => true,
11+
'generateMigrations' => false,
12+
'useJsonApi' => false,
13+
'urlPrefixes' => [
14+
'animals' => '',
15+
'/info' => ['module' => 'petinfo', 'namespace' => '\app\modules\petinfo\controllers'],
16+
'/forum' => ['namespace' => '\app\modules\forum\controllers'], # namespace contains "\modules\"
17+
'/forum2' => ['path' => '@app/modules/forum2/controllers', 'namespace' => '\app\forum2\controllers'], # path contains "/modules/"
18+
'/api/v1' => ['path' => '@app/modules/some/controllers', 'namespace' => '\app\api\v1\controllers'], # namespace contains "\modules\"; module will be "api/v1"
19+
'/api/v2' => ['path' => '@app/modules/api/v2/controllers', 'namespace' => '\app\some\controllers'], # namespace contains "\modules\"; module will be "api/v2"
20+
]
21+
];
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/api/v1/pets:
11+
get:
12+
summary: List all pets
13+
operationId: listPets
14+
tags:
15+
- pets
16+
parameters:
17+
- name: limit
18+
in: query
19+
description: How many items to return at one time (max 100)
20+
required: false
21+
schema:
22+
type: integer
23+
format: int32
24+
responses:
25+
'200':
26+
description: A paged array of pets
27+
headers:
28+
x-next:
29+
description: A link to the next page of responses
30+
schema:
31+
type: string
32+
content:
33+
application/json:
34+
schema:
35+
$ref: "#/components/schemas/Pets"
36+
default:
37+
description: unexpected error
38+
content:
39+
application/json:
40+
schema:
41+
$ref: "#/components/schemas/Error"
42+
post:
43+
summary: Create a pet
44+
operationId: createPets
45+
tags:
46+
- pets
47+
responses:
48+
'201':
49+
description: Null response
50+
default:
51+
description: unexpected error
52+
content:
53+
application/json:
54+
schema:
55+
$ref: "#/components/schemas/Error"
56+
/animals/pets/{id}:
57+
parameters:
58+
- name: id
59+
in: path
60+
required: true
61+
description: The id of the pet to update
62+
schema:
63+
type: string
64+
get:
65+
summary: Info for a specific pet
66+
operationId: showPetById
67+
tags:
68+
- pets
69+
responses:
70+
'200':
71+
description: Expected response to a valid request
72+
content:
73+
application/json:
74+
schema:
75+
$ref: "#/components/schemas/Pet"
76+
default:
77+
description: unexpected error
78+
content:
79+
application/json:
80+
schema:
81+
$ref: "#/components/schemas/Error"
82+
patch:
83+
summary: update a specific pet
84+
operationId: updatePetById
85+
tags:
86+
- pets
87+
responses:
88+
'200':
89+
description: The updated pet
90+
content:
91+
application/json:
92+
schema:
93+
$ref: "#/components/schemas/Pet"
94+
delete:
95+
summary: delete a specific pet
96+
operationId: deletePetById
97+
tags:
98+
- pets
99+
responses:
100+
'204':
101+
description: successfully deleted pet
102+
/petComments:
103+
get:
104+
description: list all pet comments
105+
responses:
106+
'200':
107+
description: list of comments
108+
/info/pet-details:
109+
get:
110+
description: list all pet details
111+
responses:
112+
'200':
113+
description: list of details
114+
/forum/pet2-details:
115+
get:
116+
description: list all pet details
117+
responses:
118+
'200':
119+
description: list of details
120+
/forum2/pet3-details:
121+
get:
122+
description: list all pet details
123+
responses:
124+
'200':
125+
description: list of details
126+
/api/v2/comments:
127+
get:
128+
description: list all pet details
129+
responses:
130+
'200':
131+
description: list of details
132+
133+
components:
134+
schemas:
135+
Pet:
136+
description: A Pet
137+
required:
138+
- id
139+
- name
140+
properties:
141+
id:
142+
type: integer
143+
format: int64
144+
readOnly: True
145+
name:
146+
type: string
147+
store:
148+
$ref: '#/components/schemas/Store'
149+
tag:
150+
type: string
151+
x-faker: "$faker->randomElement(['one', 'two', 'three', 'four'])"
152+
Store:
153+
description: A store's description
154+
required:
155+
- id
156+
- name
157+
properties:
158+
id:
159+
type: integer
160+
format: int64
161+
readOnly: True
162+
name:
163+
type: string
164+
Pets:
165+
type: array
166+
items:
167+
$ref: "#/components/schemas/Pet"
168+
Error:
169+
required:
170+
- code
171+
- message
172+
properties:
173+
code:
174+
type: integer
175+
format: int32
176+
message:
177+
type: string

0 commit comments

Comments
 (0)