forked from cebe/yii2-openapi
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolve TODO: re-check options route in fractal action #35 #36
Merged
cebe
merged 9 commits into
master
from
35-resolve-todo-re-check-options-route-in-fractal-action
Dec 11, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4f9e8d1
Initial commit
SOHELAHMED7 df315d4
Detect module from namespace or path
SOHELAHMED7 bbefd8e
Fix TODO
SOHELAHMED7 3a44f9b
Refactor and add test for RestAction
SOHELAHMED7 f3d71e7
Complete tests
SOHELAHMED7 689d98a
Delete TODO.taskpaper file
SOHELAHMED7 2424d1a
Merge branches 'master' and '35-resolve-todo-re-check-options-route-i…
SOHELAHMED7 3592e97
Merge branches 'master' and '35-resolve-todo-re-check-options-route-i…
SOHELAHMED7 d649920
Remove redundant code
SOHELAHMED7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (c) 2018 Carsten Brandt <[email protected]> and contributors | ||
* @license https://github.com/cebe/yii2-openapi/blob/master/LICENSE | ||
*/ | ||
|
||
namespace cebe\yii2openapi\lib\items; | ||
|
||
trait OptionsRoutesTrait | ||
{ | ||
public function getOptionsRoute():string | ||
{ | ||
if ($this->prefix && !empty($this->prefixSettings)) { | ||
if (isset($this->prefixSettings['module'])) { | ||
$prefix = $this->prefixSettings['module']; | ||
return static::finalOptionsRoute($prefix, $this->controllerId); | ||
} elseif (isset($this->prefixSettings['namespace']) && str_contains($this->prefixSettings['namespace'], '\modules\\')) { # if `module` not present then check in namespace and then in path | ||
$prefix = static::computeModule('\\', $this->prefixSettings['namespace']); | ||
if ($prefix) { | ||
return static::finalOptionsRoute($prefix, $this->controllerId); | ||
} | ||
} elseif (isset($this->prefixSettings['path']) && str_contains($this->prefixSettings['path'], '/modules/')) { | ||
$prefix = static::computeModule('/', $this->prefixSettings['path']); | ||
if ($prefix) { | ||
return static::finalOptionsRoute($prefix, $this->controllerId); | ||
} | ||
} | ||
} | ||
return $this->controllerId.'/options'; | ||
} | ||
|
||
/** | ||
* @param string $separator | ||
* @param string $entity path or namespace | ||
* @return void | ||
*/ | ||
public static function computeModule(string $separator, string $entity): ?string | ||
{ | ||
$parts = explode($separator . 'modules' . $separator, $entity); # /app/modules/forum/controllers => /forum/controllers | ||
if (empty($parts[1])) { | ||
return null; | ||
} | ||
if (str_contains($parts[1], 'controller')) { | ||
$result = explode($separator . 'controller', $parts[1]); // compute everything in between "modules" and "controllers" e.g. api/v1 | ||
$result = array_map(function ($val) { | ||
return str_replace('\\', '/', $val); | ||
}, $result); | ||
} else { | ||
$result = explode($separator, $parts[1]); # forum/controllers => forum | ||
} | ||
if (empty($result[0])) { | ||
return null; | ||
} | ||
return $result[0]; | ||
} | ||
|
||
public static function finalOptionsRoute(string $prefix, string $controllerId): string | ||
{ | ||
return trim($prefix, '/') . '/' . $controllerId . '/options'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
return [ | ||
'openApiPath' => '@specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.yaml', | ||
'generateUrls' => true, | ||
'generateModels' => false, | ||
'excludeModels' => [ | ||
'Error', | ||
], | ||
'generateControllers' => true, | ||
'generateMigrations' => false, | ||
'useJsonApi' => false, | ||
'urlPrefixes' => [ | ||
'animals' => '', | ||
'/info' => ['module' => 'petinfo', 'namespace' => '\app\modules\petinfo\controllers'], | ||
'/forum' => ['namespace' => '\app\modules\forum\controllers'], # namespace contains "\modules\" | ||
'/forum2' => ['path' => '@app/modules/forum2/controllers', 'namespace' => '\app\forum2\controllers'], # path contains "/modules/" | ||
'/api/v1' => ['path' => '@app/modules/some/controllers', 'namespace' => '\app\api\v1\controllers'], # namespace contains "\modules\"; module will be "api/v1" | ||
'/api/v2' => ['path' => '@app/modules/api/v2/controllers', 'namespace' => '\app\some\controllers'], # namespace contains "\modules\"; module will be "api/v2" | ||
] | ||
]; |
177 changes: 177 additions & 0 deletions
177
tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
openapi: "3.0.0" | ||
info: | ||
version: 1.0.0 | ||
title: Swagger Petstore | ||
license: | ||
name: MIT | ||
servers: | ||
- url: http://petstore.swagger.io/v1 | ||
paths: | ||
/api/v1/pets: | ||
get: | ||
summary: List all pets | ||
operationId: listPets | ||
tags: | ||
- pets | ||
parameters: | ||
- name: limit | ||
in: query | ||
description: How many items to return at one time (max 100) | ||
required: false | ||
schema: | ||
type: integer | ||
format: int32 | ||
responses: | ||
'200': | ||
description: A paged array of pets | ||
headers: | ||
x-next: | ||
description: A link to the next page of responses | ||
schema: | ||
type: string | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Pets" | ||
default: | ||
description: unexpected error | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Error" | ||
post: | ||
summary: Create a pet | ||
operationId: createPets | ||
tags: | ||
- pets | ||
responses: | ||
'201': | ||
description: Null response | ||
default: | ||
description: unexpected error | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Error" | ||
/animals/pets/{id}: | ||
parameters: | ||
- name: id | ||
in: path | ||
required: true | ||
description: The id of the pet to update | ||
schema: | ||
type: string | ||
get: | ||
summary: Info for a specific pet | ||
operationId: showPetById | ||
tags: | ||
- pets | ||
responses: | ||
'200': | ||
description: Expected response to a valid request | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Pet" | ||
default: | ||
description: unexpected error | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Error" | ||
patch: | ||
summary: update a specific pet | ||
operationId: updatePetById | ||
tags: | ||
- pets | ||
responses: | ||
'200': | ||
description: The updated pet | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Pet" | ||
delete: | ||
summary: delete a specific pet | ||
operationId: deletePetById | ||
tags: | ||
- pets | ||
responses: | ||
'204': | ||
description: successfully deleted pet | ||
/petComments: | ||
get: | ||
description: list all pet comments | ||
responses: | ||
'200': | ||
description: list of comments | ||
/info/pet-details: | ||
get: | ||
description: list all pet details | ||
responses: | ||
'200': | ||
description: list of details | ||
/forum/pet2-details: | ||
get: | ||
description: list all pet details | ||
responses: | ||
'200': | ||
description: list of details | ||
/forum2/pet3-details: | ||
get: | ||
description: list all pet details | ||
responses: | ||
'200': | ||
description: list of details | ||
/api/v2/comments: | ||
get: | ||
description: list all pet details | ||
responses: | ||
'200': | ||
description: list of details | ||
|
||
components: | ||
schemas: | ||
Pet: | ||
description: A Pet | ||
required: | ||
- id | ||
- name | ||
properties: | ||
id: | ||
type: integer | ||
format: int64 | ||
readOnly: True | ||
name: | ||
type: string | ||
store: | ||
$ref: '#/components/schemas/Store' | ||
tag: | ||
type: string | ||
x-faker: "$faker->randomElement(['one', 'two', 'three', 'four'])" | ||
Store: | ||
description: A store's description | ||
required: | ||
- id | ||
- name | ||
properties: | ||
id: | ||
type: integer | ||
format: int64 | ||
readOnly: True | ||
name: | ||
type: string | ||
Pets: | ||
type: array | ||
items: | ||
$ref: "#/components/schemas/Pet" | ||
Error: | ||
required: | ||
- code | ||
- message | ||
properties: | ||
code: | ||
type: integer | ||
format: int32 | ||
message: | ||
type: string |
25 changes: 25 additions & 0 deletions
25
...e_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/config/urls.rest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
/** | ||
* OpenAPI UrlRules | ||
* | ||
* This file is auto generated. | ||
*/ | ||
return [ | ||
'GET api/v1/pets' => 'api/v1/pet/list', | ||
'POST api/v1/pets' => 'api/v1/pet/create', | ||
'GET animals/pets/<id:[\w-]+>' => 'pet/view', | ||
'DELETE animals/pets/<id:[\w-]+>' => 'pet/delete', | ||
'PATCH animals/pets/<id:[\w-]+>' => 'pet/update', | ||
'GET petComments' => 'pet-comment/list', | ||
'GET info/pet-details' => 'petinfo/pet-detail/list', | ||
'GET forum/pet2-details' => 'forum/pet2-detail/list', | ||
'GET forum2/pet3-details' => 'forum2/pet3-detail/list', | ||
'GET api/v2/comments' => 'api/v2/comment/list', | ||
'api/v1/pets' => 'some/pet/options', | ||
'animals/pets/<id:[\w-]+>' => 'pet/options', | ||
'petComments' => 'pet-comment/options', | ||
'info/pet-details' => 'petinfo/pet-detail/options', | ||
'forum/pet2-details' => 'forum/pet2-detail/options', | ||
'forum2/pet3-details' => 'forum2/pet3-detail/options', | ||
'api/v2/comments' => 'api/v2/comment/options', | ||
]; |
20 changes: 20 additions & 0 deletions
20
..._todo_re_check_options_route_in_fractal_action/mysql/controllers/PetCommentController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace app\controllers; | ||
|
||
class PetCommentController extends \app\controllers\base\PetCommentController | ||
{ | ||
|
||
public function checkAccess($action, $model = null, $params = []) | ||
{ | ||
//TODO implement checkAccess | ||
} | ||
|
||
public function actionList() | ||
{ | ||
//TODO implement actionList | ||
} | ||
|
||
|
||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do auto generated rules contain module paths? I'd expect only controller/action here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is due to
urlPrefixes
yii2-openapi/src/generator/ApiGenerator.php
Line 77 in 592b309
It is configured here in
yii2-openapi/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php
Line 13 in 3a44f9b