Skip to content

Commit

Permalink
Add schema resolver (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Sep 18, 2022
1 parent 7add857 commit 9336e31
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.11.0] - 2022-09-18

### Added
- Configurable schema url resolver

## [1.10.0] - 2022-09-16

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

[1.11.0]: https://github.com/swaggest/json-cli/compare/v1.10.0...v1.11.0
[1.10.0]: https://github.com/swaggest/json-cli/compare/v1.9.1...v1.10.0
[1.9.1]: https://github.com/swaggest/json-cli/compare/v1.9.0...v1.9.1
[1.9.0]: https://github.com/swaggest/json-cli/compare/v1.8.8...v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions bin/json-cli
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ foreach (array(__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoloa
}

try {
if(function_exists('xdebug_disable')) { xdebug_disable(); }
ini_set("error_reporting", "Off");
Runner::create(new App())->run();
} catch (ExitCode $exception) {
die($exception->getCode());
Expand Down
2 changes: 1 addition & 1 deletion src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class App extends Command\Application
{
public static $ver = 'v1.10.0';
public static $ver = 'v1.11.0';

public $diff;
public $apply;
Expand Down
37 changes: 37 additions & 0 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Swaggest\JsonSchema\RemoteRef\BasicFetcher;
use Swaggest\JsonSchema\RemoteRef\Preloaded;
use Swaggest\JsonSchema\Schema;
use Swaggest\PhpCodeBuilder\Markdown\TypeBuilder;
use Symfony\Component\Yaml\Yaml;
use Yaoi\Command;
use Yaoi\Io\Response;
Expand All @@ -21,6 +22,7 @@ abstract class Base extends Command
public $toYaml;
public $toSerialized;
public $output;
public $schemaResolver;

/**
* @param Command\Definition $definition
Expand All @@ -32,6 +34,13 @@ static function setUpDefinition(Command\Definition $definition, $options)
->setDescription('Pretty-print result JSON');
$options->output = Command\Option::create()->setType()
->setDescription('Path to output result, default STDOUT');
$options->schemaResolver = Command\Option::create()->setType()
->setDescription('Path to schema resolver JSON file. Schema:');
$tb = new TypeBuilder();
$tb->getTypeString(SchemaResolver::schema()->exportSchema());
$options->schemaResolver->description .= "\n" . trim(substr($tb->file, 77)); // Stripping header.


$options->toYaml = Command\Option::create()->setDescription('Output in YAML format');
$options->toSerialized = Command\Option::create()->setDescription('Output in PHP serialized format');
}
Expand Down Expand Up @@ -119,6 +128,12 @@ protected static function setupGenOptions(Command\Definition $definition, $optio
$options->defPtr = Command\Option::create()->setType()->setIsVariadic()
->setDescription('Definitions pointers to strip from symbol names, default #/definitions');

$options->schemaResolver = Command\Option::create()->setType()
->setDescription('Path to schema resolver JSON file. Schema:');
$tb = new TypeBuilder();
$tb->getTypeString(SchemaResolver::schema()->exportSchema());
$options->schemaResolver->description .= "\n" . trim(substr($tb->file, 77)); // Stripping header.

static::setupLoadFileOptions($options);
}

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

$resolver = new ResolverMux();
$preloaded = new Preloaded();

if ($this->schemaResolver !== null) {
$data = file_get_contents($this->schemaResolver);
if (empty($data)) {
throw new ExitCode("empty or missing schema resolver file", 1);
}
$json = json_decode($data);
if (empty($json)) {
throw new ExitCode("invalid json in schema resolver file", 1);
}

$schemaResolver = SchemaResolver::import($json);

foreach ($schemaResolver->schemaData as $url => $data) {
$preloaded->setSchemaData($url, $data);
}

foreach ($schemaResolver->schemaFiles as $url => $file) {
$preloaded->setSchemaFile($url, $file);
}
}

$resolver->resolvers[] = $preloaded;

$dataValue = $this->loadFile();
Expand Down
26 changes: 26 additions & 0 deletions src/SchemaResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Swaggest\JsonCli;

use Swaggest\JsonSchema\Constraint\Properties;
use Swaggest\JsonSchema\Schema;
use Swaggest\JsonSchema\Structure\ClassStructure;

class SchemaResolver extends ClassStructure
{
public $schemaData = [];

public $schemaFiles = [];

/**
* @param Properties|static $properties
* @param Schema $ownerSchema
*/
public static function setUpProperties($properties, Schema $ownerSchema)
{
$properties->schemaData = Schema::object()->setAdditionalProperties(Schema::object())
->setDescription('Map of schema url to schema data.');
$properties->schemaFiles = Schema::object()->setAdditionalProperties(Schema::string())
->setDescription('Map of schema url to file path containing schema data.');
}
}

0 comments on commit 9336e31

Please sign in to comment.