Skip to content

Commit f51c54c

Browse files
authored
Merge pull request #37 from SOHELAHMED7/172-schemayaml-requestbody-has-no-effect
Draft: cebe#172 schema.yaml: requestBody has no effect
2 parents fe3cd36 + b752fad commit f51c54c

File tree

6 files changed

+183
-29
lines changed

6 files changed

+183
-29
lines changed

src/lib/openapi/ResponseSchema.php

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace cebe\yii2openapi\lib\openapi;
99

10+
use cebe\openapi\exceptions\UnresolvableReferenceException;
1011
use cebe\openapi\spec\MediaType;
1112
use cebe\openapi\spec\Operation;
1213
use cebe\openapi\spec\Reference;
@@ -36,10 +37,10 @@ protected static function isObjectSchema($schema): bool
3637
protected static function isArraySchemaWithRefItems($schema): bool
3738
{
3839
return isset($schema->items) && $schema->items instanceof Reference &&
39-
(isset($schema->type) && $schema->type === 'array');
40+
(isset($schema->type) && $schema->type === 'array');
4041
}
4142

42-
protected static function hasAttributesReference($schema):bool
43+
protected static function hasAttributesReference($schema): bool
4344
{
4445
return isset($schema->properties['attributes']) && $schema->properties['attributes'] instanceof Reference;
4546
}
@@ -58,11 +59,11 @@ protected static function schemaNameByRef($schemaOrReference): ?string
5859
}
5960

