Skip to content

Commit 649743c

Browse files
authored
Merge pull request #99 from php-openapi/fix-conflicting-pull-requests
Fix conflicting pull requests
2 parents 75a8cf0 + 4d32a93 commit 649743c

File tree

62 files changed

+1015
-89
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1015
-89
lines changed

src/generator/default/dbmodel.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,14 @@ public function get<?= $relation->getCamelName() ?>()
146146
<?php endif;?>
147147
}
148148
<?php endforeach; ?>
149-
<?php $i = 1; foreach ($model->inverseRelations as $relationName => $relation): ?>
149+
<?php $i = 1; $usedRelationNames = [];
150+
foreach ($model->belongsToRelations as $relationName => $relation): ?><?php $number = in_array($relation->getCamelName(), $usedRelationNames) ? $i : '' ?>
150151

151-
public function get<?= $relation->getCamelName().($i===1 ? '' : $i) ?>()
152+
# belongs to relation
153+
public function get<?= $relation->getCamelName() . ($number) ?>()
152154
{
153155
return $this-><?= $relation->getMethod() ?>(\<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?>::class, <?php
154-
echo $relation->linkToString() ?>)->inverseOf('<?= $relation->getInverse() ?>');
156+
echo $relation->linkToString() ?>);
155157
}
156-
<?php $i++; endforeach; ?>
158+
<?php $i++; $usedRelationNames[] = $relation->getCamelName(); endforeach; ?>
157159
}

src/lib/AttributeResolver.php

+15-26
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class AttributeResolver
3737
*/
3838
public array $relations = [];
3939

40+
/**
41+
* @var array keys contains class names and value contains array of belongs to relations
42+
*/
43+
public array $belongsToRelations = [];
44+
4045
/**
4146
* @var NonDbRelation[]|array
4247
*/
@@ -60,11 +65,6 @@ class AttributeResolver
6065

6166
private ?Config $config;
6267

63-
/**
64-
* @var AttributeRelation[]|array
65-
*/
66-
public array $inverseRelations = [];
67-
6868
public function __construct(string $schemaName, ComponentSchema $schema, JunctionSchemas $junctions, ?Config $config = null)
6969
{
7070
$this->schemaName = $schemaName;
@@ -232,6 +232,7 @@ protected function resolveProperty(
232232
->setForeignKeyColumnName($property->fkColName)
233233
->setFakerStub($this->guessFakerStub($attribute, $property))
234234
->setTableName($this->componentSchema->resolveTableName($this->schemaName));
235+
235236
if ($property->isReference()) {
236237
if ($property->isVirtual()) {
237238
throw new InvalidDefinitionException('References not supported for virtual attributes');
@@ -277,12 +278,17 @@ protected function resolveProperty(
277278
$relation->onDeleteFkConstraint = $property->onDeleteFkConstraint;
278279
if ($property->isRefPointerToSelf()) {
279280
$relation->asSelfReference();
281+
} else { # belongs to relations https://github.com/php-openapi/yii2-openapi/issues/90
282+
$belongsToRelation = Yii::createObject(
283+
AttributeRelation::class,
284+
[$this->schemaName, $this->tableName, $this->schemaName]
285+
)
286+
->asHasOne([$attribute->columnName => $fkProperty->getName()]);
287+
$this->belongsToRelations[$property->getRefClassName()][] = $belongsToRelation;
280288
}
281289
$this->relations[$property->getName()] = $relation;
282-
if (!$property->isRefPointerToSelf()) {
283-
$this->addInverseRelation($relatedClassName, $attribute, $property, $fkProperty);
284-
}
285290
}
291+
286292
if (!$property->isReference() && !$property->hasRefItems()) {
287293
[$min, $max] = $property->guessMinMax();
288294
$attribute->setIsVirtual($property->isVirtual())
@@ -338,6 +344,7 @@ protected function resolveProperty(
338344
->asHasMany([$foreignPk => $this->componentSchema->getPkName()]);
339345
return;
340346
}
347+
341348
$relatedClassName = $property->getRefClassName();
342349
$relatedTableName = $property->getRefSchema()->resolveTableName($relatedClassName);
343350
if ($this->catchManyToMany(
@@ -518,22 +525,4 @@ public static function relationName(string $propertyName, ?string $fkColumnName)
518525
}
519526
return $relationName;
520527
}
521-
522-
/**
523-
* @throws InvalidConfigException
524-
*/
525-
public function addInverseRelation(
526-
string $relatedClassName,
527-
Attribute $attribute,
528-
PropertySchema $property,
529-
PropertySchema $fkProperty
530-
): void {
531-
$inverseRelation = Yii::createObject(
532-
AttributeRelation::class,
533-
[$this->schemaName, $this->tableName, $this->schemaName]
534-
)
535-
->asHasOne([$attribute->columnName => $fkProperty->getName()]);
536-
$inverseRelation->setInverse($property->getName());
537-
$this->inverseRelations[$relatedClassName][] = $inverseRelation;
538-
}
539528
}

src/lib/SchemaToDatabase.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function prepareModels(): array
9494

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

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

109-
// $models[$schemaName] = $resolver->resolve();
110109
$resolvers[$schemaName] = $resolver;
111110
$models[$schemaName] = $resolvers[$schemaName]->resolve();
112111
}
113112

114-
// handle inverse relation
113+
// handle belongs to relation
115114
foreach ($resolvers as $aResolver) {
116-
foreach ($aResolver->inverseRelations as $name => $relations) {
117-
foreach ($relations as $relation) {
118-
/** @var AttributeRelation $relation */
119-
$models[$name]->inverseRelations[] = $relation;
120-
}
115+
foreach ($aResolver->belongsToRelations as $name => $relations) {
116+
/** @var AttributeRelation[] $relations */
117+
$models[$name]->belongsToRelations = [...$models[$name]->belongsToRelations, ...$relations];
121118
}
122119
}
123120

@@ -153,7 +150,7 @@ public function findJunctionSchemas(): JunctionSchemas
153150
{
154151
$junctions = [];
155152
$openApi = $this->config->getOpenApi();
156-
foreach ($openApi->components->schemas as $schemaName => $openApiSchema) {
153+
foreach ($openApi->components->schemas ?? [] as $schemaName => $openApiSchema) {
157154
/**@var ComponentSchema $schema */
158155
$schema = Yii::createObject(ComponentSchema::class, [$openApiSchema, $schemaName]);
159156
if ($schema->isNonDb()) {

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]);

src/lib/items/DbModel.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ class DbModel extends BaseObject
6767
*/
6868
public array $many2many = [];
6969

70-
/**
71-
* @var array|AttributeRelation[] inverse relations
72-
*/
73-
public array $inverseRelations = [];
74-
7570
public array $junctionCols = [];
7671

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

9388
/**
94-
* @var array Automatically generated scenarios from the model 'x-scenarios'.
89+
* @var AttributeRelation[] belongs to relations
9590
*/
96-
private $_scenarios;
91+
public array $belongsToRelations = [];
9792

9893
/**
9994
* @var bool
@@ -103,6 +98,11 @@ class DbModel extends BaseObject
10398
*/
10499
public $drop = false;
105100

101+
/**
102+
* @var array Automatically generated scenarios from the model 'x-scenarios'.
103+
*/
104+
private $_scenarios;
105+
106106
public function getTableAlias(): string
107107
{
108108
return '{{%' . $this->tableName . '}}';

tests/specs/blog/models/base/Category.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ public function getPosts()
3939
return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
4040
}
4141

42+
# belongs to relation
4243
public function getPost()
4344
{
44-
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
45+
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id']);
4546
}
4647
}

tests/specs/blog/models/base/Post.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ public function getComments()
6262
return $this->hasMany(\app\models\Comment::class, ['post_id' => 'uid'])->inverseOf('post');
6363
}
6464

65+
# belongs to relation
6566
public function getComment()
6667
{
67-
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'uid'])->inverseOf('post');
68+
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'uid']);
6869
}
6970
}

