Skip to content

Commit f83bbd4

Browse files
committed
Merge branch '79-response-status-codes-are-not-the-codes-defined-in-spec' of github.com:php-openapi/yii2-openapi into fix-conflicting-pull-requests
2 parents 856e1d6 + 95670f5 commit f83bbd4

File tree

9 files changed

+152
-7
lines changed

9 files changed

+152
-7
lines changed

src/lib/generators/ControllersGenerator.php

+19-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function generate():CodeFiles
5959
$controllerPath = $path;
6060
/**
6161
* @var RestAction|FractalAction $action
62-
**/
62+
**/
6363
$action = $actions[0];
6464
if ($action->prefix && !empty($action->prefixSettings)) {
6565
$controllerNamespace = trim($action->prefixSettings['namespace'], '\\');
@@ -126,15 +126,31 @@ protected function makeCustomController(
126126
];
127127
$reflection->addMethod('checkAccess', $params, AbstractMemberGenerator::FLAG_PUBLIC, '//TODO implement checkAccess');
128128
foreach ($abstractActions as $action) {
129+
$responseHttpStatusCodes = '';
130+
foreach ($this->config->getOpenApi()->paths->getPaths()[$action->urlPath]->getOperations() as $verb => $operation) {
131+
$codes = array_keys($operation->responses->getResponses());
132+
133+
$only200OrDefault = false;
134+
if ($codes === [200] || $codes === ['default']) {
135+
$only200OrDefault = true;
136+
}
137+
if (in_array('default', $codes) && in_array(200, $codes) && count($codes) === 2) {
138+
$only200OrDefault = true;
139+
}
140+
141+
if ($verb === strtolower($action->requestMethod) && !$only200OrDefault) {
142+
$responseHttpStatusCodes = implode(', ', $codes);
143+
}
144+
}
145+
129146
$params = array_map(static function ($param) {
130147
return ['name' => $param];
131148
}, $action->getParamNames());
132-
133149
$reflection->addMethod(
134150
$action->actionMethodName,
135151
$params,
136152
AbstractMemberGenerator::FLAG_PUBLIC,
137-
'//TODO implement ' . $action->actionMethodName
153+
'//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 : '')
138154
);
139155
}
140156
$classFileGenerator->setClasses([$reflection]);

tests/specs/blog_v2/controllers/CommentController.php

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function actionListForPost($postId)
1818
public function actionCreateForPost($postId)
1919
{
2020
//TODO implement actionCreateForPost
21+
// In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 201, default
2122
}
2223

2324
public function actionViewForPost($slug, $id)
@@ -28,6 +29,7 @@ public function actionViewForPost($slug, $id)
2829
public function actionDeleteForPost($slug, $id)
2930
{
3031
//TODO implement actionDeleteForPost
32+
// In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 204
3133
}
3234

3335
public function actionUpdateForPost($slug, $id)

tests/specs/issue_fix/163_generator_crash_when_using_reference_inside_an_object/pgsql/controllers/ContactController.php

+2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ public function checkAccess($action, $model = null, $params = [])
1313
public function actionListForAccount($accountId)
1414
{
1515
//TODO implement actionListForAccount
16+
// In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 200, 403
1617
}
1718

1819
public function actionViewForAccount($accountId, $contactId)
1920
{
2021
//TODO implement actionViewForAccount
22+
// In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 200, 403
2123
}
2224

2325

tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/controllers/AccountController.php

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function checkAccess($action, $model = null, $params = [])
1313
public function actionView($id)
1414
{
1515
//TODO implement actionView
16+
// In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 200, 404
1617
}
1718

1819

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.yml',
5+
'generateUrls' => false,
6+
'generateModels' => false,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => true,
11+
'generateMigrations' => false,
12+
'generateModelFaker' => false,
13+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: 79_response_status_codes_are_not_the_codes_defined_in_spec
5+
paths:
6+
/mango/cake:
7+
get:
8+
responses:
9+
'200':
10+
description: The information
11+
'403':
12+
description: The information
13+
'404':
14+
description: The information
15+
post:
16+
responses:
17+
'201':
18+
description: The information
19+
'403':
20+
description: The information
21+
'404':
22+
description: The information
23+
'422':
24+
description: The information
25+
26+
components:
27+
schemas:
28+
Address:
29+
type: object
30+
description: desc
31+
properties:
32+
id:
33+
type: integer
34+
name:
35+
type: string
36+
maxLength: 64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace app\controllers;
4+
5+
class MangoController extends \app\controllers\base\MangoController
6+
{
7+
8+
public function checkAccess($action, $model = null, $params = [])
9+
{
10+
//TODO implement checkAccess
11+
}
12+
13+
public function actionCake()
14+
{
15+
//TODO implement actionCake
16+
// In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 200, 403, 404
17+
}
18+
19+
public function actionCreateCake()
20+
{
21+
//TODO implement actionCreateCake
22+
// In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: 201, 403, 404, 422
23+
}
24+
25+
26+
}
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace app\controllers\base;
4+
5+
abstract class MangoController extends \yii\rest\Controller
6+
{
7+
public function actions()
8+
{
9+
return [
10+
'options' => [
11+
'class' => \yii\rest\OptionsAction::class,
12+
],
13+
];
14+
}
15+
16+
/**
17+
* Checks the privilege of the current user.
18+
*
19+
* This method checks whether the current user has the privilege
20+
* to run the specified action against the specified data model.
21+
* If the user does not have access, a [[ForbiddenHttpException]] should be thrown.
22+
*
23+
* @param string $action the ID of the action to be executed
24+
* @param object $model the model to be accessed. If null, it means no specific model is being accessed.
25+
* @param array $params additional parameters
26+
* @throws \yii\web\ForbiddenHttpException if the user does not have access
27+
*/
28+
abstract public function checkAccess($action, $model = null, $params = []);
29+
30+
abstract public function actionCake();
31+
32+
abstract public function actionCreateCake();
33+
34+
}

tests/unit/IssueFixTest.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -1030,15 +1030,15 @@ public function test23ConsiderOpenapiExtensionXNoRelationAlsoInOtherPertinentPla
10301030
$this->runActualMigrations();
10311031
}
10321032

