Skip to content

Commit 9336e31

Browse files
authored
Add schema resolver (#37)
1 parent 7add857 commit 9336e31

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.11.0] - 2022-09-18
8+
9+
### Added
10+
- Configurable schema url resolver
11+
712
## [1.10.0] - 2022-09-16
813

914
### Added
@@ -213,6 +218,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
213218
### Fixed
214219
- Local file resolver in references.
215220

221+
[1.11.0]: https://github.com/swaggest/json-cli/compare/v1.10.0...v1.11.0
216222
[1.10.0]: https://github.com/swaggest/json-cli/compare/v1.9.1...v1.10.0
217223
[1.9.1]: https://github.com/swaggest/json-cli/compare/v1.9.0...v1.9.1
218224
[1.9.0]: https://github.com/swaggest/json-cli/compare/v1.8.8...v1.9.0

bin/json-cli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ foreach (array(__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoloa
1313
}
1414

1515
try {
16+
if(function_exists('xdebug_disable')) { xdebug_disable(); }
17+
ini_set("error_reporting", "Off");
1618
Runner::create(new App())->run();
1719
} catch (ExitCode $exception) {
1820
die($exception->getCode());

src/App.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class App extends Command\Application
99
{
10-
public static $ver = 'v1.10.0';
10+
public static $ver = 'v1.11.0';
1111

1212
public $diff;
1313
public $apply;

src/Base.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Swaggest\JsonSchema\RemoteRef\BasicFetcher;
1010
use Swaggest\JsonSchema\RemoteRef\Preloaded;
1111
use Swaggest\JsonSchema\Schema;
12+
use Swaggest\PhpCodeBuilder\Markdown\TypeBuilder;
1213
use Symfony\Component\Yaml\Yaml;
1314
use Yaoi\Command;
1415
use Yaoi\Io\Response;
@@ -21,6 +22,7 @@ abstract class Base extends Command
2122
public $toYaml;
2223
public $toSerialized;
2324
public $output;
25+
public $schemaResolver;
2426

2527
/**
2628
* @param Command\Definition $definition
@@ -32,6 +34,13 @@ static function setUpDefinition(Command\Definition $definition, $options)
3234
->setDescription('Pretty-print result JSON');
3335
$options->output = Command\Option::create()->setType()
3436
->setDescription('Path to output result, default STDOUT');
37+
$options->schemaResolver = Command\Option::create()->setType()
38+
->setDescription('Path to schema resolver JSON file. Schema:');
39+
$tb = new TypeBuilder();
40+
$tb->getTypeString(SchemaResolver::schema()->exportSchema());
41+
$options->schemaResolver->description .= "\n" . trim(substr($tb->file, 77)); // Stripping header.
42+
43+
3544
$options->toYaml = Command\Option::create()->setDescription('Output in YAML format');
3645
$options->toSerialized = Command\Option::create()->setDescription('Output in PHP serialized format');
3746
}
@@ -119,6 +128,12 @@ protected static function setupGenOptions(Command\Definition $definition, $optio
119128
$options->defPtr = Command\Option::create()->setType()->setIsVariadic()
120129
->setDescription('Definitions pointers to strip from symbol names, default #/definitions');
121130

131+
$options->schemaResolver = Command\Option::create()->setType()
132+
->setDescription('Path to schema resolver JSON file. Schema:');
133+
$tb = new TypeBuilder();
134+
$tb->getTypeString(SchemaResolver::schema()->exportSchema());
135+
$options->schemaResolver->description .= "\n" . trim(substr($tb->file, 77)); // Stripping header.
136+
122137
static::setupLoadFileOptions($options);
123138
}
124139

@@ -139,6 +154,28 @@ protected function loadSchema(&$skipRoot, &$baseName)
139154

140155
$resolver = new ResolverMux();
141156
$preloaded = new Preloaded();
157+
158+
if ($this->schemaResolver !== null) {
159+
$data = file_get_contents($this->schemaResolver);
160+
if (empty($data)) {
161+
throw new ExitCode("empty or missing schema resolver file", 1);
162+
}
163+
$json = json_decode($data);
164+
if (empty($json)) {
165+
throw new ExitCode("invalid json in schema resolver file", 1);
166+
}
167+
168+
$schemaResolver = SchemaResolver::import($json);
169+
170+
foreach ($schemaResolver->schemaData as $url => $data) {
171+
$preloaded->setSchemaData($url, $data);
172+
}
173+
174+
foreach ($schemaResolver->schemaFiles as $url => $file) {
175+
$preloaded->setSchemaFile($url, $file);
176+
}
177+
}
178+
142179
$resolver->resolvers[] = $preloaded;
143180

144181
$dataValue = $this->loadFile();

src/SchemaResolver.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Swaggest\JsonCli;
4+
5+
use Swaggest\JsonSchema\Constraint\Properties;
6+
use Swaggest\JsonSchema\Schema;
7+
use Swaggest\JsonSchema\Structure\ClassStructure;
8+
9+
class SchemaResolver extends ClassStructure
10+
{
11+
public $schemaData = [];
12+
13+
public $schemaFiles = [];
14+
15+
/**
16+
* @param Properties|static $properties
17+
* @param Schema $ownerSchema
18+
*/
19+
public static function setUpProperties($properties, Schema $ownerSchema)
20+
{
21+
$properties->schemaData = Schema::object()->setAdditionalProperties(Schema::object())
22+
->setDescription('Map of schema url to schema data.');
23+
$properties->schemaFiles = Schema::object()->setAdditionalProperties(Schema::string())
24+
->setDescription('Map of schema url to file path containing schema data.');
25+
}
26+
}

0 commit comments

Comments
 (0)