tests/specs/blog/models/base/User.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ public function rules()
4545
];
4646
}
4747

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

53-
public function getComment2()
54+
# belongs to relation
55+
public function getComment()
5456
{
55-
return $this->hasOne(\app\models\Comment::class, ['author_id' => 'id'])->inverseOf('author');
57+
return $this->hasOne(\app\models\Comment::class, ['author_id' => 'id']);
5658
}
5759
}

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/blog_v2/models/base/Category.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ public function getPosts()
3939
return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
4040
}
4141

42+
# belongs to relation
4243
public function getPost()
4344
{
44-
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
45+
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id']);
4546
}
4647
}

tests/specs/blog_v2/models/base/Post.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ public function getTags()
7474
->viaTable('posts2tags', ['post_id' => 'id']);
7575
}
7676

77+
# belongs to relation
7778
public function getComment()
7879
{
79-
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'id'])->inverseOf('post');
80+
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'id']);
8081
}
8182
}

tests/specs/blog_v2/models/base/User.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ public function rules()
4848
];
4949
}
5050

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

56-
public function getComment2()
57+
# belongs to relation
58+
public function getComment()
5759
{
58-
return $this->hasOne(\app\models\Comment::class, ['user_id' => 'id'])->inverseOf('user');
60+
return $this->hasOne(\app\models\Comment::class, ['user_id' => 'id']);
5961
}
6062
}

tests/specs/fk_col_name/app/models/base/Delivery.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ public function rules()
2828
];
2929
}
3030

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

tests/specs/fk_col_name/app/models/base/User.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public function rules()
2929
];
3030
}
3131

32+
# belongs to relation
3233
public function getWebhook()
3334
{
34-
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id'])->inverseOf('user');
35+
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id']);
3536
}
3637
}

tests/specs/fk_col_name_index/app/models/base/Delivery.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ public function rules()
2828
];
2929
}
3030

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

37+
# belongs to relation
3638
public function getWebhook2()
3739
{
38-
return $this->hasOne(\app\models\Webhook::class, ['rd_abc_2' => 'id'])->inverseOf('rd2');
40+
return $this->hasOne(\app\models\Webhook::class, ['rd_abc_2' => 'id']);
3941
}
4042
}

tests/specs/fk_col_name_index/app/models/base/User.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public function rules()
2929
];
3030
}
3131

32+
# belongs to relation
3233
public function getWebhook()
3334
{
34-
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id'])->inverseOf('user');
35+
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id']);
3536
}
3637
}

tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Mailing.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public function rules()
3131
];
3232
}
3333

34+
# belongs to relation
3435
public function getContact()
3536
{
36-
return $this->hasOne(\app\models\Contact::class, ['mailing_id' => 'id'])->inverseOf('mailing');
37+
return $this->hasOne(\app\models\Contact::class, ['mailing_id' => 'id']);
3738
}
3839
}

tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/base/Invoice.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ public function rules()
2424
return [];
2525
}
2626

27+
# belongs to relation
2728
public function getOrder()
2829
{
29-
return $this->hasOne(\app\models\Order::class, ['invoice_id' => 'id'])->inverseOf('invoice');
30+
return $this->hasOne(\app\models\Order::class, ['invoice_id' => 'id']);
3031
}
3132
}

0 commit comments

Comments
 (0)