Skip to content

Commit fe3cd36

Browse files
authored
Merge pull request #36 from SOHELAHMED7/175-bug-allof-with-multiple-dollarrefs
Draft: Bug: allOf with multiple $refs
2 parents a6d8786 + 949a32f commit fe3cd36

21 files changed

+682
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ Such values are not allowed:
182182
- `int null default null after low_price` (null and default will be handled by `nullable` and `default` keys respectively)
183183
- MEDIUMINT(10) UNSIGNED ZEROFILL NULL DEFAULT '7' COMMENT 'comment' AFTER `seti`, ADD INDEX `t` (`w`)
184184

185+
If `enum` and `x-db-type` both are provided then for database column schema (migrations), only `x-db-type` will be considered ignoring `enum`.
186+
185187
### `x-indexes`
186188

187189
Specify table indexes
@@ -446,6 +448,8 @@ It works on all 3 DB: MySQL, MariaDb and PgSQL.
446448

447449
Note: Changes in enum values are not very simple. For Mysql and Mariadb, migrations will be generated but in many cases custom modification in it are required. For Pgsql migrations for change in enum values will not be generated. It should be handled manually.
448450

451+
It will be ignored for database column schema (migrations) if `x-db-type` is provided.
452+
449453
## Handling of `numeric` (#numeric, #MariaDb)
450454

451455
precision-default = 10

