Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public function readFromString(string $openAPI, FileFormat $fileFormat): CebeSpe
throw CannotRead::invalidFormatting($e);
}

$openAPI->resolveReferences(new Cebe\ReferenceContext($openAPI, '/tmp'));
try {
$openAPI->resolveReferences(new Cebe\ReferenceContext($openAPI, '/tmp'));
} catch (CebeException\UnresolvableReferenceException $e) {
throw CannotRead::unresolvedReference($e);
}

$this->validate($openAPI);

Expand Down
59 changes: 59 additions & 0 deletions tests/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,63 @@ public function itCannotResolveExternalReferenceFromString(string $openAPIString
(new Reader([OpenAPIVersion::Version_3_0]))
->readFromString($openAPIString, FileFormat::Json);
}

public static function provideOpenAPIWithInvalidReference(): Generator
{
yield 'missing forward slash after hash' => [
json_encode([
'openapi' => '3.0.0',
'info' => ['title' => 'API With Reference Object', 'version' => '1.0.0'],
'paths' => [
'/path' => [
'get' => [
'operationId' => 'get-path',
'responses' => [
200 => [
'description' => 'Successful Response',
'content' => [
'application/json' => [
'schema' => [
'$ref' => '#components/schemas/Test',
],
],
],
],
],
],
],
],
'components' => [
'schemas' => [
'Test' => [
'type' => 'integer',
]
]
]
]),
];
}

#[Test]
#[DataProvider('provideOpenAPIWithInvalidReference')]
public function itCannotResolveInvalidReferenceFromAbsoluteFilePath(string $openAPIString): void
{
vfsStream::setup();
file_put_contents(vfsStream::url('root/openapi.json'), $openAPIString);

self::expectExceptionObject(CannotRead::unresolvedReference(new CebeException\UnresolvableReferenceException()));

(new Reader([OpenAPIVersion::Version_3_0]))
->readFromAbsoluteFilePath(vfsStream::url('root/openapi.json'));
}

#[Test]
#[DataProvider('provideOpenAPIWithInvalidReference')]
public function itCannotResolveInvalidReferenceFromString(string $openAPIString,): void
{
self::expectExceptionObject(CannotRead::unresolvedReference(new CebeException\UnresolvableReferenceException()));

(new Reader([OpenAPIVersion::Version_3_0]))
->readFromString($openAPIString, FileFormat::Json);
}
}