Skip to content

Commit 3b4dbda

Browse files
authored
Merge pull request #71 from php-openapi/68-inverse-relation-must-not-be-generated-for-self-referenced-component-schema
Resolve: Inverse relation must not be generated for self-referenced component schema #68
2 parents 36c04a7 + 89f1d2c commit 3b4dbda

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/lib/openapi/PropertySchema.php

+18
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,24 @@ public function isRefPointerToSchema():bool
269269

270270
public function isRefPointerToSelf():bool
271271
{
272+
$allOfInSchema = null;
273+
if (isset($this->schema->getSchema()->properties[$this->name]->allOf)) {
274+
$allOfInSchema = $this->schema->getSchema()->properties[$this->name]->allOf;
275+
}
276+
277+
if ($allOfInSchema) { # fixes https://github.com/php-openapi/yii2-openapi/issues/68
278+
$refCounter = 0;
279+
foreach ($allOfInSchema as $aAllOfElement) {
280+
if ($aAllOfElement instanceof Reference) {
281+
$refCounter++;
282+
}
283+
}
284+
if ($refCounter === 1) {
285+
return $this->isRefPointerToSchema()
286+
&& str_ends_with($this->refPointer, '/' . $this->schema->getName()) !== false;
287+
}
288+
}
289+
272290
return $this->isRefPointerToSchema()
273291
&& strpos($this->refPointer, '/' . $this->schema->getName() . '/') !== false
274292
&& strpos($this->refPointer, '/properties/') !== false;

tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Invoice.php

-10
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,4 @@ public function getAnimal()
7878
{
7979
return $this->hasOne(\app\models\Animal::class, ['id' => 'animal_id']);
8080
}
81-
82-
public function getInvoice()
83-
{
84-
return $this->hasOne(\app\models\Invoice::class, ['reference_invoice_id' => 'id'])->inverseOf('reference_invoice');
85-
}
86-
87-
public function getInvoice2()
88-
{
89-
return $this->hasOne(\app\models\Invoice::class, ['reference_invoice_2_id' => 'id'])->inverseOf('reference_invoice_2');
90-
}
9181
}

0 commit comments

Comments
 (0)