Skip to content

Commit f7ecf02

Browse files
committed
Resolves issues with remote schema refs
1 parent 2bd9fbe commit f7ecf02

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

js/patternkit.jsoneditor.es6.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
this.refs[url] = 'loading';
8888
waiting++;
8989
let r = new XMLHttpRequest();
90-
let uri = settings.path.baseUrl + url + '/schema';
90+
let uri = settings.path.baseUrl + url;
9191
r.open("GET", uri, true);
9292
r.onreadystatechange = () => {
9393
if (r.readyState !== 4) {

js/patternkit.jsoneditor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
121121
_this.refs[url] = 'loading';
122122
waiting++;
123123
var r = new XMLHttpRequest();
124-
var uri = settings.path.baseUrl + url + '/schema';
124+
var uri = settings.path.baseUrl + url;
125125
r.open("GET", uri, true);
126126

127127
r.onreadystatechange = function () {

src/PatternLibraryJSONParserTrait.php

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\patternkit;
44

5+
use Drupal\Core\Url;
56
use Drupal\patternkit\Entity\PatternInterface;
67

78
/**
@@ -39,7 +40,7 @@ public function fetchJsonSchemaAsset($file_uri) {
3940
* @return object
4041
* The updated schema properties object.
4142
*
42-
* @todo JSON Schema technically allows the following valid refs:
43+
* @example JSON Schema technically allows the following valid refs:
4344
* - URI "example.json" "../../example.json" "./example.json"
4445
* - URL "https://json-schema.org/person.schema.json"
4546
* - JSON Pointer "#/definitions/example" "example.json#/example"
@@ -76,15 +77,23 @@ public static function schemaDereference($properties, PatternInterface $pattern)
7677
}
7778
$path = $value;
7879
if (strpos($value,'@') === 0) {
79-
$library_name = strstr($value, '/', TRUE);
80-
$pattern_library = $library->getLibraryDefinitions()[trim($library_name, '@')];
81-
$path = substr($value, strlen());
80+
$library_namespace = strstr($value, '/', TRUE);
81+
$path = substr($value, strlen("$library_namespace/"));
82+
}
83+
$ref = '';
84+
if (strstr($path, '#/')) {
85+
$ref = explode('#/', $path);
86+
$path = reset($ref);
87+
$ref = !empty($ref[1]) ? '#/' . $ref[1] : '';
8288
}
89+
$library_name = $pattern->getLibrary();
8390
if (strstr($path, './')) {
91+
$pattern_library = $library->getLibraryDefinitions()[trim($library_name, '@')];
92+
$library_path = $pattern_library->getPatternInfo()['data'] ?? $pattern_library->getExtension()->getPath();
8493
/** @var \Drupal\Core\File\FileSystem $filesystem */
85-
$path = realpath($pattern->getPath() . $path);
94+
$path = substr(str_replace('\\', '/', realpath($library_path . '/' . $path)), strlen(\Drupal::root() . '/' . $library_path . '/'));
8695
}
87-
$properties[$property] = 'api/patternkit/' . trim($value, '.json');
96+
$properties[$property] = Url::fromUserInput('/api/patternkit/' . trim($library_namespace ?? $library_name, '@') . '/' . trim($path, '.json') . '/json')->toString() . $ref;
8897
}
8998
return $properties;
9099
}

src/Plugin/PatternLibrary/PatternLibraryFile.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Drupal\patternkit\Plugin\PatternLibrary;
44

55
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6+
use Drupal\patternkit\Annotation\PatternLibrary;
67
use Drupal\patternkit\Entity\PatternInterface;
7-
use Drupal\patternkit\Pattern;
88
use Drupal\patternkit\PatternEditorConfig;
99
use Drupal\patternkit\PatternLibraryPluginDefault;
1010
use Drupal\patternkit\PatternLibraryPluginInterface;
@@ -36,7 +36,7 @@ class PatternLibraryFile extends PatternLibraryPluginDefault implements Containe
3636
/**
3737
* File pattern library parser service.
3838
*
39-
* @var \Drupal\patternkit\PatternLibraryParser\FilePatternLibraryParser
39+
* @var \Drupal\patternkit\Asset\PatternLibraryParser\FilePatternLibraryParser
4040
*/
4141
protected $fileParser;
4242

@@ -47,7 +47,7 @@ class PatternLibraryFile extends PatternLibraryPluginDefault implements Containe
4747
*/
4848
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): PatternLibraryPluginInterface {
4949
$root = $container->get('app.root');
50-
/** @var \Drupal\patternkit\PatternLibraryParserInterface $file_parser */
50+
/** @var \Drupal\patternkit\Asset\PatternLibraryParserInterface $file_parser */
5151
$file_parser = $container->get('patternkit.asset.library.parser.file');
5252
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
5353
$config_factory = $container->get('config.factory');

0 commit comments

Comments
 (0)