From 426fecb1c5afcb2c100f2549112be9398f7091a8 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 7 Mar 2025 16:20:29 +0530 Subject: [PATCH 1/5] Modify a file to create pull request at GitHub --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f68df469..4cc1d72a 100644 --- a/README.md +++ b/README.md @@ -779,3 +779,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 49cd50e33135debecf375cff2b3ba81e7d4f821a Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 7 Mar 2025 18:18:19 +0530 Subject: [PATCH 2/5] Add message --- README.md | 1 - src/lib/generators/ControllersGenerator.php | 13 +++++-- .../index.php | 13 +++++++ .../index.yml | 36 +++++++++++++++++++ tests/unit/IssueFixTest.php | 14 ++++++++ 5 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.php create mode 100644 tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.yml diff --git a/README.md b/README.md index 4cc1d72a..f68df469 100644 --- a/README.md +++ b/README.md @@ -779,4 +779,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/generators/ControllersGenerator.php b/src/lib/generators/ControllersGenerator.php index bd749c9f..e3ddcf23 100644 --- a/src/lib/generators/ControllersGenerator.php +++ b/src/lib/generators/ControllersGenerator.php @@ -59,7 +59,7 @@ public function generate():CodeFiles $controllerPath = $path; /** * @var RestAction|FractalAction $action - **/ + **/ $action = $actions[0]; if ($action->prefix && !empty($action->prefixSettings)) { $controllerNamespace = trim($action->prefixSettings['namespace'], '\\'); @@ -126,15 +126,22 @@ protected function makeCustomController( ]; $reflection->addMethod('checkAccess', $params, AbstractMemberGenerator::FLAG_PUBLIC, '//TODO implement checkAccess'); foreach ($abstractActions as $action) { + + $responseHttpStatusCodes = ''; + foreach ($this->config->getOpenApi()->paths->getPaths()[$action->urlPath]->getOperations() as $verb => $operation) { + if ($verb === strtolower($action->requestMethod)) { + $responseHttpStatusCodes = implode(', ', array_keys($operation->responses->getResponses())); + } + } + $params = array_map(static function ($param) { return ['name' => $param]; }, $action->getParamNames()); - $reflection->addMethod( $action->actionMethodName, $params, AbstractMemberGenerator::FLAG_PUBLIC, - '//TODO implement ' . $action->actionMethodName + '// TODO implement ' . $action->actionMethodName . PHP_EOL . '// In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: ' . $responseHttpStatusCodes ); } $classFileGenerator->setClasses([$reflection]); diff --git a/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.php b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.php new file mode 100644 index 00000000..cf88e27a --- /dev/null +++ b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.yml', + 'generateUrls' => false, + 'generateModels' => false, + 'excludeModels' => [ + 'Error', + ], + 'generateControllers' => true, + 'generateMigrations' => false, + 'generateModelFaker' => false, +]; diff --git a/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.yml b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.yml new file mode 100644 index 00000000..df5b1314 --- /dev/null +++ b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.yml @@ -0,0 +1,36 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: 79_response_status_codes_are_not_the_codes_defined_in_spec +paths: + /mango/cake: + get: + responses: + '200': + description: The information + '403': + description: The information + '404': + description: The information + post: + responses: + '201': + description: The information + '403': + description: The information + '404': + description: The information + '422': + description: The information + +components: + schemas: + Address: + type: object + description: desc + properties: + id: + type: integer + name: + type: string + maxLength: 64 diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index ad3ff8fd..cda4e714 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -1014,4 +1014,18 @@ public function test22BugRulesRequiredIsGeneratedBeforeDefault() ]); $this->checkFiles($actualFiles, $expectedFiles); } + + // https://github.com/php-openapi/yii2-openapi/issues/79 + public function test79ResponseStatusCodesAreNotTheCodesDefinedInSpec() + { + $testFile = Yii::getAlias("@specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.php"); + $this->runGenerator($testFile); +// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ +// 'recursive' => true, +// ]); +// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql"), [ +// 'recursive' => true, +// ]); +// $this->checkFiles($actualFiles, $expectedFiles); + } } From ecbee9432dc6581367e0983c338be3656d125af1 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 7 Mar 2025 18:19:17 +0530 Subject: [PATCH 3/5] Complete the test --- .../mysql/controllers/MangoController.php | 27 +++++++++++++++ .../controllers/base/MangoController.php | 34 +++++++++++++++++++ tests/unit/IssueFixTest.php | 14 ++++---- 3 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql/controllers/MangoController.php create mode 100644 tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql/controllers/base/MangoController.php diff --git a/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql/controllers/MangoController.php b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql/controllers/MangoController.php new file mode 100644 index 00000000..4b3e60e3 --- /dev/null +++ b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql/controllers/MangoController.php @@ -0,0 +1,27 @@ + [ + '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 actionCake(); + + abstract public function actionCreateCake(); + +} diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index cda4e714..13bf8702 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -1020,12 +1020,12 @@ public function test79ResponseStatusCodesAreNotTheCodesDefinedInSpec() { $testFile = Yii::getAlias("@specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.php"); $this->runGenerator($testFile); -// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ -// 'recursive' => true, -// ]); -// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql"), [ -// 'recursive' => true, -// ]); -// $this->checkFiles($actualFiles, $expectedFiles); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); } } From 229c0034438060e562aee47ad230c9db564ea8ee Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 8 Mar 2025 07:31:40 +0530 Subject: [PATCH 4/5] Refactor --- src/lib/generators/ControllersGenerator.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib/generators/ControllersGenerator.php b/src/lib/generators/ControllersGenerator.php index e3ddcf23..372af037 100644 --- a/src/lib/generators/ControllersGenerator.php +++ b/src/lib/generators/ControllersGenerator.php @@ -129,8 +129,9 @@ protected function makeCustomController( $responseHttpStatusCodes = ''; foreach ($this->config->getOpenApi()->paths->getPaths()[$action->urlPath]->getOperations() as $verb => $operation) { - if ($verb === strtolower($action->requestMethod)) { - $responseHttpStatusCodes = implode(', ', array_keys($operation->responses->getResponses())); + $codes = array_keys($operation->responses->getResponses()); + if ($verb === strtolower($action->requestMethod) && $codes !== [200]) { + $responseHttpStatusCodes = implode(', ', $codes); } } @@ -141,7 +142,7 @@ protected function makeCustomController( $action->actionMethodName, $params, AbstractMemberGenerator::FLAG_PUBLIC, - '// TODO implement ' . $action->actionMethodName . PHP_EOL . '// In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: ' . $responseHttpStatusCodes + '//TODO implement ' . $action->actionMethodName . ($responseHttpStatusCodes ? PHP_EOL . '// In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: ' . $responseHttpStatusCodes : '') ); } $classFileGenerator->setClasses([$reflection]); From d89fe286ae94d906092e809d36d09e6a7ee8b844 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 8 Mar 2025 08:12:43 +0530 Subject: [PATCH 5/5] Refactor and fix failing test --- src/lib/generators/ControllersGenerator.php | 12 ++++++++++-- .../specs/blog_v2/controllers/CommentController.php | 2 ++ .../pgsql/controllers/ContactController.php | 2 ++ .../pgsql/controllers/AccountController.php | 1 + .../mysql/controllers/MangoController.php | 4 ++-- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/lib/generators/ControllersGenerator.php b/src/lib/generators/ControllersGenerator.php index 372af037..ea0c73c4 100644 --- a/src/lib/generators/ControllersGenerator.php +++ b/src/lib/generators/ControllersGenerator.php @@ -126,11 +126,19 @@ protected function makeCustomController( ]; $reflection->addMethod('checkAccess', $params, AbstractMemberGenerator::FLAG_PUBLIC, '//TODO implement checkAccess'); foreach ($abstractActions as $action) { - $responseHttpStatusCodes = ''; foreach ($this->config->getOpenApi()->paths->getPaths()[$action->urlPath]->getOperations() as $verb => $operation) { $codes = array_keys($operation->responses->getResponses()); - if ($verb === strtolower($action->requestMethod) && $codes !== [200]) { + + $only200OrDefault = false; + if ($codes === [200] || $codes === ['default']) { + $only200OrDefault = true; + } + if (in_array('default', $codes) && in_array(200, $codes) && count($codes) === 2) { + $only200OrDefault = true; + } + + if ($verb === strtolower($action->requestMethod) && !$only200OrDefault) { $responseHttpStatusCodes = implode(', ', $codes); } } diff --git a/tests/specs/blog_v2/controllers/CommentController.php b/tests/specs/blog_v2/controllers/CommentController.php index 0448ec72..c46307ed 100644 --- a/tests/specs/blog_v2/controllers/CommentController.php +++ b/tests/specs/blog_v2/controllers/CommentController.php @@ -18,6 +18,7 @@ public function actionListForPost($postId) public function actionCreateForPost($postId) { //TODO implement actionCreateForPost + // In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 201, default } public function actionViewForPost($slug, $id) @@ -28,6 +29,7 @@ public function actionViewForPost($slug, $id) public function actionDeleteForPost($slug, $id) { //TODO implement actionDeleteForPost + // In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 204 } public function actionUpdateForPost($slug, $id) diff --git a/tests/specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/pgsql/controllers/ContactController.php b/tests/specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/pgsql/controllers/ContactController.php index 10422b18..5f21cabb 100644 --- a/tests/specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/pgsql/controllers/ContactController.php +++ b/tests/specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/pgsql/controllers/ContactController.php @@ -13,11 +13,13 @@ public function checkAccess($action, $model = null, $params = []) public function actionListForAccount($accountId) { //TODO implement actionListForAccount + // In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 200, 403 } public function actionViewForAccount($accountId, $contactId) { //TODO implement actionViewForAccount + // In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 200, 403 } diff --git a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/controllers/AccountController.php b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/controllers/AccountController.php index b4044b8f..cfa4a6d3 100644 --- a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/controllers/AccountController.php +++ b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/controllers/AccountController.php @@ -13,6 +13,7 @@ public function checkAccess($action, $model = null, $params = []) public function actionView($id) { //TODO implement actionView + // In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 200, 404 } diff --git a/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql/controllers/MangoController.php b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql/controllers/MangoController.php index 4b3e60e3..46c40ad1 100644 --- a/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql/controllers/MangoController.php +++ b/tests/specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql/controllers/MangoController.php @@ -12,13 +12,13 @@ public function checkAccess($action, $model = null, $params = []) public function actionCake() { - // TODO implement actionCake + //TODO implement actionCake // In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 200, 403, 404 } public function actionCreateCake() { - // TODO implement actionCreateCake + //TODO implement actionCreateCake // In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 201, 403, 404, 422 }