From 4f9e8d16c042451429e35b9b9b79fb7c65951f97 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 28 Aug 2024 20:25:49 +0530 Subject: [PATCH 01/15] Initial commit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1cd2090b..3bb79307 100644 --- a/README.md +++ b/README.md @@ -568,3 +568,4 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + From df315d45fa1200ff03afe0dff7da22369fec7548 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 30 Aug 2024 20:29:51 +0530 Subject: [PATCH 02/15] Detect module from namespace or path --- README.md | 1 - TODO.taskpaper | 10 +++++++ src/lib/items/FractalAction.php | 42 +++++++++++++++++++++++++-- tests/specs/petstore_urlprefixes.php | 3 ++ tests/specs/petstore_urlprefixes.yaml | 12 ++++++++ tests/unit/IssueFixTest.php | 15 ++++++++++ 6 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 TODO.taskpaper diff --git a/README.md b/README.md index 3bb79307..1cd2090b 100644 --- a/README.md +++ b/README.md @@ -568,4 +568,3 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). - diff --git a/TODO.taskpaper b/TODO.taskpaper new file mode 100644 index 00000000..5562aa55 --- /dev/null +++ b/TODO.taskpaper @@ -0,0 +1,10 @@ +TODO.taskpaper + +### Add validation rules by attribute name or pattern #30 + ☐ Re-check + ☐ create failing test + ☐ implement the solution + ☐ fix failing tests if any + ☐ resolve TODOs if any + ☐ review PR + ☐ delete this file and submit PR diff --git a/src/lib/items/FractalAction.php b/src/lib/items/FractalAction.php index 148ac191..bb0e0f4d 100644 --- a/src/lib/items/FractalAction.php +++ b/src/lib/items/FractalAction.php @@ -106,8 +106,21 @@ public function getOptionsRoute():string { //@TODO: re-check if ($this->prefix && !empty($this->prefixSettings)) { - $prefix = $this->prefixSettings['module'] ?? $this->prefix; - return trim($prefix, '/').'/'.$this->controllerId.'/options'; +// $prefix = $this->prefixSettings['module'] ?? $this->prefix; + if (isset($this->prefixSettings['module'])) { + $prefix = $this->prefixSettings['module']; + return trim($prefix, '/') . '/' . $this->controllerId . '/options'; + } elseif (isset($this->prefixSettings['namespace']) && str_contains($this->prefixSettings['namespace'], '\modules\\')) { + $prefix = static::computeModule('\\', $this->prefixSettings['namespace']); + if ($prefix) { + return trim($prefix, '/') . '/' . $this->controllerId . '/options'; + } + } elseif (isset($this->prefixSettings['path']) && str_contains($this->prefixSettings['path'], '/modules/')) { + $prefix = static::computeModule('/', $this->prefixSettings['path']); + if ($prefix) { + return trim($prefix, '/') . '/' . $this->controllerId . '/options'; + } + } } return $this->controllerId.'/options'; } @@ -251,4 +264,29 @@ public function getIdParamType(): string } return $this->params[$this->idParam]['type'] === 'integer' ? 'int' : 'string'; } + + /** + * @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); + if (empty($parts[1])) { + return null; + } + if (str_contains($parts[1], 'controller')) { + $result = explode($separator . 'controller', $parts[1]); + $result = array_map(function ($val) { + return str_replace('\\', '/', $val); + }, $result); + } else { + $result = explode($separator, $parts[1]); + } + if (empty($result[0])) { + return null; + } + return $result[0]; + } } diff --git a/tests/specs/petstore_urlprefixes.php b/tests/specs/petstore_urlprefixes.php index 8041c1fe..f14c4bca 100644 --- a/tests/specs/petstore_urlprefixes.php +++ b/tests/specs/petstore_urlprefixes.php @@ -9,9 +9,12 @@ ], 'generateControllers' => true, 'generateMigrations' => false, + 'useJsonApi' => true, 'urlPrefixes' => [ 'animals' => '', '/info' => ['module' =>'petinfo','namespace' => '\app\modules\petinfo\controllers'], + '/fgh' => ['namespace' => '\app\modules\fgh\controllers'], + '/fgh2' => ['path' => '@app/modules/fgh2/controllers', 'namespace' => '\app\fgh2\controllers'], '/api/v1' => ['path' => '@app/modules/api/v1/controllers', 'namespace' => '\app\api\v1\controllers'] ] ]; diff --git a/tests/specs/petstore_urlprefixes.yaml b/tests/specs/petstore_urlprefixes.yaml index 98701d27..7a3a549a 100644 --- a/tests/specs/petstore_urlprefixes.yaml +++ b/tests/specs/petstore_urlprefixes.yaml @@ -111,6 +111,18 @@ paths: responses: '200': description: list of details + /fgh/pet-detail2s: + get: + description: list all pet details + responses: + '200': + description: list of details + /fgh2/pet-detail3s: + get: + description: list all pet details + responses: + '200': + description: list of details components: schemas: diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index b6c7abdb..6f5fa668 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -360,4 +360,19 @@ public function test158BugGiiapiGeneratedRulesEnumWithTrim() ]); $this->checkFiles($actualFiles, $expectedFiles); } + + + public function testRecheckFractalActionsOptionsRoute() + { + $this->changeDbToMariadb(); + $testFile = Yii::getAlias("@specs/petstore_urlprefixes.php"); + $this->runGenerator($testFile); +// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ +// 'recursive' => true, +// ]); +// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/maria"), [ +// 'recursive' => true, +// ]); +// $this->checkFiles($actualFiles, $expectedFiles); + } } From bbefd8efde3527ec6ccb3f8f6be9a07b50ee46d1 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 3 Sep 2024 16:14:38 +0530 Subject: [PATCH 03/15] Fix TODO --- src/lib/items/FractalAction.php | 13 +- .../index.php | 21 +++ .../index.yaml | 177 ++++++++++++++++++ tests/specs/petstore_urlprefixes.php | 3 - tests/specs/petstore_urlprefixes.yaml | 12 -- tests/unit/IssueFixTest.php | 8 +- 6 files changed, 210 insertions(+), 24 deletions(-) create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.yaml diff --git a/src/lib/items/FractalAction.php b/src/lib/items/FractalAction.php index bb0e0f4d..40101e99 100644 --- a/src/lib/items/FractalAction.php +++ b/src/lib/items/FractalAction.php @@ -104,21 +104,19 @@ public function getRoute():string public function getOptionsRoute():string { - //@TODO: re-check if ($this->prefix && !empty($this->prefixSettings)) { -// $prefix = $this->prefixSettings['module'] ?? $this->prefix; if (isset($this->prefixSettings['module'])) { $prefix = $this->prefixSettings['module']; - return trim($prefix, '/') . '/' . $this->controllerId . '/options'; + return static::finalOptionsRoute($prefix, $this->controllerId); } elseif (isset($this->prefixSettings['namespace']) && str_contains($this->prefixSettings['namespace'], '\modules\\')) { $prefix = static::computeModule('\\', $this->prefixSettings['namespace']); if ($prefix) { - return trim($prefix, '/') . '/' . $this->controllerId . '/options'; + 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 trim($prefix, '/') . '/' . $this->controllerId . '/options'; + return static::finalOptionsRoute($prefix, $this->controllerId); } } } @@ -289,4 +287,9 @@ public static function computeModule(string $separator, string $entity): ?string } return $result[0]; } + + public static function finalOptionsRoute(string $prefix, string $controllerId): string + { + return trim($prefix, '/') . '/' . $controllerId . '/options'; + } } diff --git a/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php new file mode 100644 index 00000000..3c4da05f --- /dev/null +++ b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php @@ -0,0 +1,21 @@ + '@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' => true, + '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" + ] +]; diff --git a/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.yaml b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.yaml new file mode 100644 index 00000000..5dd945dd --- /dev/null +++ b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.yaml @@ -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 diff --git a/tests/specs/petstore_urlprefixes.php b/tests/specs/petstore_urlprefixes.php index f14c4bca..8041c1fe 100644 --- a/tests/specs/petstore_urlprefixes.php +++ b/tests/specs/petstore_urlprefixes.php @@ -9,12 +9,9 @@ ], 'generateControllers' => true, 'generateMigrations' => false, - 'useJsonApi' => true, 'urlPrefixes' => [ 'animals' => '', '/info' => ['module' =>'petinfo','namespace' => '\app\modules\petinfo\controllers'], - '/fgh' => ['namespace' => '\app\modules\fgh\controllers'], - '/fgh2' => ['path' => '@app/modules/fgh2/controllers', 'namespace' => '\app\fgh2\controllers'], '/api/v1' => ['path' => '@app/modules/api/v1/controllers', 'namespace' => '\app\api\v1\controllers'] ] ]; diff --git a/tests/specs/petstore_urlprefixes.yaml b/tests/specs/petstore_urlprefixes.yaml index 7a3a549a..98701d27 100644 --- a/tests/specs/petstore_urlprefixes.yaml +++ b/tests/specs/petstore_urlprefixes.yaml @@ -111,18 +111,6 @@ paths: responses: '200': description: list of details - /fgh/pet-detail2s: - get: - description: list all pet details - responses: - '200': - description: list of details - /fgh2/pet-detail3s: - get: - description: list all pet details - responses: - '200': - description: list of details components: schemas: diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 6f5fa668..d53ba5c5 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -362,15 +362,15 @@ public function test158BugGiiapiGeneratedRulesEnumWithTrim() } - public function testRecheckFractalActionsOptionsRoute() + // https://github.com/php-openapi/yii2-openapi/issues/35 + public function test35ResolveTodoReCheckOptionsRouteInFractalAction() { - $this->changeDbToMariadb(); - $testFile = Yii::getAlias("@specs/petstore_urlprefixes.php"); + $testFile = Yii::getAlias("@specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php"); $this->runGenerator($testFile); // $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ // 'recursive' => true, // ]); -// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/maria"), [ +// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/mysql"), [ // 'recursive' => true, // ]); // $this->checkFiles($actualFiles, $expectedFiles); From 3a44f9bb3c912739f72385cc44ade67e7aa3e77f Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 3 Sep 2024 16:45:01 +0530 Subject: [PATCH 04/15] Refactor and add test for RestAction --- src/lib/items/FractalAction.php | 53 +--------------- src/lib/items/OptionsRouteTrait.php | 62 +++++++++++++++++++ src/lib/items/RestAction.php | 12 +--- .../index.php | 2 +- tests/unit/IssueFixTest.php | 16 +++++ 5 files changed, 83 insertions(+), 62 deletions(-) create mode 100644 src/lib/items/OptionsRouteTrait.php diff --git a/src/lib/items/FractalAction.php b/src/lib/items/FractalAction.php index 40101e99..9bfba221 100644 --- a/src/lib/items/FractalAction.php +++ b/src/lib/items/FractalAction.php @@ -32,6 +32,8 @@ */ final class FractalAction extends BaseObject { + use OptionsRouteTrait; + /**@var string* */ public $id; @@ -102,27 +104,6 @@ public function getRoute():string return $this->controllerId.'/'.$this->id; } - 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\\')) { - $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'; - } - public function getBaseModelName():string { return $this->modelFqn ? StringHelper::basename($this->modelFqn) : ''; @@ -262,34 +243,4 @@ public function getIdParamType(): string } return $this->params[$this->idParam]['type'] === 'integer' ? 'int' : 'string'; } - - /** - * @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); - if (empty($parts[1])) { - return null; - } - if (str_contains($parts[1], 'controller')) { - $result = explode($separator . 'controller', $parts[1]); - $result = array_map(function ($val) { - return str_replace('\\', '/', $val); - }, $result); - } else { - $result = explode($separator, $parts[1]); - } - if (empty($result[0])) { - return null; - } - return $result[0]; - } - - public static function finalOptionsRoute(string $prefix, string $controllerId): string - { - return trim($prefix, '/') . '/' . $controllerId . '/options'; - } } diff --git a/src/lib/items/OptionsRouteTrait.php b/src/lib/items/OptionsRouteTrait.php new file mode 100644 index 00000000..3d23b37c --- /dev/null +++ b/src/lib/items/OptionsRouteTrait.php @@ -0,0 +1,62 @@ + and contributors + * @license https://github.com/cebe/yii2-openapi/blob/master/LICENSE + */ + +namespace cebe\yii2openapi\lib\items; + +trait OptionsRouteTrait +{ + 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\\')) { + $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'; + } +} diff --git a/src/lib/items/RestAction.php b/src/lib/items/RestAction.php index 6852fafc..edd96025 100644 --- a/src/lib/items/RestAction.php +++ b/src/lib/items/RestAction.php @@ -31,6 +31,8 @@ */ final class RestAction extends BaseObject { + use OptionsRouteTrait; + /**@var string* */ public $id; @@ -77,16 +79,6 @@ public function getRoute():string return $this->controllerId.'/'.$this->id; } - public function getOptionsRoute():string - { - //@TODO: re-check - if ($this->prefix && !empty($this->prefixSettings)) { - $prefix = $this->prefixSettings['module'] ?? $this->prefix; - return trim($prefix, '/').'/'.$this->controllerId.'/options'; - } - return $this->controllerId.'/options'; - } - public function getBaseModelName():string { return $this->modelFqn ? StringHelper::basename($this->modelFqn) : ''; diff --git a/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php index 3c4da05f..f4e7c94b 100644 --- a/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php +++ b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php @@ -9,7 +9,7 @@ ], 'generateControllers' => true, 'generateMigrations' => false, - 'useJsonApi' => true, + 'useJsonApi' => false, 'urlPrefixes' => [ 'animals' => '', '/info' => ['module' => 'petinfo', 'namespace' => '\app\modules\petinfo\controllers'], diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index d53ba5c5..f7843ed2 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -373,6 +373,22 @@ public function test35ResolveTodoReCheckOptionsRouteInFractalAction() // $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/mysql"), [ // 'recursive' => true, // ]); +// $this->checkFiles($actualFiles, $expectedFiles); + } + + // https://github.com/php-openapi/yii2-openapi/issues/35 + public function test35ResolveTodoReCheckOptionsRouteInRestAction() + { + $testFile = Yii::getAlias("@specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php"); + $content = str_replace("'useJsonApi' => true,", "'useJsonApi' => false,", file_get_contents($testFile)); + file_put_contents($testFile, $content); + $this->runGenerator($testFile); +// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ +// 'recursive' => true, +// ]); +// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/mysql"), [ +// 'recursive' => true, +// ]); // $this->checkFiles($actualFiles, $expectedFiles); } } From f3d71e7c39a1ab6a509a86d97cce6793fb7fcefd Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 3 Sep 2024 16:55:22 +0530 Subject: [PATCH 05/15] Complete tests --- TODO.taskpaper | 12 ++-- src/lib/items/FractalAction.php | 2 +- ...sRouteTrait.php => OptionsRoutesTrait.php} | 4 +- src/lib/items/RestAction.php | 2 +- .../mysql/config/urls.rest.php | 25 +++++++++ .../controllers/PetCommentController.php | 20 +++++++ .../controllers/base/PetCommentController.php | 32 +++++++++++ .../api/v2/controllers/CommentController.php | 20 +++++++ .../v2/controllers/base/CommentController.php | 32 +++++++++++ .../controllers/Pet2DetailController.php | 20 +++++++ .../controllers/base/Pet2DetailController.php | 32 +++++++++++ .../controllers/Pet3DetailController.php | 20 +++++++ .../controllers/base/Pet3DetailController.php | 32 +++++++++++ .../controllers/PetDetailController.php | 20 +++++++ .../controllers/base/PetDetailController.php | 32 +++++++++++ .../some/controllers/PetController.php | 15 +++++ .../some/controllers/base/PetController.php | 55 +++++++++++++++++++ tests/unit/IssueFixTest.php | 28 +++++----- 18 files changed, 379 insertions(+), 24 deletions(-) rename src/lib/items/{OptionsRouteTrait.php => OptionsRoutesTrait.php} (96%) create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/config/urls.rest.php create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/controllers/PetCommentController.php create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/controllers/base/PetCommentController.php create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/api/v2/controllers/CommentController.php create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/api/v2/controllers/base/CommentController.php create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/forum/controllers/Pet2DetailController.php create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/forum/controllers/base/Pet2DetailController.php create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/forum2/controllers/Pet3DetailController.php create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/forum2/controllers/base/Pet3DetailController.php create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/petinfo/controllers/PetDetailController.php create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/petinfo/controllers/base/PetDetailController.php create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/some/controllers/PetController.php create mode 100644 tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/some/controllers/base/PetController.php diff --git a/TODO.taskpaper b/TODO.taskpaper index 5562aa55..f91a3657 100644 --- a/TODO.taskpaper +++ b/TODO.taskpaper @@ -1,10 +1,10 @@ TODO.taskpaper ### Add validation rules by attribute name or pattern #30 - ☐ Re-check - ☐ create failing test - ☐ implement the solution - ☐ fix failing tests if any - ☐ resolve TODOs if any - ☐ review PR + ✔ Re-check @done (24-09-03 16:48) + ✔ create failing test @done (24-09-03 16:48) + ✔ implement the solution @done (24-09-03 16:48) + ✔ fix failing tests if any @done (24-09-03 16:48) + ✔ resolve TODOs if any @done (24-09-03 16:55) + ✔ review PR @done (24-09-03 16:55) ☐ delete this file and submit PR diff --git a/src/lib/items/FractalAction.php b/src/lib/items/FractalAction.php index 9bfba221..7e824c19 100644 --- a/src/lib/items/FractalAction.php +++ b/src/lib/items/FractalAction.php @@ -32,7 +32,7 @@ */ final class FractalAction extends BaseObject { - use OptionsRouteTrait; + use OptionsRoutesTrait; /**@var string* */ public $id; diff --git a/src/lib/items/OptionsRouteTrait.php b/src/lib/items/OptionsRoutesTrait.php similarity index 96% rename from src/lib/items/OptionsRouteTrait.php rename to src/lib/items/OptionsRoutesTrait.php index 3d23b37c..df9e6714 100644 --- a/src/lib/items/OptionsRouteTrait.php +++ b/src/lib/items/OptionsRoutesTrait.php @@ -7,7 +7,7 @@ namespace cebe\yii2openapi\lib\items; -trait OptionsRouteTrait +trait OptionsRoutesTrait { public function getOptionsRoute():string { @@ -15,7 +15,7 @@ public function getOptionsRoute():string 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\\')) { + } 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); diff --git a/src/lib/items/RestAction.php b/src/lib/items/RestAction.php index edd96025..0ef5dcc1 100644 --- a/src/lib/items/RestAction.php +++ b/src/lib/items/RestAction.php @@ -31,7 +31,7 @@ */ final class RestAction extends BaseObject { - use OptionsRouteTrait; + use OptionsRoutesTrait; /**@var string* */ public $id; diff --git a/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/config/urls.rest.php b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/config/urls.rest.php new file mode 100644 index 00000000..fb1998ac --- /dev/null +++ b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/config/urls.rest.php @@ -0,0 +1,25 @@ + 'api/v1/pet/list', + 'POST api/v1/pets' => 'api/v1/pet/create', + 'GET animals/pets/' => 'pet/view', + 'DELETE animals/pets/' => 'pet/delete', + 'PATCH animals/pets/' => '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/' => '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', +]; diff --git a/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/controllers/PetCommentController.php b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/controllers/PetCommentController.php new file mode 100644 index 00000000..b71a5cb0 --- /dev/null +++ b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/controllers/PetCommentController.php @@ -0,0 +1,20 @@ + [ + 'class' => \yii\rest\OptionsAction::class, + ], + ]; + } + + /** + * Checks the privilege of the current user. + * + * This method checks whether the current user has the privilege + * to run the specified action against the specified data model. + * If the user does not have access, a [[ForbiddenHttpException]] should be thrown. + * + * @param string $action the ID of the action to be executed + * @param object $model the model to be accessed. If null, it means no specific model is being accessed. + * @param array $params additional parameters + * @throws \yii\web\ForbiddenHttpException if the user does not have access + */ + abstract public function checkAccess($action, $model = null, $params = []); + + abstract public function actionList(); + +} diff --git a/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/api/v2/controllers/CommentController.php b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/api/v2/controllers/CommentController.php new file mode 100644 index 00000000..55b0bc54 --- /dev/null +++ b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/api/v2/controllers/CommentController.php @@ -0,0 +1,20 @@ + [ + 'class' => \yii\rest\OptionsAction::class, + ], + ]; + } + + /** + * Checks the privilege of the current user. + * + * This method checks whether the current user has the privilege + * to run the specified action against the specified data model. + * If the user does not have access, a [[ForbiddenHttpException]] should be thrown. + * + * @param string $action the ID of the action to be executed + * @param object $model the model to be accessed. If null, it means no specific model is being accessed. + * @param array $params additional parameters + * @throws \yii\web\ForbiddenHttpException if the user does not have access + */ + abstract public function checkAccess($action, $model = null, $params = []); + + abstract public function actionList(); + +} diff --git a/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/forum/controllers/Pet2DetailController.php b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/forum/controllers/Pet2DetailController.php new file mode 100644 index 00000000..550fa36d --- /dev/null +++ b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/forum/controllers/Pet2DetailController.php @@ -0,0 +1,20 @@ + [ + 'class' => \yii\rest\OptionsAction::class, + ], + ]; + } + + /** + * Checks the privilege of the current user. + * + * This method checks whether the current user has the privilege + * to run the specified action against the specified data model. + * If the user does not have access, a [[ForbiddenHttpException]] should be thrown. + * + * @param string $action the ID of the action to be executed + * @param object $model the model to be accessed. If null, it means no specific model is being accessed. + * @param array $params additional parameters + * @throws \yii\web\ForbiddenHttpException if the user does not have access + */ + abstract public function checkAccess($action, $model = null, $params = []); + + abstract public function actionList(); + +} diff --git a/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/forum2/controllers/Pet3DetailController.php b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/forum2/controllers/Pet3DetailController.php new file mode 100644 index 00000000..9fbebaf9 --- /dev/null +++ b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/forum2/controllers/Pet3DetailController.php @@ -0,0 +1,20 @@ + [ + 'class' => \yii\rest\OptionsAction::class, + ], + ]; + } + + /** + * Checks the privilege of the current user. + * + * This method checks whether the current user has the privilege + * to run the specified action against the specified data model. + * If the user does not have access, a [[ForbiddenHttpException]] should be thrown. + * + * @param string $action the ID of the action to be executed + * @param object $model the model to be accessed. If null, it means no specific model is being accessed. + * @param array $params additional parameters + * @throws \yii\web\ForbiddenHttpException if the user does not have access + */ + abstract public function checkAccess($action, $model = null, $params = []); + + abstract public function actionList(); + +} diff --git a/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/petinfo/controllers/PetDetailController.php b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/petinfo/controllers/PetDetailController.php new file mode 100644 index 00000000..8f574d21 --- /dev/null +++ b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/petinfo/controllers/PetDetailController.php @@ -0,0 +1,20 @@ + [ + 'class' => \yii\rest\OptionsAction::class, + ], + ]; + } + + /** + * Checks the privilege of the current user. + * + * This method checks whether the current user has the privilege + * to run the specified action against the specified data model. + * If the user does not have access, a [[ForbiddenHttpException]] should be thrown. + * + * @param string $action the ID of the action to be executed + * @param object $model the model to be accessed. If null, it means no specific model is being accessed. + * @param array $params additional parameters + * @throws \yii\web\ForbiddenHttpException if the user does not have access + */ + abstract public function checkAccess($action, $model = null, $params = []); + + abstract public function actionList(); + +} diff --git a/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/some/controllers/PetController.php b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/some/controllers/PetController.php new file mode 100644 index 00000000..729402bc --- /dev/null +++ b/tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql/modules/some/controllers/PetController.php @@ -0,0 +1,15 @@ + [ + 'class' => \yii\rest\IndexAction::class, + 'modelClass' => \app\models\Pet::class, + 'checkAccess' => [$this, 'checkAccess'], + ], + 'create' => [ + 'class' => \yii\rest\CreateAction::class, + 'modelClass' => \app\models\Pet::class, + 'checkAccess' => [$this, 'checkAccess'], + ], + 'view' => [ + 'class' => \yii\rest\ViewAction::class, + 'modelClass' => \app\models\Pet::class, + 'checkAccess' => [$this, 'checkAccess'], + ], + 'delete' => [ + 'class' => \yii\rest\DeleteAction::class, + 'modelClass' => \app\models\Pet::class, + 'checkAccess' => [$this, 'checkAccess'], + ], + 'update' => [ + 'class' => \yii\rest\UpdateAction::class, + 'modelClass' => \app\models\Pet::class, + 'checkAccess' => [$this, 'checkAccess'], + ], + 'options' => [ + 'class' => \yii\rest\OptionsAction::class, + ], + ]; + } + + /** + * Checks the privilege of the current user. + * + * This method checks whether the current user has the privilege + * to run the specified action against the specified data model. + * If the user does not have access, a [[ForbiddenHttpException]] should be thrown. + * + * @param string $action the ID of the action to be executed + * @param object $model the model to be accessed. If null, it means no specific model is being accessed. + * @param array $params additional parameters + * @throws \yii\web\ForbiddenHttpException if the user does not have access + */ + abstract public function checkAccess($action, $model = null, $params = []); + +} diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index f7843ed2..38bd28b3 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -367,13 +367,13 @@ public function test35ResolveTodoReCheckOptionsRouteInFractalAction() { $testFile = Yii::getAlias("@specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php"); $this->runGenerator($testFile); -// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ -// 'recursive' => true, -// ]); -// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/mysql"), [ -// 'recursive' => true, -// ]); -// $this->checkFiles($actualFiles, $expectedFiles); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); } // https://github.com/php-openapi/yii2-openapi/issues/35 @@ -383,12 +383,12 @@ public function test35ResolveTodoReCheckOptionsRouteInRestAction() $content = str_replace("'useJsonApi' => true,", "'useJsonApi' => false,", file_get_contents($testFile)); file_put_contents($testFile, $content); $this->runGenerator($testFile); -// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ -// 'recursive' => true, -// ]); -// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/mysql"), [ -// 'recursive' => true, -// ]); -// $this->checkFiles($actualFiles, $expectedFiles); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); } } From 689d98a8aaa1ae100dccf0be6f46d46549d39457 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 3 Sep 2024 16:56:00 +0530 Subject: [PATCH 06/15] Delete TODO.taskpaper file --- TODO.taskpaper | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 TODO.taskpaper diff --git a/TODO.taskpaper b/TODO.taskpaper deleted file mode 100644 index f91a3657..00000000 --- a/TODO.taskpaper +++ /dev/null @@ -1,10 +0,0 @@ -TODO.taskpaper - -### Add validation rules by attribute name or pattern #30 - ✔ Re-check @done (24-09-03 16:48) - ✔ create failing test @done (24-09-03 16:48) - ✔ implement the solution @done (24-09-03 16:48) - ✔ fix failing tests if any @done (24-09-03 16:48) - ✔ resolve TODOs if any @done (24-09-03 16:55) - ✔ review PR @done (24-09-03 16:55) - ☐ delete this file and submit PR From 2e9d39fe72dd4316eca3286ca36b3f30019ccb0d Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 6 Sep 2024 09:20:59 +0530 Subject: [PATCH 07/15] Create PR --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1cd2090b..3bb79307 100644 --- a/README.md +++ b/README.md @@ -568,3 +568,4 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + From 433984836b40779e61cbb9f840e12a428eef5773 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 6 Sep 2024 09:41:29 +0530 Subject: [PATCH 08/15] Fix issue of inconsistent return value --- README.md | 1 - src/lib/migrations/MysqlMigrationBuilder.php | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3bb79307..1cd2090b 100644 --- a/README.md +++ b/README.md @@ -568,4 +568,3 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). - diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index 42354df3..359c1af1 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -117,11 +117,10 @@ protected function findTableIndexes():array public static function getColumnSchemaBuilderClass(): string { - if (ApiGenerator::isMysql()) { - return \yii\db\mysql\ColumnSchemaBuilder::class; - } elseif (ApiGenerator::isMariaDb()) { + if (ApiGenerator::isMariaDb()) { return \SamIT\Yii2\MariaDb\ColumnSchemaBuilder::class; } + return \yii\db\mysql\ColumnSchemaBuilder::class; } public function modifyCurrent(ColumnSchema $current): void From f7ed24bc094c9d9f7a216e309189c564f5172628 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 6 Sep 2024 16:24:58 +0530 Subject: [PATCH 09/15] Refactor --- src/lib/ColumnToCode.php | 3 --- src/lib/migrations/MigrationRecordBuilder.php | 1 + 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index e9a570bc..cdaef236 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -167,9 +167,6 @@ public function getCode(bool $quoted = false):string } if ($this->rawParts['default'] === null) { $default = ''; - } elseif (ApiGenerator::isPostgres() && $this->isEnum()) { - $default = - $this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : ''; } else { $default = $this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : ''; } diff --git a/src/lib/migrations/MigrationRecordBuilder.php b/src/lib/migrations/MigrationRecordBuilder.php index 98ee03b8..0512bbbe 100644 --- a/src/lib/migrations/MigrationRecordBuilder.php +++ b/src/lib/migrations/MigrationRecordBuilder.php @@ -227,6 +227,7 @@ public function addFk(string $fkName, string $tableAlias, string $fkCol, string $onUpdate ); } + return ''; } public function addUniqueIndex(string $tableAlias, string $indexName, array $columns):string From 9e8905ced5ada34fb3eff5e15854956a4166dcae Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 6 Sep 2024 16:26:34 +0530 Subject: [PATCH 10/15] Refactor --- src/lib/ColumnToCode.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index cdaef236..52460e49 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -165,12 +165,8 @@ public function getCode(bool $quoted = false):string array_unshift($parts, '$this'); return implode('->', array_filter(array_map('trim', $parts))); } - if ($this->rawParts['default'] === null) { - $default = ''; - } else { - $default = $this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : ''; - } + $default = $this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : ''; $code = $this->rawParts['type'] . ' ' . $this->rawParts['nullable'] . $default; if ((ApiGenerator::isMysql() || ApiGenerator::isMariaDb()) && $this->rawParts['position']) { $code .= ' ' . $this->rawParts['position']; From f3b3ad93183d893148cbfb1e411abd3fa8166ba0 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 6 Sep 2024 16:28:29 +0530 Subject: [PATCH 11/15] Refactor --- src/lib/ColumnToCode.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index 52460e49..81062b0d 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -166,14 +166,15 @@ public function getCode(bool $quoted = false):string return implode('->', array_filter(array_map('trim', $parts))); } + if (ApiGenerator::isPostgres() && $this->alterByXDbType) { + return $quoted ? VarDumper::export($this->rawParts['type']) : $this->rawParts['type']; + } + $default = $this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : ''; $code = $this->rawParts['type'] . ' ' . $this->rawParts['nullable'] . $default; if ((ApiGenerator::isMysql() || ApiGenerator::isMariaDb()) && $this->rawParts['position']) { $code .= ' ' . $this->rawParts['position']; } - if (ApiGenerator::isPostgres() && $this->alterByXDbType) { - return $quoted ? VarDumper::export($this->rawParts['type']) : $this->rawParts['type']; - } return $quoted ? VarDumper::export($code) : $code; } From e24d6c1efa5fe17b21c6fdaec6a4f23cb05f7422 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 6 Sep 2024 17:04:41 +0530 Subject: [PATCH 12/15] Remove redundant code --- src/lib/migrations/MysqlMigrationBuilder.php | 3 --- tests/unit/EnumTest.php | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/lib/migrations/MysqlMigrationBuilder.php b/src/lib/migrations/MysqlMigrationBuilder.php index 359c1af1..81061a98 100644 --- a/src/lib/migrations/MysqlMigrationBuilder.php +++ b/src/lib/migrations/MysqlMigrationBuilder.php @@ -29,9 +29,6 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir foreach ($changed as $attr) { $newColumn->$attr = $desired->$attr; } - if (static::isEnum($newColumn)) { - $newColumn->dbType = 'enum'; // TODO this is concretely not correct - } $this->migration->addUpCode($this->recordBuilder->alterColumn($this->model->getTableAlias(), $newColumn)) ->addDownCode($this->recordBuilder->alterColumn($this->model->getTableAlias(), $current)); } diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php index 8b48e249..46b08d45 100644 --- a/tests/unit/EnumTest.php +++ b/tests/unit/EnumTest.php @@ -168,7 +168,7 @@ public function testChangeToAndFromEnum() // edit enum to string and vice versa $this->deleteTables(); } - // TODO ENH enum change is more work than just changing the eunm values. And for PgSQL it is even more + // TODO ENH enum change is more work than just changing the enum values. And for PgSQL it is even more // public function testEnumValuesChange() // { // $this->deleteTables(); From d43368398168ec9e121ae9af76db6c6008d03b6a Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 6 Sep 2024 17:05:07 +0530 Subject: [PATCH 13/15] Present in https://github.com/cebe/yii2-openapi/issues/122 and https://github.com/php-openapi/yii2-openapi/issues/10 --- tests/unit/EnumTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php index 46b08d45..febc1047 100644 --- a/tests/unit/EnumTest.php +++ b/tests/unit/EnumTest.php @@ -168,7 +168,6 @@ public function testChangeToAndFromEnum() // edit enum to string and vice versa $this->deleteTables(); } - // TODO ENH enum change is more work than just changing the enum values. And for PgSQL it is even more // public function testEnumValuesChange() // { // $this->deleteTables(); From c4b4cf64c6ca22e09e5350a22c05ca4629dd1b68 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 6 Sep 2024 17:15:19 +0530 Subject: [PATCH 14/15] Already have a task in issue --- tests/unit/EnumTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/EnumTest.php b/tests/unit/EnumTest.php index febc1047..79797d38 100644 --- a/tests/unit/EnumTest.php +++ b/tests/unit/EnumTest.php @@ -200,7 +200,6 @@ public function testChangeToAndFromEnum() // edit enum to string and vice versa // public function testChangeEnumValues() // { - // // TODO // // add a value to list // // fix a typo in a enum value present in existing list // // remove a value from list From d6499207a3ce78cbcf9f879d9464a140c47dc559 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 28 Nov 2024 20:41:17 +0530 Subject: [PATCH 15/15] Remove redundant code --- src/lib/ColumnToCode.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib/ColumnToCode.php b/src/lib/ColumnToCode.php index f4da8079..b407aaaf 100644 --- a/src/lib/ColumnToCode.php +++ b/src/lib/ColumnToCode.php @@ -407,8 +407,6 @@ private function getIsBuiltinType($type, $dbType) private function resolveEnumType():void { if (ApiGenerator::isPostgres()) { - // $rawTableName = $this->dbSchema->getRawTableName($this->tableAlias); - // $this->rawParts['type'] = '"enum_'.$rawTableName.'_' . $this->column->name.'"'; $this->rawParts['type'] = '"'.$this->column->dbType.'"'; return; }