Skip to content

Commit dff8a03

Browse files
committed
Merge branch 'master' of github.com:php-openapi/yii2-openapi into 68-inverse-relation-must-not-be-generated-for-self-referenced-component-schema
2 parents 3232b89 + ca23577 commit dff8a03

File tree

20 files changed

+650
-22
lines changed

20 files changed

+650
-22
lines changed

src/lib/ColumnToCode.php

-2
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,6 @@ private function getIsBuiltinType($type, $dbType)
400400
private function resolveEnumType():void
401401
{
402402
if (ApiGenerator::isPostgres()) {
403-
// $rawTableName = $this->dbSchema->getRawTableName($this->tableAlias);
404-
// $this->rawParts['type'] = '"enum_'.$rawTableName.'_' . $this->column->name.'"';
405403
$this->rawParts['type'] = '"'.$this->column->dbType.'"';
406404
return;
407405
}

src/lib/items/FractalAction.php

+2-10
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

+62
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

+2-10
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) : '';
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+
];
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* OpenAPI UrlRules
4+
*
5+
* This file is auto generated.
6+
*/
7+
return [
8+
'GET api/v1/pets' => 'api/v1/pet/list',
9+
'POST api/v1/pets' => 'api/v1/pet/create',
10+
'GET animals/pets/<id:[\w-]+>' => 'pet/view',
11+
'DELETE animals/pets/<id:[\w-]+>' => 'pet/delete',
12+
'PATCH animals/pets/<id:[\w-]+>' => 'pet/update',
13+
'GET petComments' => 'pet-comment/list',
14+
'GET info/pet-details' => 'petinfo/pet-detail/list',
15+
'GET forum/pet2-details' => 'forum/pet2-detail/list',
16+
'GET forum2/pet3-details' => 'forum2/pet3-detail/list',
17+
'GET api/v2/comments' => 'api/v2/comment/list',
18+
'api/v1/pets' => 'some/pet/options',
19+
'animals/pets/<id:[\w-]+>' => 'pet/options',
20+
'petComments' => 'pet-comment/options',
21+
'info/pet-details' => 'petinfo/pet-detail/options',
22+
'forum/pet2-details' => 'forum/pet2-detail/options',
23+
'forum2/pet3-details' => 'forum2/pet3-detail/options',
24+
'api/v2/comments' => 'api/v2/comment/options',
25+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace app\controllers;
4+
5+
class PetCommentController extends \app\controllers\base\PetCommentController
6+
{
7+
8+
public function checkAccess($action, $model = null, $params = [])
9+
{
10+
//TODO implement checkAccess
11+
}
12+
13+
public function actionList()
14+
{
15+
//TODO implement actionList
16+
}
17+
18+
19+
}
20+

0 commit comments

Comments
 (0)