Skip to content

Fix conflicting pull requests #99

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 30 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
cbb5adb
Create PR
SOHELAHMED7 Feb 12, 2025
ca8e804
Add passing test
SOHELAHMED7 Feb 14, 2025
b050abd
Failing test
SOHELAHMED7 Mar 1, 2025
45f3f90
Fix
SOHELAHMED7 Mar 1, 2025
e69fc04
Fix failing tests
SOHELAHMED7 Mar 3, 2025
ab6629b
Create PR
SOHELAHMED7 Mar 3, 2025
207228b
Add test stub
SOHELAHMED7 Mar 3, 2025
4c409f7
WIP
SOHELAHMED7 Mar 4, 2025
2eda480
Refactor
SOHELAHMED7 Mar 4, 2025
1231531
Refactor 2
SOHELAHMED7 Mar 4, 2025
d93d0b3
Merge branch '88-in-case-of-updating-a-model-generator-creates-redund…
SOHELAHMED7 Mar 4, 2025
a69f43a
Implement
SOHELAHMED7 Mar 4, 2025
f92b782
Complete the test
SOHELAHMED7 Mar 4, 2025
5b1ec54
Fix failing tests
SOHELAHMED7 Mar 4, 2025
2134ddf
Refactor and add docs
SOHELAHMED7 Mar 4, 2025
426fecb
Modify a file to create pull request at GitHub
SOHELAHMED7 Mar 7, 2025
49cd50e
Add message
SOHELAHMED7 Mar 7, 2025
ecbee94
Complete the test
SOHELAHMED7 Mar 7, 2025
229c003
Refactor
SOHELAHMED7 Mar 8, 2025
d89fe28
Refactor and fix failing test
SOHELAHMED7 Mar 8, 2025
4aa7668
Fix
SOHELAHMED7 Mar 8, 2025
24d559a
Merge branches 'master' and '96-component-schema-should-be-optional' …
SOHELAHMED7 Mar 12, 2025
95670f5
Merge branch 'master' of github.com:php-openapi/yii2-openapi into 79-…
SOHELAHMED7 Mar 12, 2025
c4c9467
Merge branch 'master' of github.com:php-openapi/yii2-openapi into 90-…
SOHELAHMED7 Mar 12, 2025
ca1e12a
Merge branch 'master' of github.com:php-openapi/yii2-openapi into 88-…
SOHELAHMED7 Mar 12, 2025
856e1d6
Merge branches 'master' and '96-component-schema-should-be-optional' …
SOHELAHMED7 Mar 12, 2025
f83bbd4
Merge branch '79-response-status-codes-are-not-the-codes-defined-in-s…
SOHELAHMED7 Mar 12, 2025
8921fed
Merge branch '88-in-case-of-updating-a-model-generator-creates-redund…
SOHELAHMED7 Mar 12, 2025
dd34cdb
Merge branch '78-properties-that-are-marked-as-readonly-are-not-read-…
SOHELAHMED7 Mar 12, 2025
4d32a93
Merge branch '90-implement-belongs-to-relations-in-models' of github.…
SOHELAHMED7 Mar 12, 2025
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
10 changes: 6 additions & 4 deletions src/generator/default/dbmodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,14 @@ public function get<?= $relation->getCamelName() ?>()
<?php endif;?>
}
<?php endforeach; ?>
<?php $i = 1; foreach ($model->inverseRelations as $relationName => $relation): ?>
<?php $i = 1; $usedRelationNames = [];
foreach ($model->belongsToRelations as $relationName => $relation): ?><?php $number = in_array($relation->getCamelName(), $usedRelationNames) ? $i : '' ?>

public function get<?= $relation->getCamelName().($i===1 ? '' : $i) ?>()
# belongs to relation
public function get<?= $relation->getCamelName() . ($number) ?>()
{
return $this-><?= $relation->getMethod() ?>(\<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?>::class, <?php
echo $relation->linkToString() ?>)->inverseOf('<?= $relation->getInverse() ?>');
echo $relation->linkToString() ?>);
}
<?php $i++; endforeach; ?>
<?php $i++; $usedRelationNames[] = $relation->getCamelName(); endforeach; ?>
}
41 changes: 15 additions & 26 deletions src/lib/AttributeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class AttributeResolver
*/
public array $relations = [];

/**
* @var array keys contains class names and value contains array of belongs to relations
*/
public array $belongsToRelations = [];

/**
* @var NonDbRelation[]|array
*/
Expand All @@ -60,11 +65,6 @@ class AttributeResolver

private ?Config $config;

/**
* @var AttributeRelation[]|array
*/
public array $inverseRelations = [];

