Skip to content

Commit

Permalink
Merge pull request swaggest#47 from swaggest/handling-local-references
Browse files Browse the repository at this point in the history
adding file:// references support
  • Loading branch information
vearutop authored Jul 6, 2018
2 parents aa5ce40 + f3d7928 commit 36fc856
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/RemoteRef/BasicFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class BasicFetcher implements RemoteRefProvider
{
public function getSchemaData($url)
{
if (strtolower(substr($url, 0, 7)) === 'file://') {
$url = substr($url, 7);
}
return json_decode(file_get_contents($url));
}
}
61 changes: 61 additions & 0 deletions tests/src/PHPUnit/Ref/FileResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,65 @@ public function testFileResolver()
}
}


function testFileReference()
{
$pathToExternalSchema = __DIR__ . '/../../../resources/remotes/subSchemas.json';
$schemaData = json_decode(<<<JSON
{"\$ref": "file://$pathToExternalSchema#/integer"}
JSON
);

$schema = Schema::import($schemaData);
$schema->in(123);

$this->setExpectedException(get_class(new InvalidValue()));
$schema->in('abc');
}

function testFileReferenceCapitalized()
{
$pathToExternalSchema = __DIR__ . '/../../../resources/remotes/subSchemas.json';
$schemaData = json_decode(<<<JSON
{"\$ref": "FILE://$pathToExternalSchema#/integer"}
JSON
);

$schema = Schema::import($schemaData);
$schema->in(123);

$this->setExpectedException(get_class(new InvalidValue()));
$schema->in('abc');
}

function testFileReferenceRealpath()
{
$pathToExternalSchema = realpath(__DIR__ . '/../../../resources/remotes/subSchemas.json');
$schemaData = json_decode(<<<JSON
{"\$ref": "file://$pathToExternalSchema#/integer"}
JSON
);

$schema = Schema::import($schemaData);
$schema->in(123);

$this->setExpectedException(get_class(new InvalidValue()));
$schema->in('abc');
}

function testFileReferenceNoProtocolScheme()
{
$pathToExternalSchema = __DIR__ . '/../../../resources/remotes/subSchemas.json';
$schemaData = json_decode(<<<JSON
{"\$ref": "$pathToExternalSchema#/integer"}
JSON
);

$schema = Schema::import($schemaData);
$schema->in(123);

$this->setExpectedException(get_class(new InvalidValue()));
$schema->in('abc');
}

}

0 comments on commit 36fc856

Please sign in to comment.