Skip to content

Path reference fix #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/spec/PathItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function resolveReferences(ReferenceContext $context = null)
foreach ($this->$attribute as $k => $item) {
if ($item instanceof Reference) {
$referencedObject = $item->resolve();
$this->$attribute[$k] = $referencedObject;
$this->$attribute = [$k => $referencedObject] + $this->$attribute;
if (!$referencedObject instanceof Reference && $referencedObject !== null) {
$referencedObject->resolveReferences();
}
Expand Down
72 changes: 36 additions & 36 deletions tests/spec/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,43 +103,43 @@ public function specProvider()

// examples from https://github.com/Mermade/openapi3-examples
$mermadeExamples = [
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/externalPathItemRef.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/deprecated.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/swagger2openapi/openapi.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Fixed_file.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Fixed_file.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Fixed_multipart.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Improved_examples.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Improved_pathdescriptions.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Improved_securityschemes.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._Improved_serverseverywhere.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._New_callbacks.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example1_from_._New_links.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._Different_requestbody.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._Different_servers.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._Fixed_multipart.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._Improved_securityschemes.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._New_callbacks.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example2_from_._New_links.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example3_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example3_from_._Different_servers.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example4_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/gluecon/example5_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/externalPathItemRef.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/deprecated.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/swagger2openapi/openapi.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Fixed_file.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Fixed_file.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Fixed_multipart.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Improved_examples.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Improved_pathdescriptions.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Improved_securityschemes.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._Improved_serverseverywhere.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._New_callbacks.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example1_from_._New_links.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._Different_requestbody.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._Different_servers.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._Fixed_multipart.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._Improved_securityschemes.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._New_callbacks.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example2_from_._New_links.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example3_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example3_from_._Different_servers.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example4_from_._Different_parameters.md.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/gluecon/example5_from_._Different_parameters.md.yaml',
// TODO symfony/yaml can not read this file!?
// __DIR__ . '/../../vendor/mermade/openapi3-examples/pass/OAI/api-with-examples.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/OAI/petstore-expanded.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/OAI/petstore.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/pass/OAI/uber.yaml',

__DIR__ . '/../../vendor/mermade/openapi3-examples/malicious/rapid7-html.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/malicious/rapid7-java.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/malicious/rapid7-js.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/malicious/rapid7-php.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/malicious/rapid7-ruby.json',
// __DIR__ . '/../../vendor/mermade/openapi3-examples/malicious/yamlbomb.yaml',
// __DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/OAI/api-with-examples.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/OAI/petstore-expanded.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/OAI/petstore.yaml',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/pass/OAI/uber.yaml',

__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/malicious/rapid7-html.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/malicious/rapid7-java.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/malicious/rapid7-js.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/malicious/rapid7-php.json',
__DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/malicious/rapid7-ruby.json',
// __DIR__ . '/../../vendor/mermade/openapi3-examples/3.0/malicious/yamlbomb.yaml',
];

// examples from https://github.com/APIs-guru/openapi-directory/tree/openapi3.0.0/APIs
Expand Down
38 changes: 38 additions & 0 deletions tests/spec/ReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,44 @@ public function testTransitiveReferenceOverTwoFiles()
'200':
description: 'return a cat'

YAML;
// remove line endings to make string equal on windows
$expected = preg_replace('~\R~', "\n", $expected);
if (PHP_VERSION_ID < 70200) {
// PHP <7.2 returns numeric properties in yaml maps as integer, since 7.2 these are string
// probably related to https://www.php.net/manual/de/migration72.incompatible.php#migration72.incompatible.object-array-casts
$this->assertEquals(str_replace("'200':", "200:", $expected), $yaml, $yaml);
} else {
$this->assertEquals($expected, $yaml, $yaml);
}
}

public function testReferencedCommonParamsInReferencedPath()
{
$openapi = Reader::readFromYamlFile(__DIR__ . '/data/reference/ReferencedCommonParamsInReferencedPath.yml', OpenApi::class, \cebe\openapi\ReferenceContext::RESOLVE_MODE_INLINE);
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
$expected = <<<YAML
openapi: 3.0.0
info:
title: 'Nested reference with common path params'
version: 1.0.0
paths:
/example:
get:
responses:
'200':
description: 'OK if common params can be references'
parameters:
-
name: test
in: header
description: 'Test parameter to be referenced'
required: true
schema:
enum:
- test
type: string

YAML;
// remove line endings to make string equal on windows
$expected = preg_replace('~\R~', "\n", $expected);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
openapi: 3.0.0
info:
title: Nested reference with common path params
version: 1.0.0
paths:
/example:
$ref: 'paths/ReferencesCommonParams.yml'
7 changes: 7 additions & 0 deletions tests/spec/data/reference/parameters/TestParameter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: test
in: header
description: Test parameter to be referenced
required: true
schema:
type: string
enum: [ 'test' ]
7 changes: 7 additions & 0 deletions tests/spec/data/reference/paths/ReferencesCommonParams.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
- $ref: '../parameters/TestParameter.yml'

get:
responses:
200:
description: OK if common params can be references