Skip to content

Commit b26cdef

Browse files
committed
fix error with resolving references in objects that implement ArrayAccess
1 parent 4cd7404 commit b26cdef

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

src/json/JsonPointer.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,20 @@ public function evaluate($jsonDocument)
102102

103103
foreach ($this->getPath() as $part) {
104104

105-
if (is_array($currentReference) || $currentReference instanceof \ArrayAccess) {
106-
if (!array_key_exists($part, $currentReference)) {
105+
if (is_array($currentReference)) {
106+
// if (!preg_match('~^([1-9]*[0-9]|-)$~', $part)) {
107+
// throw new NonexistentJsonPointerReferenceException(
108+
// "Failed to evaluate pointer '$this->_pointer'. Invalid pointer path '$part' for Array at path '$currentPath'."
109+
// );
110+
// }
111+
if ($part === '-' || !array_key_exists($part, $currentReference)) {
112+
throw new NonexistentJsonPointerReferenceException(
113+
"Failed to evaluate pointer '$this->_pointer'. Array has no member $part at path '$currentPath'."
114+
);
115+
}
116+
$currentReference = $currentReference[$part];
117+
} elseif ($currentReference instanceof \ArrayAccess) {
118+
if (!$currentReference->offsetExists($part)) {
107119
throw new NonexistentJsonPointerReferenceException(
108120
"Failed to evaluate pointer '$this->_pointer'. Array has no member $part at path '$currentPath'."
109121
);

tests/spec/ReferenceTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use cebe\openapi\Reader;
44
use cebe\openapi\spec\OpenApi;
55
use cebe\openapi\spec\Reference;
6+
use cebe\openapi\spec\RequestBody;
67
use cebe\openapi\spec\Response;
78
use cebe\openapi\spec\Schema;
89
use cebe\openapi\spec\Example;
@@ -193,4 +194,26 @@ public function testResolveFileHttp()
193194
$this->assertArrayHasKey('name', $openapi->components->schemas['Dog']->properties);
194195
}
195196

197+
public function testResolvePaths()
198+
{
199+
/** @var $openapi OpenApi */
200+
$openapi = Reader::readFromJsonFile(__DIR__ . '/data/reference/playlist.json', OpenApi::class, false);
201+
202+
$result = $openapi->validate();
203+
$this->assertEquals([], $openapi->getErrors());
204+
$this->assertTrue($result);
205+
206+
$playlistsBody = $openapi->paths['/playlist']->post->requestBody;
207+
$playlistBody = $openapi->paths['/playlist/{id}']->patch->requestBody;
208+
209+
$this->assertInstanceOf(RequestBody::class, $playlistsBody);
210+
$this->assertInstanceOf(Reference::class, $playlistBody);
211+
212+
$openapi->resolveReferences();
213+
214+
$newPlaylistBody = $openapi->paths['/playlist/{id}']->patch->requestBody;
215+
$this->assertInstanceOf(RequestBody::class, $playlistsBody);
216+
$this->assertInstanceOf(RequestBody::class, $newPlaylistBody);
217+
$this->assertSame($playlistsBody, $newPlaylistBody);
218+
}
196219
}

0 commit comments

Comments
 (0)