Skip to content

Commit a168e07

Browse files
committed
Avoid inverse relation for self reference
1 parent 5dda237 commit a168e07

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/lib/AttributeResolver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ protected function resolveProperty(
249249
$relation->asSelfReference();
250250
}
251251
$this->relations[$property->getName()] = $relation;
252-
$this->addInverseRelation($relatedClassName, $attribute, $property, $fkProperty);
252+
if (!$property->isRefPointerToSelf()) {
253+
$this->addInverseRelation($relatedClassName, $attribute, $property, $fkProperty);
254+
}
253255
}
254256
if (!$property->isReference() && !$property->hasRefItems()) {
255257
[$min, $max] = $property->guessMinMax();
@@ -498,15 +500,13 @@ public function addInverseRelation(
498500
Attribute $attribute,
499501
PropertySchema $property,
500502
PropertySchema $fkProperty
501-
): void
502-
{
503+
): void {
503504
$inverseRelation = Yii::createObject(
504505
AttributeRelation::class,
505506
[$this->schemaName, $this->tableName, $this->schemaName]
506507
)
507508
->asHasOne([$attribute->columnName => $fkProperty->getName()]);
508509
$inverseRelation->setInverse($property->getName());
509510
$this->inverseRelations[$relatedClassName][] = $inverseRelation;
510-
511511
}
512512
}

tests/specs/issue_fix/25_generate_inverse_relations/index.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ components:
4949
- $ref: '#/components/schemas/User'
5050
- x-fk-column-name: user3
5151

52+
Menu:
53+
required:
54+
- id
55+
- name
56+
properties:
57+
id:
58+
type: integer
59+
format: int64
60+
readOnly: True
61+
name:
62+
type: string
63+
maxLength: 100
64+
minLength: 3
65+
parent:
66+
$ref: '#/components/schemas/Menu/properties/id'
67+
childes:
68+
type: array
69+
items:
70+
$ref: '#/components/schemas/Menu/properties/parent'
71+
5272
paths:
5373
'/account':
5474
get:

0 commit comments

Comments
 (0)