Skip to content

Commit 03c5e82

Browse files
committed
fix resolving references to a file without jsonPointer
1 parent 4018163 commit 03c5e82

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

src/spec/Paths.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ public function getIterator()
192192
public function resolveReferences(ReferenceContext $context)
193193
{
194194
foreach ($this->_paths as $key => $path) {
195+
if ($path === null) {
196+
continue;
197+
}
195198
$path->resolveReferences($context);
196199
}
197200
}

src/spec/Reference.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function resolve(ReferenceContext $context)
102102

103103
$file = ($pos === false) ? $this->_ref : substr($this->_ref, 0, $pos);
104104
$file = $context->resolveRelativeUri($file);
105-
$jsonPointer = substr($this->_ref, $pos + 1);
105+
$jsonPointer = ($pos === false) ? '' : substr($this->_ref, $pos + 1);
106106

107107
// TODO could be a good idea to cache loaded files in current context to avoid loading the same files over and over again
108108
$fileContent = $this->fetchReferencedFile($file);

tests/spec/ReferenceTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ public function testResolveFile()
153153
$this->assertInstanceOf(Schema::class, $petItems = $openapi->components->schemas['Dog']);
154154
$this->assertArrayHasKey('id', $openapi->components->schemas['Pet']->properties);
155155
$this->assertArrayHasKey('name', $openapi->components->schemas['Dog']->properties);
156+
157+
// second level reference inside of definitions.yaml
158+
$this->assertArrayHasKey('food', $openapi->components->schemas['Dog']->properties);
159+
$this->assertInstanceOf(Reference::class, $openapi->components->schemas['Dog']->properties['food']);
160+
161+
$openapi->resolveReferences(new \cebe\openapi\ReferenceContext($openapi, $file));
162+
163+
$this->assertArrayHasKey('food', $openapi->components->schemas['Dog']->properties);
164+
$this->assertInstanceOf(Schema::class, $openapi->components->schemas['Dog']->properties['food']);
165+
$this->assertArrayHasKey('id', $openapi->components->schemas['Dog']->properties['food']->properties);
166+
$this->assertArrayHasKey('name', $openapi->components->schemas['Dog']->properties['food']->properties);
167+
$this->assertEquals(1, $openapi->components->schemas['Dog']->properties['food']->properties['id']->example);
156168
}
157169

158170
public function testResolveFileHttp()

tests/spec/data/reference/Food.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
properties:
2+
id:
3+
type: integer
4+
example: 1
5+
name:
6+
type: string

tests/spec/data/reference/definitions.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ Dog:
99
properties:
1010
name:
1111
type: string
12+
food:
13+
$ref: 'Food.yaml'

0 commit comments

Comments
 (0)