diff --git a/src/Reader.php b/src/Reader.php index 1d3a066..4d40031 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -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); diff --git a/tests/ReaderTest.php b/tests/ReaderTest.php index 0230a49..742c3a4 100644 --- a/tests/ReaderTest.php +++ b/tests/ReaderTest.php @@ -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); + } }