From 36aa9352d8077f545d8e822464d9946d60e99b4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Vitry?= Date: Fri, 13 Oct 2023 12:01:19 +0200 Subject: [PATCH] Update validate command - Fail with invalid path $ref --- bin/php-openapi | 2 +- tests/spec/ReferenceTest.php | 27 ++++++++++++++++++++++++++- tests/spec/data/reference/subdir.yaml | 2 ++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/bin/php-openapi b/bin/php-openapi index ae2ffdc8..bb79fe83 100755 --- a/bin/php-openapi +++ b/bin/php-openapi @@ -124,7 +124,7 @@ switch ($command) { $openApi = read_input($inputFile, $inputFormat); $referenceContext = new ReferenceContext($openApi, $inputFile ? realpath($inputFile) : ''); $referenceContext->throwException = false; - $referenceContext->mode = ReferenceContext::RESOLVE_MODE_INLINE; + $referenceContext->mode = $referenceMode; $openApi->resolveReferences($referenceContext); $openApi->setDocumentContext($openApi, new \cebe\openapi\json\JsonPointer('')); diff --git a/tests/spec/ReferenceTest.php b/tests/spec/ReferenceTest.php index b94c946e..4e314241 100644 --- a/tests/spec/ReferenceTest.php +++ b/tests/spec/ReferenceTest.php @@ -189,7 +189,9 @@ public function testResolveFileInSubdir() $this->assertInstanceOf(Reference::class, $openapi->components->schemas['Dog']); $this->assertInstanceOf(Reference::class, $openapi->components->parameters['Parameter.PetId']); - $openapi->resolveReferences(new \cebe\openapi\ReferenceContext($openapi, $file)); + $referenceContext = new \cebe\openapi\ReferenceContext($openapi, $file); + $referenceContext->throwException = false; + $openapi->resolveReferences($referenceContext); $this->assertInstanceOf(Schema::class, $openapi->components->schemas['Pet']); $this->assertInstanceOf(Schema::class, $openapi->components->schemas['Dog']); @@ -657,4 +659,27 @@ public function testResolveRelativePathAll() } } + public function testResolveFileInSubdirWithFailReference() + { + $file = __DIR__ . '/data/reference/subdir.yaml'; + /** @var $openapi OpenApi */ + $openapi = Reader::readFromYamlFile($file, OpenApi::class, false); + + $referenceContext = new \cebe\openapi\ReferenceContext($openapi, $file); + $referenceContext->throwException = false; + $referenceContext->mode = \cebe\openapi\ReferenceContext::RESOLVE_MODE_ALL; + $openapi->resolveReferences($referenceContext); + + $result = $openapi->validate(); + $this->assertMatchesRegularExpression( + "/Failed to resolve Reference 'subdir\/Foo\/Bar.yaml'/", + $openapi->getErrors()[0] + ); + $this->assertFalse($result); + + $this->assertInstanceOf(Schema::class, $openapi->components->schemas['Pet']); + $this->assertInstanceOf(Schema::class, $openapi->components->schemas['Dog']); + $this->assertInstanceOf(Parameter::class, $openapi->components->parameters['Parameter.PetId']); + + } } diff --git a/tests/spec/data/reference/subdir.yaml b/tests/spec/data/reference/subdir.yaml index 12da957e..132ca6c0 100644 --- a/tests/spec/data/reference/subdir.yaml +++ b/tests/spec/data/reference/subdir.yaml @@ -8,6 +8,8 @@ components: $ref: 'subdir/Pet.yaml' Dog: $ref: 'subdir/Dog.yaml' + Fail: + $ref: 'subdir/Foo/Bar.yaml' parameters: "Parameter.PetId": "$ref": "./subdir/Parameter.PetId.json"