Skip to content

Commit 43044d5

Browse files
committed
Catch recursion when resolving inside included file
fixes #144
1 parent a02f958 commit 43044d5

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ install: composer.lock yarn.lock
4141
$(DOCKER_PHP) composer install --prefer-dist --no-interaction --no-progress --ansi
4242
$(DOCKER_NODE) yarn install
4343

44-
test: unit test-recursion.json test-recursion2.yaml test-empty-maps.json
44+
test: unit test-recursion.json test-recursion2.yaml test-recursion3_index.yaml test-empty-maps.json
4545

4646
unit:
4747
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) vendor/bin/phpunit --verbose --colors=always $(TESTCASE)

Diff for: src/spec/Reference.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ private function resolveTransitiveReference(Reference $referencedObject, Referen
289289
return $transitiveRefResult;
290290
}
291291

292+
private $_recursingInsideFile = false;
293+
292294
/**
293295
* Adjust relative references inside of the file to match the context of the base file
294296
*/
@@ -306,7 +308,14 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
306308
// direcly inline references in the same document,
307309
// these are not going to be valid in the new context anymore
308310
$inlineDocument = (new JsonPointer(substr($value, 1)))->evaluate($baseDocument);
309-
return $this->adjustRelativeReferences($inlineDocument, $basePath, $baseDocument, $oContext);
311+
if ($this->_recursingInsideFile) {
312+
// keep reference when it is a recursive reference
313+
return ['$ref' => $basePath . $value];
314+
}
315+
$this->_recursingInsideFile = true;
316+
$return = $this->adjustRelativeReferences($inlineDocument, $basePath, $baseDocument, $oContext);
317+
$this->_recursingInsideFile = false;
318+
return $return;
310319
}
311320
$referencedDocument[$key] = $context->resolveRelativeUri($value);
312321
$parts = explode('#', $referencedDocument[$key], 2);

Diff for: tests/spec/data/recursion3_index.yaml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Link Example
4+
version: 1.0.0
5+
#components:
6+
# parameters:
7+
# "Parameter.PetId":
8+
# "$ref": "./subdir/Parameter.PetId.json"
9+
paths:
10+
/contents/menus/{id}/trees:
11+
put:
12+
tags:
13+
- menus
14+
operationId: updateMenuTrees
15+
summary: '123'
16+
description: '456'
17+
# parameters:
18+
# - $ref: '#/components/parameters/PathId'
19+
requestBody:
20+
required: true
21+
content:
22+
application/json:
23+
schema:
24+
$ref: './recursion3_menu_tree.yaml#/UpdateMenuTreesRequest'
25+
responses:
26+
"200":
27+
description: Успешный ответ
28+
content:
29+
application/json:
30+
schema:
31+
$ref: './recursion3_menu_tree.yaml#/UpdateMenuTreesResponse'

Diff for: tests/spec/data/recursion3_menu_tree.yaml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
MenuTree:
2+
type: object
3+
properties:
4+
name:
5+
type: string
6+
description: 'Name'
7+
example: 'Home'
8+
url:
9+
type: string
10+
description: Link
11+
nullable: true
12+
example: '/about/'
13+
children:
14+
type: array
15+
items:
16+
$ref: '#/MenuTree'
17+
example: []
18+
UpdateMenuTreesRequest:
19+
type: object
20+
properties:
21+
items:
22+
type: array
23+
items:
24+
$ref: '#/MenuTree'
25+
UpdateMenuTreesResponse:
26+
type: object
27+
properties:
28+
data:
29+
type: array
30+
items:
31+
$ref: '#/MenuTree'
32+
required:
33+
- data

0 commit comments

Comments
 (0)