1033-
// https://github.com/php-openapi/yii2-openapi/issues/96
1034-
public function test96ComponentSchemaShouldBeOptional()
1033+
// https://github.com/php-openapi/yii2-openapi/issues/79
1034+
public function test79ResponseStatusCodesAreNotTheCodesDefinedInSpec()
10351035
{
1036-
$testFile = Yii::getAlias("@specs/issue_fix/96_component_schema_should_be_optional/index.php");
1036+
$testFile = Yii::getAlias("@specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/index.php");
10371037
$this->runGenerator($testFile);
10381038
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
10391039
'recursive' => true,
10401040
]);
1041-
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/96_component_schema_should_be_optional/mysql"), [
1041+
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/79_response_status_codes_are_not_the_codes_defined_in_spec/mysql"), [
10421042
'recursive' => true,
10431043
]);
10441044
$this->checkFiles($actualFiles, $expectedFiles);
@@ -1058,4 +1058,18 @@ public function test87ImplementForJsonInIsrefpointertoschema()
10581058
$this->checkFiles($actualFiles, $expectedFiles);
10591059
$this->runActualMigrations();
10601060
}
1061+
1062+
// https://github.com/php-openapi/yii2-openapi/issues/96
1063+
public function test96ComponentSchemaShouldBeOptional()
1064+
{
1065+
$testFile = Yii::getAlias("@specs/issue_fix/96_component_schema_should_be_optional/index.php");
1066+
$this->runGenerator($testFile);
1067+
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
1068+
'recursive' => true,
1069+
]);
1070+
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/96_component_schema_should_be_optional/mysql"), [
1071+
'recursive' => true,
1072+
]);
1073+
$this->checkFiles($actualFiles, $expectedFiles);
1074+
}
10611075
}

0 commit comments

Comments
 (0)