Skip to content

Commit 89f9bf1

Browse files
committed
Enable external network references
1 parent d5880d2 commit 89f9bf1

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

src/Model/SchemaDefinition/SchemaDefinitionDictionary.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public function getDefinition(string $key, SchemaProcessor $schemaProcessor, arr
117117

118118
if (!isset($this[$key]) && strstr($key, '#', true)) {
119119
[$jsonSchemaFile, $externalKey] = explode('#', $key);
120-
$jsonSchemaFile = $this->sourceDirectory . DIRECTORY_SEPARATOR . $jsonSchemaFile;
121120

122121
if (array_key_exists($jsonSchemaFile, $this->parsedExternalFileSchemas)) {
123122
return $this->parsedExternalFileSchemas[$jsonSchemaFile]->getSchemaDictionary()->getDefinition(
@@ -151,17 +150,21 @@ protected function parseExternalFile(
151150
SchemaProcessor $schemaProcessor,
152151
array &$path
153152
): ?SchemaDefinition {
154-
if (!is_file($jsonSchemaFile)) {
155-
return null;
153+
$jsonSchemaFilePath = filter_var($jsonSchemaFile, FILTER_VALIDATE_URL)
154+
? $jsonSchemaFile
155+
: $this->sourceDirectory . '/' . $jsonSchemaFile;
156+
157+
if (!filter_var($jsonSchemaFilePath, FILTER_VALIDATE_URL) && !is_file($jsonSchemaFilePath)) {
158+
throw new SchemaException("Reference to non existing JSON-Schema file $path");
156159
}
157160

158-
$jsonSchema = file_get_contents($jsonSchemaFile);
161+
$jsonSchema = file_get_contents($jsonSchemaFilePath);
159162

160163
if (!$jsonSchema || !($jsonSchema = json_decode($jsonSchema, true))) {
161-
throw new SchemaException("Invalid JSON-Schema file $jsonSchemaFile");
164+
throw new SchemaException("Invalid JSON-Schema file $jsonSchemaFilePath");
162165
}
163166

164-
$schema = new Schema(new self(dirname($jsonSchemaFile)));
167+
$schema = new Schema(new self(dirname($jsonSchemaFilePath)));
165168
$schema->getSchemaDictionary()->setUpDefinitionDictionary($jsonSchema, $schemaProcessor, $schema);
166169
$this->parsedExternalFileSchemas[$jsonSchemaFile] = $schema;
167170

src/PropertyProcessor/Property/ReferenceProcessor.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ public function process(string $propertyName, array $propertyData): PropertyInte
2525
$path = [];
2626
$reference = $propertyData['$ref'];
2727
$dictionary = $this->schema->getSchemaDictionary();
28-
$definition = $dictionary->getDefinition($reference, $this->schemaProcessor, $path);
2928

30-
if ($definition) {
31-
try {
29+
try {
30+
$definition = $dictionary->getDefinition($reference, $this->schemaProcessor, $path);
31+
32+
if ($definition) {
3233
return $definition->resolveReference($propertyName, $path, $this->propertyCollectionProcessor);
33-
} catch (Exception $exception) {
34-
throw new SchemaException("Unresolved Reference: $reference", 0, $exception);
3534
}
35+
} catch (Exception $exception) {
36+
throw new SchemaException("Unresolved Reference: $reference", 0, $exception);
3637
}
3738

3839
throw new SchemaException("Unresolved Reference: $reference");

tests/Objects/ReferencePropertyTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,17 @@ public function invalidCombinedReferenceObjectPropertyTypeDataProvider(): array
389389
}
390390

391391
/**
392+
* @dataProvider nestedReferenceProvider
393+
*
394+
* @param string $reference
395+
*
392396
* @throws FileSystemException
393397
* @throws RenderException
394398
* @throws SchemaException
395399
*/
396-
public function testNestedExternalReference(): void
400+
public function testNestedExternalReference(string $reference): void
397401
{
398-
$className = $this->generateObjectFromFile('NestedExternalReference.json');
402+
$className = $this->generateObjectFromFileTemplate('NestedExternalReference.json', [$reference]);
399403

400404
$object = new $className([
401405
'family' => [
@@ -422,6 +426,14 @@ public function testNestedExternalReference(): void
422426
$this->assertEmpty($object->getFamily()->getMember()[1]->getChildren());
423427
}
424428

429+
public function nestedReferenceProvider(): array
430+
{
431+
return [
432+
'Local reference' => ['external/library.json'],
433+
'Network reference' => ['https://raw.githubusercontent.com/wol-soft/php-json-schema-model-generator/master/tests/Schema/ReferencePropertyTest/external/library.json'],
434+
];
435+
}
436+
425437
/**
426438
* Combine two data providers
427439
*

tests/Schema/ReferencePropertyTest/NestedExternalReference.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"type": "object",
33
"properties": {
44
"family": {
5-
"$ref": "external/library.json#/definitions/family"
5+
"$ref": "%s#/definitions/family"
66
}
77
}
88
}

0 commit comments

Comments
 (0)