6061
/**
61-
* @param \cebe\openapi\spec\Operation $operation
62+
* @param Operation $operation
6263
* @return array
63-
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException
64+
* @throws UnresolvableReferenceException
6465
*/
65-
public static function guessResponseRelations(Operation $operation):array
66+
public static function guessResponseRelations(Operation $operation): array
6667
{
6768
if (!isset($operation->responses)) {
6869
return [];
@@ -112,12 +113,12 @@ public static function guessResponseRelations(Operation $operation):array
112113
}
113114

114115
/**
115-
* @param \cebe\openapi\spec\Operation $operation
116+
* @param Operation $operation
116117
* @param $actionType
117118
* @return string|null
118-
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException
119+
* @throws UnresolvableReferenceException
119120
*/
120-
public static function guessModelClass(Operation $operation, $actionType):?string
121+
public static function guessModelClass(Operation $operation, $actionType): ?string
121122
{
122123
// first, check request body
123124
$requestBody = $operation->requestBody;
@@ -162,20 +163,26 @@ public static function guessModelClass(Operation $operation, $actionType):?strin
162163
}
163164

164165
/**
165-
* @param \cebe\openapi\SpecObjectInterface $property
166+
* @param SpecObjectInterface $property
166167
* @return array|null[]
167-
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException
168+
* @throws UnresolvableReferenceException
168169
*/
169170
public static function guessModelClassFromJsonResource(SpecObjectInterface $property): array
170171
{
171-
$schema = $property instanceof Reference? $property->resolve() : $property;
172-
173-
if (self::isObjectSchema($schema) && self::hasAttributesReference($schema)) {
174-
$name = self::schemaNameByRef($schema->properties['attributes']);
175-
if ($name !== null) {
176-
return [$name, '', '', 'object'];
172+
$schema = $property instanceof Reference ? $property->resolve() : $property;
173+
if (self::isObjectSchema($schema)) {
174+
if (self::hasAttributesReference($schema)) {
175+
$name = self::schemaNameByRef($schema->properties['attributes']);
176+
if ($name !== null) {
177+
return [$name, '', '', 'object'];
178+
}
179+
return [null, null, null, null];
180+
} else { # https://github.com/cebe/yii2-openapi/issues/172
181+
$name = self::schemaNameByRef($property);
182+
if ($name !== null) {
183+
return [$name, '', '', 'object'];
184+
}
177185
}
178-
return [null, null, null, null];
179186
}
180187
if (self::isArraySchemaWithRefItems($property)) {
181188
$ref = $property->items->resolve();
@@ -191,11 +198,11 @@ public static function guessModelClassFromJsonResource(SpecObjectInterface $prop
191198
}
192199

193200
/**
194-
* @param \cebe\openapi\spec\MediaType $content
201+
* @param MediaType $content
195202
* @return array|null[]
196-
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException
203+
* @throws UnresolvableReferenceException
197204
*/
198-
public static function guessModelClassFromContent(MediaType $content):array
205+
public static function guessModelClassFromContent(MediaType $content): array
199206
{
200207
/** @var $referencedSchema Schema */
201208
if ($content->schema instanceof Reference) {
@@ -258,9 +265,9 @@ public static function guessModelClassFromContent(MediaType $content):array
258265
* @param Operation $operation
259266
* @param $modelClass
260267
* @return null|array
261-
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException
268+
* @throws UnresolvableReferenceException
262269
*/
263-
public static function findResponseWrapper(Operation $operation, $modelClass):?array
270+
public static function findResponseWrapper(Operation $operation, $modelClass): ?array
264271
{
265272
if (!isset($operation->responses)) {
266273
return null;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/172_schemayaml_requestbody_has_no_effect/index.yaml',
5+
'generateUrls' => false,
6+
'generateModels' => false,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => true,
11+
'generateMigrations' => false,
12+
'generateModelFaker' => false, // `generateModels` must be `true` in orde to use `generateModelFaker` as `true`
13+
];
14+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
openapi: 3.0.3
2+
3+
info:
4+
title: 'Proxy-Service'
5+
version: 1.0.0
6+
7+
components:
8+
9+
requestBodies:
10+
11+
Account:
12+
description: 'Create / update account'
13+
required: true
14+
content:
15+
application/vnd.api+json:
16+
schema:
17+
type: object
18+
properties:
19+
data:
20+
$ref: '#/components/schemas/Account'
21+
22+
responses:
23+
24+
Account:
25+
description: 'Returns one account by ID.'
26+
content:
27+
application/vnd.api+json:
28+
schema:
29+
$ref: '#/components/schemas/Account'
30+
31+
schemas:
32+
33+
Account:
34+
description: Account
35+
type: object
36+
required:
37+
- id
38+
- name
39+
properties:
40+
id:
41+
type: integer
42+
readOnly: true
43+
name:
44+
description: account name
45+
type: string
46+
maxLength: 128
47+
48+
paths:
49+
50+
'/accounts':
51+
52+
post:
53+
operationId: createAccount
54+
summary: Create a account
55+
description: Create account
56+
requestBody:
57+
$ref: '#/components/requestBodies/Account'
58+
responses:
59+
'201':
60+
description: OK
61+
# $ref: '#/components/responses/Account'
62+
'400':
63+
description: BodyParams must be an array.
64+
'422':
65+
description: Validation error.
66+
tags:
67+
- Accounts
68+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace app\controllers;
4+
5+
class AccountController extends \app\controllers\base\AccountController
6+
{
7+
8+
public function checkAccess($action, $model = null, $params = [])
9+
{
10+
//TODO implement checkAccess
11+
}
12+
13+
14+
}
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace app\controllers\base;
4+
5+
abstract class AccountController extends \yii\rest\Controller
6+
{
7+
public function actions()
8+
{
9+
return [
10+
'create' => [
11+
'class' => \yii\rest\CreateAction::class,
12+
'modelClass' => \app\models\Account::class,
13+
'checkAccess' => [$this, 'checkAccess'],
14+
],
15+
'options' => [
16+
'class' => \yii\rest\OptionsAction::class,
17+
],
18+
];
19+
}
20+
21+
/**
22+
* Checks the privilege of the current user.
23+
*
24+
* This method checks whether the current user has the privilege
25+
* to run the specified action against the specified data model.
26+
* If the user does not have access, a [[ForbiddenHttpException]] should be thrown.
27+
*
28+
* @param string $action the ID of the action to be executed
29+
* @param object $model the model to be accessed. If null, it means no specific model is being accessed.
30+
* @param array $params additional parameters
31+
* @throws \yii\web\ForbiddenHttpException if the user does not have access
32+
*/
33+
abstract public function checkAccess($action, $model = null, $params = []);
34+
35+
}

tests/unit/IssueFixTest.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,27 @@ public function test175BugAllOfWithMultipleDollarRefs()
307307
{
308308
$testFile = Yii::getAlias("@specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/index.php");
309309
$this->runGenerator($testFile, 'pgsql');
310-
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
311-
'recursive' => true,
312-
]);
313-
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql"), [
314-
'recursive' => true,
315-
]);
316-
$this->checkFiles($actualFiles, $expectedFiles);
310+
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
311+
'recursive' => true,
312+
]);
313+
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql"), [
314+
'recursive' => true,
315+
]);
316+
$this->checkFiles($actualFiles, $expectedFiles);
317+
}
318+
319+
// #172 https://github.com/cebe/yii2-openapi/issues/172
320+
// schema.yaml: requestBody has no effect
321+
public function test172SchemayamlRequestBodyHasNoEffect()
322+
{
323+
$testFile = Yii::getAlias("@specs/issue_fix/172_schemayaml_requestbody_has_no_effect/index.php");
324+
$this->runGenerator($testFile, 'pgsql');
325+
$actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
326+
'recursive' => true,
327+
]);
328+
$expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/172_schemayaml_requestbody_has_no_effect/pgsql"), [
329+
'recursive' => true,
330+
]);
331+
$this->checkFiles($actualFiles, $expectedFiles);
317332
}
318333
}

0 commit comments

Comments
 (0)