Skip to content

Response Status Codes are not the Codes defined in spec #95

New issue

Have a question about this project? Sign up for a free account to open an issue and contact its maintainers and the community.

By clicking “Sign up for ”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on ? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/lib/generators/ControllersGenerator.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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'], '\\');
Expand DownExpand Up@@ -126,15 +126,31 @@ 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());

$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);
}
}

$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 . ($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]);
Expand Down
2 changes: 2 additions & 0 deletions tests/specs/blog_v2/controllers/CommentController.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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)
Expand All@@ -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)
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
}


Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
}


Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

return [
'openApiPath' => '@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,
];
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
<?php

namespace app\controllers;

class MangoController extends \app\controllers\base\MangoController
{

public function checkAccess($action, $model = null, $params = [])
{
//TODO implement checkAccess
}

public function 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
// In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 201, 403, 404, 422
}


}

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
<?php

namespace app\controllers\base;

abstract class MangoController extends \yii\rest\Controller
{
public function actions()
{
return [
'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 = []);

abstract public function actionCake();

abstract public function actionCreateCake();

}
14 changes: 14 additions & 0 deletions tests/unit/IssueFixTest.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -1029,4 +1029,18 @@ public function test23ConsiderOpenapiExtensionXNoRelationAlsoInOtherPertinentPla
$this->checkFiles($actualFiles, $expectedFiles);
$this->runActualMigrations();
}

// https://.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);
}
}