From 4f137bc24b0b6edd6db571b17dafaacb830f68ab Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 3 Dec 2024 19:54:56 +0530 Subject: [PATCH 1/3] Fix this issue --- src/lib/openapi/PropertySchema.php | 16 ++++++++++++++++ .../mysql/models/base/Invoice.php | 10 ---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/lib/openapi/PropertySchema.php b/src/lib/openapi/PropertySchema.php index 38f1873c..b366e06b 100644 --- a/src/lib/openapi/PropertySchema.php +++ b/src/lib/openapi/PropertySchema.php @@ -269,6 +269,22 @@ public function isRefPointerToSchema():bool public function isRefPointerToSelf():bool { + $schema = Json::decode(Json::encode($this->schema->getSchema()->getSerializableData())); + + if (isset($schema['properties'][$this->name]['allOf'])) { # fixes https://github.com/php-openapi/yii2-openapi/issues/68 + $allOfs = $schema['properties'][$this->name]['allOf']; + $refCounter = 0; + foreach ($allOfs as $allOf) { + if (isset($allOf['$ref'])) { + $refCounter++; + } + } + if ($refCounter === 1) { + return $this->isRefPointerToSchema() + && str_ends_with($this->refPointer, '/' . $this->schema->getName()) !== false; + } + } + return $this->isRefPointerToSchema() && strpos($this->refPointer, '/' . $this->schema->getName() . '/') !== false && strpos($this->refPointer, '/properties/') !== false; diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Invoice.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Invoice.php index 73190e01..8e944b9b 100644 --- a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Invoice.php +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Invoice.php @@ -78,14 +78,4 @@ public function getAnimal() { return $this->hasOne(\app\models\Animal::class, ['id' => 'animal_id']); } - - public function getInvoice() - { - return $this->hasOne(\app\models\Invoice::class, ['reference_invoice_id' => 'id'])->inverseOf('reference_invoice'); - } - - public function getInvoice2() - { - return $this->hasOne(\app\models\Invoice::class, ['reference_invoice_2_id' => 'id'])->inverseOf('reference_invoice_2'); - } } From c1402a964f781ff222355bfe43dda074cd8b80c9 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 11 Dec 2024 19:44:59 +0530 Subject: [PATCH 2/3] Implement feedback --- src/lib/openapi/PropertySchema.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib/openapi/PropertySchema.php b/src/lib/openapi/PropertySchema.php index b366e06b..519656cd 100644 --- a/src/lib/openapi/PropertySchema.php +++ b/src/lib/openapi/PropertySchema.php @@ -269,13 +269,15 @@ public function isRefPointerToSchema():bool public function isRefPointerToSelf():bool { - $schema = Json::decode(Json::encode($this->schema->getSchema()->getSerializableData())); + $allOfsInSchema = null; + if (isset($this->schema->getSchema()->properties[$this->name]->allOf)) { + $allOfsInSchema = $this->schema->getSchema()->properties[$this->name]->allOf; + } - if (isset($schema['properties'][$this->name]['allOf'])) { # fixes https://github.com/php-openapi/yii2-openapi/issues/68 - $allOfs = $schema['properties'][$this->name]['allOf']; + if ($allOfsInSchema) { # fixes https://github.com/php-openapi/yii2-openapi/issues/68 $refCounter = 0; - foreach ($allOfs as $allOf) { - if (isset($allOf['$ref'])) { + foreach ($allOfsInSchema as $allOf) { + if ($allOf instanceof Reference) { $refCounter++; } } From 89f1d2c9b4d86f58b02b43cdb4a04c5c1cbd1dc3 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 11 Dec 2024 20:17:34 +0530 Subject: [PATCH 3/3] Refactor: better variable naming --- src/lib/openapi/PropertySchema.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/openapi/PropertySchema.php b/src/lib/openapi/PropertySchema.php index 519656cd..9ff64094 100644 --- a/src/lib/openapi/PropertySchema.php +++ b/src/lib/openapi/PropertySchema.php @@ -269,15 +269,15 @@ public function isRefPointerToSchema():bool public function isRefPointerToSelf():bool { - $allOfsInSchema = null; + $allOfInSchema = null; if (isset($this->schema->getSchema()->properties[$this->name]->allOf)) { - $allOfsInSchema = $this->schema->getSchema()->properties[$this->name]->allOf; + $allOfInSchema = $this->schema->getSchema()->properties[$this->name]->allOf; } - if ($allOfsInSchema) { # fixes https://github.com/php-openapi/yii2-openapi/issues/68 + if ($allOfInSchema) { # fixes https://github.com/php-openapi/yii2-openapi/issues/68 $refCounter = 0; - foreach ($allOfsInSchema as $allOf) { - if ($allOf instanceof Reference) { + foreach ($allOfInSchema as $aAllOfElement) { + if ($aAllOfElement instanceof Reference) { $refCounter++; } }