Skip to content

Commit 36fc856

Browse files
authored
Merge pull request #47 from swaggest/handling-local-references
adding file:// references support
2 parents aa5ce40 + f3d7928 commit 36fc856

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/RemoteRef/BasicFetcher.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class BasicFetcher implements RemoteRefProvider
88
{
99
public function getSchemaData($url)
1010
{
11+
if (strtolower(substr($url, 0, 7)) === 'file://') {
12+
$url = substr($url, 7);
13+
}
1114
return json_decode(file_get_contents($url));
1215
}
1316
}

tests/src/PHPUnit/Ref/FileResolverTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,65 @@ public function testFileResolver()
6767
}
6868
}
6969

70+
71+
function testFileReference()
72+
{
73+
$pathToExternalSchema = __DIR__ . '/../../../resources/remotes/subSchemas.json';
74+
$schemaData = json_decode(<<<JSON
75+
{"\$ref": "file://$pathToExternalSchema#/integer"}
76+
JSON
77+
);
78+
79+
$schema = Schema::import($schemaData);
80+
$schema->in(123);
81+
82+
$this->setExpectedException(get_class(new InvalidValue()));
83+
$schema->in('abc');
84+
}
85+
86+
function testFileReferenceCapitalized()
87+
{
88+
$pathToExternalSchema = __DIR__ . '/../../../resources/remotes/subSchemas.json';
89+
$schemaData = json_decode(<<<JSON
90+
{"\$ref": "FILE://$pathToExternalSchema#/integer"}
91+
JSON
92+
);
93+
94+
$schema = Schema::import($schemaData);
95+
$schema->in(123);
96+
97+
$this->setExpectedException(get_class(new InvalidValue()));
98+
$schema->in('abc');
99+
}
100+
101+
function testFileReferenceRealpath()
102+
{
103+
$pathToExternalSchema = realpath(__DIR__ . '/../../../resources/remotes/subSchemas.json');
104+
$schemaData = json_decode(<<<JSON
105+
{"\$ref": "file://$pathToExternalSchema#/integer"}
106+
JSON
107+
);
108+
109+
$schema = Schema::import($schemaData);
110+
$schema->in(123);
111+
112+
$this->setExpectedException(get_class(new InvalidValue()));
113+
$schema->in('abc');
114+
}
115+
116+
function testFileReferenceNoProtocolScheme()
117+
{
118+
$pathToExternalSchema = __DIR__ . '/../../../resources/remotes/subSchemas.json';
119+
$schemaData = json_decode(<<<JSON
120+
{"\$ref": "$pathToExternalSchema#/integer"}
121+
JSON
122+
);
123+
124+
$schema = Schema::import($schemaData);
125+
$schema->in(123);
126+
127+
$this->setExpectedException(get_class(new InvalidValue()));
128+
$schema->in('abc');
129+
}
130+
70131
}

0 commit comments

Comments
 (0)