public function __construct(string $schemaName, ComponentSchema $schema, JunctionSchemas $junctions, ?Config $config = null)
{
$this->schemaName = $schemaName;
Expand Down Expand Up @@ -232,6 +232,7 @@ protected function resolveProperty(
->setForeignKeyColumnName($property->fkColName)
->setFakerStub($this->guessFakerStub($attribute, $property))
->setTableName($this->componentSchema->resolveTableName($this->schemaName));

if ($property->isReference()) {
if ($property->isVirtual()) {
throw new InvalidDefinitionException('References not supported for virtual attributes');
Expand Down Expand Up @@ -277,12 +278,17 @@ protected function resolveProperty(
$relation->onDeleteFkConstraint = $property->onDeleteFkConstraint;
if ($property->isRefPointerToSelf()) {
$relation->asSelfReference();
} else { # belongs to relations https://github.com/php-openapi/yii2-openapi/issues/90
$belongsToRelation = Yii::createObject(
AttributeRelation::class,
[$this->schemaName, $this->tableName, $this->schemaName]
)
->asHasOne([$attribute->columnName => $fkProperty->getName()]);
$this->belongsToRelations[$property->getRefClassName()][] = $belongsToRelation;
}
$this->relations[$property->getName()] = $relation;
if (!$property->isRefPointerToSelf()) {
$this->addInverseRelation($relatedClassName, $attribute, $property, $fkProperty);
}
}

if (!$property->isReference() && !$property->hasRefItems()) {
[$min, $max] = $property->guessMinMax();
$attribute->setIsVirtual($property->isVirtual())
Expand Down Expand Up @@ -338,6 +344,7 @@ protected function resolveProperty(
->asHasMany([$foreignPk => $this->componentSchema->getPkName()]);
return;
}

$relatedClassName = $property->getRefClassName();
$relatedTableName = $property->getRefSchema()->resolveTableName($relatedClassName);
if ($this->catchManyToMany(
Expand Down Expand Up @@ -518,22 +525,4 @@ public static function relationName(string $propertyName, ?string $fkColumnName)
}
return $relationName;
}

/**
* @throws InvalidConfigException
*/
public function addInverseRelation(
string $relatedClassName,
Attribute $attribute,
PropertySchema $property,
PropertySchema $fkProperty
): void {
$inverseRelation = Yii::createObject(
AttributeRelation::class,
[$this->schemaName, $this->tableName, $this->schemaName]
)
->asHasOne([$attribute->columnName => $fkProperty->getName()]);
$inverseRelation->setInverse($property->getName());
$this->inverseRelations[$relatedClassName][] = $inverseRelation;
}
}
15 changes: 6 additions & 9 deletions src/lib/SchemaToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function prepareModels(): array

$openApi = $this->config->getOpenApi();
$junctions = $this->findJunctionSchemas();
foreach ($openApi->components->schemas as $schemaName => $openApiSchema) {
foreach ($openApi->components->schemas ?? [] as $schemaName => $openApiSchema) {
$schema = Yii::createObject(ComponentSchema::class, [$openApiSchema, $schemaName]);

if (!$this->canGenerateModel($schemaName, $schema)) {
Expand All @@ -106,18 +106,15 @@ public function prepareModels(): array
/** @var AttributeResolver $resolver */
$resolver = Yii::createObject(AttributeResolver::class, [$schemaName, $schema, $junctions, $this->config]);

// $models[$schemaName] = $resolver->resolve();
$resolvers[$schemaName] = $resolver;
$models[$schemaName] = $resolvers[$schemaName]->resolve();
}

// handle inverse relation
// handle belongs to relation
foreach ($resolvers as $aResolver) {
foreach ($aResolver->inverseRelations as $name => $relations) {
foreach ($relations as $relation) {
/** @var AttributeRelation $relation */
$models[$name]->inverseRelations[] = $relation;
}
foreach ($aResolver->belongsToRelations as $name => $relations) {
/** @var AttributeRelation[] $relations */
$models[$name]->belongsToRelations = [...$models[$name]->belongsToRelations, ...$relations];
}
}

Expand Down Expand Up @@ -153,7 +150,7 @@ public function findJunctionSchemas(): JunctionSchemas
{
$junctions = [];
$openApi = $this->config->getOpenApi();
foreach ($openApi->components->schemas as $schemaName => $openApiSchema) {
foreach ($openApi->components->schemas ?? [] as $schemaName => $openApiSchema) {
/**@var ComponentSchema $schema */
$schema = Yii::createObject(ComponentSchema::class, [$openApiSchema, $schemaName]);
if ($schema->isNonDb()) {
Expand Down
22 changes: 19 additions & 3 deletions src/lib/generators/ControllersGenerator.php
Original file line number Diff line number Diff 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 Down Expand 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
14 changes: 7 additions & 7 deletions src/lib/items/DbModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ class DbModel extends BaseObject
*/
public array $many2many = [];

/**
* @var array|AttributeRelation[] inverse relations
*/
public array $inverseRelations = [];

public array $junctionCols = [];

/**
Expand All @@ -91,9 +86,9 @@ class DbModel extends BaseObject
public bool $descriptionIsComment = false;

/**
* @var array Automatically generated scenarios from the model 'x-scenarios'.
* @var AttributeRelation[] belongs to relations
*/
private $_scenarios;
public array $belongsToRelations = [];

/**
* @var bool
Expand All @@ -103,6 +98,11 @@ class DbModel extends BaseObject
*/
public $drop = false;

/**
* @var array Automatically generated scenarios from the model 'x-scenarios'.
*/
private $_scenarios;

public function getTableAlias(): string
{
return '{{%' . $this->tableName . '}}';
Expand Down
3 changes: 2 additions & 1 deletion tests/specs/blog/models/base/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public function getPosts()
return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
}

# belongs to relation
public function getPost()
{
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id']);
}
}
3 changes: 2 additions & 1 deletion tests/specs/blog/models/base/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public function getComments()
return $this->hasMany(\app\models\Comment::class, ['post_id' => 'uid'])->inverseOf('post');
}

# belongs to relation
public function getComment()
{
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'uid'])->inverseOf('post');
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'uid']);
}
}
8 changes: 5 additions & 3 deletions tests/specs/blog/models/base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ public function rules()
];
}

# belongs to relation
public function getPost()
{
return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id'])->inverseOf('created_by');
return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id']);
}

public function getComment2()
# belongs to relation
public function getComment()
{
return $this->hasOne(\app\models\Comment::class, ['author_id' => 'id'])->inverseOf('author');
return $this->hasOne(\app\models\Comment::class, ['author_id' => 'id']);
}
}
2 changes: 2 additions & 0 deletions tests/specs/blog_v2/controllers/CommentController.php
Original file line number Diff line number Diff 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
3 changes: 2 additions & 1 deletion tests/specs/blog_v2/models/base/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public function getPosts()
return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
}

