Skip to content

Resolve: #175 Bug: allOf with multiple $refs #36

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 28, 2024
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ Such values are not allowed:
- `int null default null after low_price` (null and default will be handled by `nullable` and `default` keys respectively)
- MEDIUMINT(10) UNSIGNED ZEROFILL NULL DEFAULT '7' COMMENT 'comment' AFTER `seti`, ADD INDEX `t` (`w`)

If `enum` and `x-db-type` both are provided then for database column schema (migrations), only `x-db-type` will be considered ignoring `enum`.

### `x-indexes`

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

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.

It will be ignored for database column schema (migrations) if `x-db-type` is provided.

## Handling of `numeric` (#numeric, #MariaDb)

precision-default = 10
Expand Down
3 changes: 3 additions & 0 deletions src/lib/openapi/ResponseSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ protected static function schemaNameByRef($schemaOrReference): ?string
// if($schemaOrReference instanceof Reference){
// $schemaOrReference->resolve();
// }
if (!$schemaOrReference instanceof Reference) { # https://github.com/cebe/yii2-openapi/issues/175
return null;
}
$ref = $schemaOrReference->getJsonReference()->getJsonPointer()->getPointer();
$name = strpos($ref, '/components/schemas/') === 0 ? substr($ref, 20) : null;
return str_replace(JunctionSchemas::PREFIX, '', $name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return [
'openApiPath' => '@specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/index.yaml',
'generateUrls' => true,
'generateModels' => true,
'excludeModels' => [
'Error',
],
'generateControllers' => true,
'generateMigrations' => true,
'generateModelFaker' => true, // `generateModels` must be `true` in orde to use `generateModelFaker` as `true`
];

Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@

openapi: 3.0.3

info:
title: 'Proxy-Service'
version: 1.0.0

components:

responses:

AccountExpanded:
description: 'Returns one account by ID with additional information.'
content:
application/vnd.api+json:
schema:
type: object
required:
- status
- Account
properties:
status:
type: string
enum:
- valid
- invalid
Account:
allOf:
- $ref: '#/components/schemas/Account'
- type: object
properties:
invoiceContact:
$ref: "#/components/schemas/Contact"
paymentMethod:
$ref: "#/components/schemas/PaymentMethod"
errors:
type: object
description: only exists if status = invalid

schemas:

Account:
description: Account
type: object
required:
- id
- name
properties:
id:
type: integer
readOnly: true
name:
description: account name
type: string
maxLength: 128
paymentMethodName:
type: string

Contact:
description: Contact
type: object
required:
- id
- account
properties:
id:
type: integer
readOnly: true
account:
$ref: '#/components/schemas/Account'
active:
type: boolean
default: false
nickname:
type: string

PaymentMethod:
type: object
description: PaymentMethod
x-indexes:
- 'unique:name'
required:
- id
- name
properties:
id:
type: integer
readOnly: true
name:
type: string
example: Bank transfer within 14 days
maxLength: 150
x-faker: false

paths:

'/account/{id}':
parameters:
- name: Id
in: path
description: ID of Account
required: true
schema:
type: integer

get:
operationId: getAccount
summary: Account information
responses:
'200':
$ref: '#/components/responses/AccountExpanded'
'404':
description: Account with id = "\<account_id\>" was not found.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* OpenAPI UrlRules
*
* This file is auto generated.
*/
return [
'GET account/<id>' => 'account/view',
'account/<id>' => 'account/options',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace app\controllers;

class AccountController extends \app\controllers\base\AccountController
{

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

public function actionView($id)
{
//TODO implement actionView
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace app\controllers\base;

abstract class AccountController 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 actionView($id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Table for Account
*/
class m200000_000000_create_table_accounts extends \yii\db\Migration
{
public function up()
{
$this->createTable('{{%accounts}}', [
'id' => $this->primaryKey(),
'name' => $this->string(128)->notNull(),
'paymentMethodName' => $this->text()->null(),
]);
}

public function down()
{
$this->dropTable('{{%accounts}}');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Table for Contact
*/
class m200000_000001_create_table_contacts extends \yii\db\Migration
{
public function up()
{
$this->createTable('{{%contacts}}', [
'id' => $this->primaryKey(),
'account_id' => $this->integer()->notNull(),
'active' => $this->boolean()->null()->defaultValue(false),
'nickname' => $this->text()->null(),
]);
$this->addForeignKey('fk_contacts_account_id_accounts_id', '{{%contacts}}', 'account_id', '{{%accounts}}', 'id');
}

public function down()
{
$this->dropForeignKey('fk_contacts_account_id_accounts_id', '{{%contacts}}');
$this->dropTable('{{%contacts}}');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Table for PaymentMethod
*/
class m200000_000002_create_table_payment_methods extends \yii\db\Migration
{
public function up()
{
$this->createTable('{{%payment_methods}}', [
'id' => $this->primaryKey(),
'name' => $this->string(150)->notNull(),
]);
$this->createIndex('payment_methods_name_key', '{{%payment_methods}}', 'name', true);
}

public function down()
{
$this->dropIndex('payment_methods_name_key', '{{%payment_methods}}');
$this->dropTable('{{%payment_methods}}');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace app\models;

class Account extends \app\models\base\Account
{


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace app\models;

use Faker\UniqueGenerator;

/**
* Fake data generator for Account
* @method static Account makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
* @method static Account saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
* @method static Account[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
* @method static Account[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
*/
class AccountFaker extends BaseModelFaker
{

/**
* @param array|callable $attributes
* @return Account|\yii\db\ActiveRecord
* @example
* $model = (new PostFaker())->generateModels(['author_id' => 1]);
* $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) {
* $model->scenario = 'create';
* $model->author_id = 1;
* return $model;
* });
**/
public function generateModel($attributes = [])
{
$faker = $this->faker;
$uniqueFaker = $this->uniqueFaker;
$model = new Account();
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
$model->name = substr($faker->text(128), 0, 128);
$model->paymentMethodName = $faker->sentence;
if (!is_callable($attributes)) {
$model->setAttributes($attributes, false);
} else {
$model = $attributes($model, $faker, $uniqueFaker);
}
return $model;
}
}
Loading