Skip to content

Commit 16af48f

Browse files
authored
Merge pull request #4 from membrane-php/catch-unresolved-reference
Catch unresolveableReferenceException in readFromString
2 parents cfd65af + 08ea7e3 commit 16af48f

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

src/Reader.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ public function readFromString(string $openAPI, FileFormat $fileFormat): CebeSpe
5959
throw CannotRead::invalidFormatting($e);
6060
}
6161

62-
$openAPI->resolveReferences(new Cebe\ReferenceContext($openAPI, '/tmp'));
62+
try {
63+
$openAPI->resolveReferences(new Cebe\ReferenceContext($openAPI, '/tmp'));
64+
} catch (CebeException\UnresolvableReferenceException $e) {
65+
throw CannotRead::unresolvedReference($e);
66+
}
6367

6468
$this->validate($openAPI);
6569

tests/ReaderTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,4 +428,63 @@ public function itCannotResolveExternalReferenceFromString(string $openAPIString
428428
(new Reader([OpenAPIVersion::Version_3_0]))
429429
->readFromString($openAPIString, FileFormat::Json);
430430
}
431+
432+
public static function provideOpenAPIWithInvalidReference(): Generator
433+
{
434+
yield 'missing forward slash after hash' => [
435+
json_encode([
436+
'openapi' => '3.0.0',
437+
'info' => ['title' => 'API With Reference Object', 'version' => '1.0.0'],
438+
'paths' => [
439+
'/path' => [
440+
'get' => [
441+
'operationId' => 'get-path',
442+
'responses' => [
443+
200 => [
444+
'description' => 'Successful Response',
445+
'content' => [
446+
'application/json' => [
447+
'schema' => [
448+
'$ref' => '#components/schemas/Test',
449+
],
450+
],
451+
],
452+
],
453+
],
454+
],
455+
],
456+
],
457+
'components' => [
458+
'schemas' => [
459+
'Test' => [
460+
'type' => 'integer',
461+
]
462+
]
463+
]
464+
]),
465+
];
466+
}
467+
468+
#[Test]
469+
#[DataProvider('provideOpenAPIWithInvalidReference')]
470+
public function itCannotResolveInvalidReferenceFromAbsoluteFilePath(string $openAPIString): void
471+
{
472+
vfsStream::setup();
473+
file_put_contents(vfsStream::url('root/openapi.json'), $openAPIString);
474+
475+
self::expectExceptionObject(CannotRead::unresolvedReference(new CebeException\UnresolvableReferenceException()));
476+
477+
(new Reader([OpenAPIVersion::Version_3_0]))
478+
->readFromAbsoluteFilePath(vfsStream::url('root/openapi.json'));
479+
}
480+
481+
#[Test]
482+
#[DataProvider('provideOpenAPIWithInvalidReference')]
483+
public function itCannotResolveInvalidReferenceFromString(string $openAPIString,): void
484+
{
485+
self::expectExceptionObject(CannotRead::unresolvedReference(new CebeException\UnresolvableReferenceException()));
486+
487+
(new Reader([OpenAPIVersion::Version_3_0]))
488+
->readFromString($openAPIString, FileFormat::Json);
489+
}
431490
}

0 commit comments

Comments
 (0)