Skip to content

Commit b130a4f

Browse files
committed
Fix this issue
1 parent bccf2a8 commit b130a4f

File tree

3 files changed

+9
-26
lines changed

3 files changed

+9
-26
lines changed

src/lib/openapi/PropertySchema.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private function initItemsReference():void
196196
return;
197197
}
198198
$this->refPointer = $items->getJsonReference()->getJsonPointer()->getPointer();
199-
$this->uri = $this->property->getJsonReference()->getDocumentUri();
199+
$this->uri = $items->getJsonReference()->getDocumentUri();
200200
if ($this->isRefPointerToSelf()) {
201201
$this->refSchema = $this->schema;
202202
} elseif ($this->isRefPointerToSchema()) {
@@ -267,7 +267,9 @@ public function getSelfTargetProperty():?PropertySchema
267267

268268
public function isRefPointerToSchema():bool
269269
{
270-
return $this->refPointer && strpos($this->refPointer, self::REFERENCE_PATH) === 0;
270+
return $this->refPointer &&
271+
((strpos($this->refPointer, self::REFERENCE_PATH) === 0) ||
272+
(str_ends_with($this->uri, '.yml')) || (str_ends_with($this->uri, '.yaml')));
271273
}
272274

273275
public function isRefPointerToSelf():bool
@@ -303,16 +305,14 @@ public function getRefSchemaName():string
303305
$pattern = strpos($this->refPointer, '/properties/') !== false ?
304306
'~^'.self::REFERENCE_PATH.'(?<schemaName>.+)/properties/(?<propName>.+)$~'
305307
: '~^'.self::REFERENCE_PATH.'(?<schemaName>.+)$~';
308+
$separateFilePattern = '/((\.\/)*)(?<schemaName>.+)(\.)(yml|yaml)(.*)/'; # https://github.com/php-openapi/yii2-openapi/issues/74
306309
if (!\preg_match($pattern, $this->refPointer, $matches)) {
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 {
310+
if (!\preg_match($separateFilePattern, $this->uri, $separateFilePatternMatches)) {
312311
throw new InvalidDefinitionException('Invalid schema reference');
312+
} else {
313+
return $separateFilePatternMatches['schemaName'];
313314
}
314315
}
315-
var_dump($matches);
316316
return $matches['schemaName'];
317317
}
318318

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

-8
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@ required:
77
- id
88
- vat_rate
99

10-
1110
properties:
12-
1311
id:
1412
type: integer
15-
example: 12531
16-
readOnly: true
17-
18-
1913
vat_rate:
2014
type: string
2115
enum:
2216
- standard
23-
- reduced
24-
- custom
2517
- none
2618
default: standard

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

+1-10
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,4 @@ components:
2020
# $ref: '#/components/schemas/Product/properties/vat_rate' # issue is not observed
2121
$ref: './Product.yaml#/properties/vat_rate' # issue is observed
2222
Product:
23-
type: object
24-
properties:
25-
id:
26-
type: integer
27-
vat_rate:
28-
type: string
29-
enum:
30-
- normal
31-
- none
32-
default: normal
23+
$ref: ./Product.yaml

0 commit comments

Comments
 (0)