Skip to content

Commit

Permalink
Catch recursion when resolving inside included file
Browse files Browse the repository at this point in the history
fixes #144
  • Loading branch information
cebe committed Feb 9, 2022
1 parent a02f958 commit 43044d5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ install: composer.lock yarn.lock
$(DOCKER_PHP) composer install --prefer-dist --no-interaction --no-progress --ansi
$(DOCKER_NODE) yarn install

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

unit:
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) vendor/bin/phpunit --verbose --colors=always $(TESTCASE)
Expand Down
11 changes: 10 additions & 1 deletion src/spec/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ private function resolveTransitiveReference(Reference $referencedObject, Referen
return $transitiveRefResult;
}

private $_recursingInsideFile = false;

/**
* Adjust relative references inside of the file to match the context of the base file
*/
Expand All @@ -306,7 +308,14 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
// direcly inline references in the same document,
// these are not going to be valid in the new context anymore
$inlineDocument = (new JsonPointer(substr($value, 1)))->evaluate($baseDocument);
return $this->adjustRelativeReferences($inlineDocument, $basePath, $baseDocument, $oContext);
if ($this->_recursingInsideFile) {
// keep reference when it is a recursive reference
return ['$ref' => $basePath . $value];
}
$this->_recursingInsideFile = true;
$return = $this->adjustRelativeReferences($inlineDocument, $basePath, $baseDocument, $oContext);
$this->_recursingInsideFile = false;
return $return;
}
$referencedDocument[$key] = $context->resolveRelativeUri($value);
$parts = explode('#', $referencedDocument[$key], 2);
Expand Down
31 changes: 31 additions & 0 deletions tests/spec/data/recursion3_index.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
openapi: 3.0.3
info:
title: Link Example
version: 1.0.0
#components:
# parameters:
# "Parameter.PetId":
# "$ref": "./subdir/Parameter.PetId.json"
paths:
/contents/menus/{id}/trees:
put:
tags:
- menus
operationId: updateMenuTrees
summary: '123'
description: '456'
# parameters:
# - $ref: '#/components/parameters/PathId'
requestBody:
required: true
content:
application/json:
schema:
$ref: './recursion3_menu_tree.yaml#/UpdateMenuTreesRequest'
responses:
"200":
description: Успешный ответ
content:
application/json:
schema:
$ref: './recursion3_menu_tree.yaml#/UpdateMenuTreesResponse'
33 changes: 33 additions & 0 deletions tests/spec/data/recursion3_menu_tree.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
MenuTree:
type: object
properties:
name:
type: string
description: 'Name'
example: 'Home'
url:
type: string
description: Link
nullable: true
example: '/about/'
children:
type: array
items:
$ref: '#/MenuTree'
example: []
UpdateMenuTreesRequest:
type: object
properties:
items:
type: array
items:
$ref: '#/MenuTree'
UpdateMenuTreesResponse:
type: object
properties:
data:
type: array
items:
$ref: '#/MenuTree'
required:
- data

0 comments on commit 43044d5

Please sign in to comment.