Skip to content

Commit 518a3d3

Browse files
committed
added failing test
1 parent fb736f7 commit 518a3d3

6 files changed

Lines changed: 87 additions & 5 deletions

File tree

src/ReferenceContext.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public function __construct(?SpecObjectInterface $base, string $uri, $cache = nu
5151
$this->_cache = $cache ?? new ReferenceContextCache();
5252
}
5353

54+
public function getCache(): ReferenceContextCache
55+
{
56+
return $this->_cache;
57+
}
58+
5459
/**
5560
* @throws UnresolvableReferenceException in case an invalid or non-absolute URI is provided.
5661
*/
@@ -150,8 +155,6 @@ private function dirname($path)
150155
return '';
151156
}
152157

153-
private $_fileCache;
154-
155158
/**
156159
* Fetch referenced file by URI.
157160
*
@@ -203,7 +206,7 @@ public function resolveReferenceData($uri, JsonPointer $pointer, $data, $toType)
203206

204207
// transitive reference
205208
if (isset($referencedData['$ref'])) {
206-
return (new Reference($referencedData, $toType))->resolve(new ReferenceContext(null, $uri));
209+
return (new Reference($referencedData, $toType))->resolve(new ReferenceContext(null, $uri, $this->_cache));
207210
}
208211
/** @var SpecObjectInterface|array $referencedObject */
209212
$referencedObject = $toType !== null ? new $toType($referencedData) : $referencedData;

src/spec/Reference.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,15 @@ public function resolve(ReferenceContext $context = null)
220220
$referencedObject = $context->resolveReferenceData($file, $jsonReference->getJsonPointer(), $referencedDocument, $this->_to);
221221

222222
if ($jsonReference->getJsonPointer()->getPointer() === '') {
223-
$newContext = new ReferenceContext($referencedObject instanceof SpecObjectInterface ? $referencedObject : null, $file);
223+
$newContext = new ReferenceContext($referencedObject instanceof SpecObjectInterface ? $referencedObject : null, $file, $context->getCache());
224224
if ($referencedObject instanceof DocumentContextInterface) {
225225
$referencedObject->setDocumentContext($referencedObject, $jsonReference->getJsonPointer());
226226
}
227227
} else {
228228
// resolving references recursively does not work the same if we have not referenced
229229
// the whole document. We do not know the base type of the file at this point,
230230
// so base document must be null.
231-
$newContext = new ReferenceContext(null, $file);
231+
$newContext = new ReferenceContext(null, $file, $context->getCache());
232232
}
233233
$newContext->throwException = $context->throwException;
234234
if ($referencedObject instanceof SpecObjectInterface) {

tests/spec/ReferenceTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,4 +416,48 @@ public function testTransitiveReferenceCyclic()
416416

417417
$openapi->resolveReferences(new \cebe\openapi\ReferenceContext($openapi, 'file:///tmp/openapi.yaml'));
418418
}
419+
420+
public function testResolveRelativePath()
421+
{
422+
$openapi = Reader::readFromYamlFile(__DIR__ . '/data/reference/openapi_models.yaml');
423+
424+
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
425+
426+
$this->assertEquals(
427+
<<<YAML
428+
openapi: 3.0.3
429+
info:
430+
title: 'Link Example'
431+
version: 1.0.0
432+
paths:
433+
/pet:
434+
get:
435+
responses:
436+
'200':
437+
description: 'return a pet'
438+
components:
439+
schemas:
440+
Pet:
441+
type: object
442+
properties:
443+
id:
444+
type: integer
445+
format: int64
446+
cat:
447+
\$ref: '#/components/schemas/Cat'
448+
description: 'A Pet'
449+
Cat:
450+
type: object
451+
properties:
452+
id:
453+
type: integer
454+
format: int64
455+
name:
456+
type: string
457+
description: 'the cats name'
458+
description: 'A Cat'
459+
460+
YAML
461+
, $yaml);
462+
}
419463
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type: object
2+
description: "A Cat"
3+
properties:
4+
id:
5+
type: integer
6+
format: int64
7+
name:
8+
type: string
9+
description: the cats name
10+
pet:
11+
$ref: '../openapi_models.yaml#/components/schemas/Pet'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type: object
2+
description: "A Pet"
3+
properties:
4+
id:
5+
type: integer
6+
format: int64
7+
cat:
8+
$ref: '../openapi_models.yaml#/components/schemas/Cat'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Link Example
4+
version: 1.0.0
5+
components:
6+
schemas:
7+
Pet:
8+
$ref: models/Pet.yaml
9+
Cat:
10+
$ref: models/Cat.yaml
11+
paths:
12+
'/pet':
13+
get:
14+
responses:
15+
200:
16+
description: return a pet

0 commit comments

Comments
 (0)