Skip to content

Commit bccf2a8

Browse files
committed
In progress
1 parent 8ab3810 commit bccf2a8

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/lib/openapi/PropertySchema.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class PropertySchema
7070

7171
/** @var string $refPointer */
7272
private $refPointer;
73+
private $uri;
7374

7475
/** @var \cebe\yii2openapi\lib\openapi\ComponentSchema $refSchema */
7576
private $refSchema;
@@ -170,6 +171,7 @@ private function initReference():void
170171
{
171172
$this->isReference = true;
172173
$this->refPointer = $this->property->getJsonReference()->getJsonPointer()->getPointer();
174+
$this->uri = $this->property->getJsonReference()->getDocumentUri();
173175
$refSchemaName = $this->getRefSchemaName();
174176
if ($this->isRefPointerToSelf()) {
175177
$this->refSchema = $this->schema;
@@ -194,6 +196,7 @@ private function initItemsReference():void
194196
return;
195197
}
196198
$this->refPointer = $items->getJsonReference()->getJsonPointer()->getPointer();
199+
$this->uri = $this->property->getJsonReference()->getDocumentUri();
197200
if ($this->isRefPointerToSelf()) {
198201
$this->refSchema = $this->schema;
199202
} elseif ($this->isRefPointerToSchema()) {
@@ -301,8 +304,15 @@ public function getRefSchemaName():string
301304
'~^'.self::REFERENCE_PATH.'(?<schemaName>.+)/properties/(?<propName>.+)$~'
302305
: '~^'.self::REFERENCE_PATH.'(?<schemaName>.+)$~';
303306
if (!\preg_match($pattern, $this->refPointer, $matches)) {
304-
throw new InvalidDefinitionException('Invalid schema reference');
307+
$pattern = '/((\.\/)*)(?<schemaName>.+)(\.)(yml|yaml)(.*)/';
308+
// $pattern = '~^(?<schemaName>.+)(\.+)(yaml+)(.*)$~';
309+
if (strpos($this->uri, '#') !== false && !\preg_match($pattern, $this->uri, $matches)) {
310+
// throw new InvalidDefinitionException('Invalid schema reference');
311+
} else {
312+
throw new InvalidDefinitionException('Invalid schema reference');
313+
}
305314
}
315+
var_dump($matches);
306316
return $matches['schemaName'];
307317
}
308318

tests/specs/issue_fix/74_invalid_schema_reference_error/index.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ components:
1717
id:
1818
type: integer
1919
vat_rate:
20-
# $ref: '#/components/schemas/Product/properties/vat_rate' # issue is not observed
20+
# $ref: '#/components/schemas/Product/properties/vat_rate' # issue is not observed
2121
$ref: './Product.yaml#/properties/vat_rate' # issue is observed
2222
Product:
2323
type: object

tests/unit/IssueFixTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,14 @@ public function test63JustColumnNameRename()
966966
// https://github.com/php-openapi/yii2-openapi/issues/63
967967
public function test74InvalidSchemaReferenceError()
968968
{
969+
// $pattern = '~^(?<schemaName>.+)(\.+)(yaml+)(.*)$~';
970+
// $pattern = '/((\.\/)*)(?<schemaName>.+)(\.)(yml|yaml)(.*)/';
971+
//
972+
// $pointer = './Product.yaml#/properties/vat_rate';
973+
// preg_match($pattern, $pointer, $matches);
974+
// $this->assertSame('supda saf', $matches);
975+
// return;
976+
969977
$testFile = Yii::getAlias("@specs/issue_fix/74_invalid_schema_reference_error/index.php");
970978

971979
$this->runGenerator($testFile);

0 commit comments

Comments
 (0)