src/lib/openapi/ResponseSchema.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ protected static function schemaNameByRef($schemaOrReference): ?string
4949
// if($schemaOrReference instanceof Reference){
5050
// $schemaOrReference->resolve();
5151
// }
52+
if (!$schemaOrReference instanceof Reference) { # https://github.com/cebe/yii2-openapi/issues/175
53+
return null;
54+
}
5255
$ref = $schemaOrReference->getJsonReference()->getJsonPointer()->getPointer();
5356
$name = strpos($ref, '/components/schemas/') === 0 ? substr($ref, 20) : null;
5457
return str_replace(JunctionSchemas::PREFIX, '', $name);
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/175_bug_allof_with_multiple_dollarrefs/index.yaml',
5+
'generateUrls' => true,
6+
'generateModels' => true,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => true,
11+
'generateMigrations' => true,
12+
'generateModelFaker' => true, // `generateModels` must be `true` in orde to use `generateModelFaker` as `true`
13+
];
14+
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
2+
openapi: 3.0.3
3+
4+
info:
5+
title: 'Proxy-Service'
6+
version: 1.0.0
7+
8+
components:
9+
10+
responses:
11+
12+
AccountExpanded:
13+
description: 'Returns one account by ID with additional information.'
14+
content:
15+
application/vnd.api+json:
16+
schema:
17+
type: object
18+
required:
19+
- status
20+
- Account
21+
properties:
22+
status:
23+
type: string
24+
enum:
25+
- valid
26+
- invalid
27+
Account:
28+
allOf:
29+
- $ref: '#/components/schemas/Account'
30+
- type: object
31+
properties:
32+
invoiceContact:
33+
$ref: "#/components/schemas/Contact"
34+
paymentMethod:
35+
$ref: "#/components/schemas/PaymentMethod"
36+
errors:
37+
type: object
38+
description: only exists if status = invalid
39+
40+
schemas:
41+
42+
Account:
43+
description: Account
44+
type: object
45+
required:
46+
- id
47+
- name
48+
properties:
49+
id:
50+
type: integer
51+
readOnly: true
52+
name:
53+
description: account name
54+
type: string
55+
maxLength: 128
56+
paymentMethodName:
57+
type: string
58+
59+
Contact:
60+
description: Contact
61+
type: object
62+
required:
63+
- id
64+
- account
65+
properties:
66+
id:
67+
type: integer
68+
readOnly: true
69+
account:
70+
$ref: '#/components/schemas/Account'
71+
active:
72+
type: boolean
73+
default: false
74+
nickname:
75+
type: string
76+
77+
PaymentMethod:
78+
type: object
79+
description: PaymentMethod
80+
x-indexes:
81+
- 'unique:name'
82+
required:
83+
- id
84+
- name
85+
properties:
86+
id:
87+
type: integer
88+
readOnly: true
89+
name:
90+
type: string
91+
example: Bank transfer within 14 days
92+
maxLength: 150
93+
x-faker: false
94+
95+
paths:
96+
97+
'/account/{id}':
98+
parameters:
99+
- name: Id
100+
in: path
101+
description: ID of Account
102+
required: true
103+
schema:
104+
type: integer
105+
106+
get:
107+
operationId: getAccount
108+
summary: Account information
109+
responses:
110+
'200':
111+
$ref: '#/components/responses/AccountExpanded'
112+
'404':
113+
description: Account with id = "\<account_id\>" was not found.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* OpenAPI UrlRules
4+
*
5+
* This file is auto generated.
6+
*/
7+
return [
8+
'GET account/<id>' => 'account/view',
9+
'account/<id>' => 'account/options',
10+
];
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
public function actionView($id)
14+
{
15+
//TODO implement actionView
16+
}
17+
18+
19+
}
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
'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 actionView($id);
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* Table for Account
5+
*/
6+
class m200000_000000_create_table_accounts extends \yii\db\Migration
7+
{
8+
public function up()
9+
{
10+
$this->createTable('{{%accounts}}', [
11+
'id' => $this->primaryKey(),
12+
'name' => $this->string(128)->notNull(),
13+
'paymentMethodName' => $this->text()->null(),
14+
]);
15+
}
16+
17+
public function down()
18+
{
19+
$this->dropTable('{{%accounts}}');
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* Table for Contact
5+
*/
6+
class m200000_000001_create_table_contacts extends \yii\db\Migration
7+
{
8+
public function up()
9+
{
10+
$this->createTable('{{%contacts}}', [
11+
'id' => $this->primaryKey(),
12+
'account_id' => $this->integer()->notNull(),
13+
'active' => $this->boolean()->null()->defaultValue(false),
14+
'nickname' => $this->text()->null(),
15+
]);
16+
$this->addForeignKey('fk_contacts_account_id_accounts_id', '{{%contacts}}', 'account_id', '{{%accounts}}', 'id');
17+
}
18+
19+
public function down()
20+
{
21+
$this->dropForeignKey('fk_contacts_account_id_accounts_id', '{{%contacts}}');
22+
$this->dropTable('{{%contacts}}');
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* Table for PaymentMethod
5+
*/
6+
class m200000_000002_create_table_payment_methods extends \yii\db\Migration
7+
{
8+
public function up()
9+
{
10+
$this->createTable('{{%payment_methods}}', [
11+
'id' => $this->primaryKey(),
12+
'name' => $this->string(150)->notNull(),
13+
]);
14+
$this->createIndex('payment_methods_name_key', '{{%payment_methods}}', 'name', true);
15+
}
16+
17+
public function down()
18+
{
19+
$this->dropIndex('payment_methods_name_key', '{{%payment_methods}}');
20+
$this->dropTable('{{%payment_methods}}');
21+
}
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
class Account extends \app\models\base\Account
6+
{
7+
8+
9+
}
10+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace app\models;
3+
4+
use Faker\UniqueGenerator;
5+
6+
/**
7+
* Fake data generator for Account
8+
* @method static Account makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
9+
* @method static Account saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
10+
* @method static Account[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
11+
* @method static Account[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
12+
*/
13+
class AccountFaker extends BaseModelFaker
14+
{
15+
16+
/**
17+
* @param array|callable $attributes
18+
* @return Account|\yii\db\ActiveRecord
19+
* @example
20+
* $model = (new PostFaker())->generateModels(['author_id' => 1]);
21+
* $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) {
22+
* $model->scenario = 'create';
23+
* $model->author_id = 1;
24+
* return $model;
25+
* });
26+
**/
27+
public function generateModel($attributes = [])
28+
{
29+
$faker = $this->faker;
30+
$uniqueFaker = $this->uniqueFaker;
31+
$model = new Account();
32+
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
33+
$model->name = substr($faker->text(128), 0, 128);
34+
$model->paymentMethodName = $faker->sentence;
35+
if (!is_callable($attributes)) {
36+
$model->setAttributes($attributes, false);
37+
} else {
38+
$model = $attributes($model, $faker, $uniqueFaker);
39+
}
40+
return $model;
41+
}
42+
}

0 commit comments

Comments
 (0)