# belongs to relation
public function getPost()
{
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id']);
}
}
3 changes: 2 additions & 1 deletion tests/specs/blog_v2/models/base/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ public function getTags()
->viaTable('posts2tags', ['post_id' => 'id']);
}

# belongs to relation
public function getComment()
{
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'id'])->inverseOf('post');
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'id']);
}
}
8 changes: 5 additions & 3 deletions tests/specs/blog_v2/models/base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ public function rules()
];
}

# belongs to relation
public function getPost()
{
return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id'])->inverseOf('created_by');
return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id']);
}

public function getComment2()
# belongs to relation
public function getComment()
{
return $this->hasOne(\app\models\Comment::class, ['user_id' => 'id'])->inverseOf('user');
return $this->hasOne(\app\models\Comment::class, ['user_id' => 'id']);
}
}
3 changes: 2 additions & 1 deletion tests/specs/fk_col_name/app/models/base/Delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public function rules()
];
}

# belongs to relation
public function getWebhook()
{
return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id'])->inverseOf('redelivery_of');
return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id']);
}
}
3 changes: 2 additions & 1 deletion tests/specs/fk_col_name/app/models/base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public function rules()
];
}

# belongs to relation
public function getWebhook()
{
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id'])->inverseOf('user');
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id']);
}
}
6 changes: 4 additions & 2 deletions tests/specs/fk_col_name_index/app/models/base/Delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ public function rules()
];
}

# belongs to relation
public function getWebhook()
{
return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id'])->inverseOf('redelivery_of');
return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id']);
}

# belongs to relation
public function getWebhook2()
{
return $this->hasOne(\app\models\Webhook::class, ['rd_abc_2' => 'id'])->inverseOf('rd2');
return $this->hasOne(\app\models\Webhook::class, ['rd_abc_2' => 'id']);
}
}
3 changes: 2 additions & 1 deletion tests/specs/fk_col_name_index/app/models/base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public function rules()
];
}

# belongs to relation
public function getWebhook()
{
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id'])->inverseOf('user');
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public function rules()
];
}

# belongs to relation
public function getContact()
{
return $this->hasOne(\app\models\Contact::class, ['mailing_id' => 'id'])->inverseOf('mailing');
return $this->hasOne(\app\models\Contact::class, ['mailing_id' => 'id']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public function rules()
return [];
}

# belongs to relation
public function getOrder()
{
return $this->hasOne(\app\models\Order::class, ['invoice_id' => 'id'])->inverseOf('invoice');
return $this->hasOne(\app\models\Order::class, ['invoice_id' => 'id']);
}
}
Loading