Skip to content

Commit

Permalink
Support ingesting YAML schemas
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Jan 15, 2025
1 parent cb771ae commit 3211437
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 26 deletions.
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
vendorpull https://github.com/sourcemeta/vendorpull dea311b5bfb53b6926a4140267959ae334d3ecf4
noa https://github.com/sourcemeta/noa 924f5cc8549af7f12227869dcbab4259029ac650
jsontoolkit https://github.com/sourcemeta/jsontoolkit 5ad87d03d7c1dde6e66a047e8e76d8291114627a
jsontoolkit https://github.com/sourcemeta/jsontoolkit a65ff3f97fc43f2e75409489d6f069579a4bd202
blaze https://github.com/sourcemeta/blaze a906d6601bc5d7afd4ddb6cbf9f64326aeee1b0c
hydra https://github.com/sourcemeta/hydra c31b5b612beb3e6a870deb2581ae437fc5183f47
bootstrap https://github.com/twbs/bootstrap v5.3.3
Expand Down
1 change: 1 addition & 0 deletions src/index/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set_target_properties(schema_registry_index PROPERTIES OUTPUT_NAME sourcemeta-re
target_link_libraries(schema_registry_index PRIVATE sourcemeta::jsontoolkit::uri)
target_link_libraries(schema_registry_index PRIVATE sourcemeta::jsontoolkit::json)
target_link_libraries(schema_registry_index PRIVATE sourcemeta::jsontoolkit::jsonschema)
target_link_libraries(schema_registry_index PRIVATE sourcemeta::jsontoolkit::yaml)
target_link_libraries(schema_registry_index PRIVATE sourcemeta::blaze::compiler)
target_link_libraries(schema_registry_index PRIVATE sourcemeta::blaze::evaluator)

Expand Down
23 changes: 19 additions & 4 deletions src/index/index.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <sourcemeta/jsontoolkit/json.h>
#include <sourcemeta/jsontoolkit/jsonschema.h>
#include <sourcemeta/jsontoolkit/uri.h>
#include <sourcemeta/jsontoolkit/yaml.h>

#include <sourcemeta/blaze/compiler.h>
#include <sourcemeta/blaze/evaluator.h>
Expand Down Expand Up @@ -38,6 +39,20 @@ static auto write_lower_except_trailing(T &stream, const std::string &input,
}
}

static auto is_yaml(const std::filesystem::path &path) -> bool {
return path.extension() == ".yaml" || path.extension() == ".yml";
}

static auto is_schema_file(const std::filesystem::path &path) -> bool {
return is_yaml(path) || path.extension() == ".json";
}

static auto schema_reader(const std::filesystem::path &path)
-> sourcemeta::jsontoolkit::JSON {
return is_yaml(path) ? sourcemeta::jsontoolkit::from_yaml(path)
: sourcemeta::jsontoolkit::from_file(path);
}

static auto url_join(const std::string &first, const std::string &second,
const std::string &third, const std::string &extension)
-> std::string {
Expand Down Expand Up @@ -112,7 +127,7 @@ static auto index(sourcemeta::jsontoolkit::FlatFileSchemaResolver &resolver,

for (const auto &entry :
std::filesystem::recursive_directory_iterator{collection_path}) {
if (!entry.is_regular_file() || entry.path().extension() != ".json" ||
if (!entry.is_regular_file() || !is_schema_file(entry.path()) ||
entry.path().stem().string().starts_with(".")) {
continue;
}
Expand All @@ -133,9 +148,9 @@ static auto index(sourcemeta::jsontoolkit::FlatFileSchemaResolver &resolver,

default_identifier << relative_path;

const auto &current_identifier{resolver.add(
entry.path(), sourcemeta::jsontoolkit::from_file(entry.path()),
default_dialect, default_identifier.str())};
const auto &current_identifier{resolver.add(entry.path(), default_dialect,
default_identifier.str(),
schema_reader)};
auto identifier_uri{
sourcemeta::jsontoolkit::URI{current_identifier}.canonicalize()};
std::cerr << identifier_uri.recompose();
Expand Down
31 changes: 31 additions & 0 deletions test/e2e/common/yaml.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
GET {{base}}/example/schemas/yaml.yaml.json
HTTP 200
Content-Type: application/schema+json
Link: <http://json-schema.org/draft-07/schema\#>; rel="describedby"
[Asserts]
header "ETag" exists
header "X-Request-id" exists

GET {{base}}/example/schemas/yaml.yaml.json
HTTP 200
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "{{base}}/example/schemas/yaml.yaml.json",
"type": "string"
}

GET {{base}}/example/schemas/yml.yml.json
HTTP 200
Content-Type: application/schema+json
Link: <http://json-schema.org/draft-07/schema\#>; rel="describedby"
[Asserts]
header "ETag" exists
header "X-Request-id" exists

GET {{base}}/example/schemas/yml.yml.json
HTTP 200
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "{{base}}/example/schemas/yml.yml.json",
"type": "string"
}
6 changes: 6 additions & 0 deletions test/sandbox/manifest-ce.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
./bundles/example/schemas/no-schema-nor-id.json
./bundles/example/schemas/no-schema.json
./bundles/example/schemas/string.json
./bundles/example/schemas/yaml.yaml.json
./bundles/example/schemas/yml.yml.json
./bundles/example/v2.0
./bundles/example/v2.0/schema.json
./bundles/hyper
Expand All @@ -39,6 +41,8 @@
./schemas/example/schemas/no-schema-nor-id.json
./schemas/example/schemas/no-schema.json
./schemas/example/schemas/string.json
./schemas/example/schemas/yaml.yaml.json
./schemas/example/schemas/yml.yml.json
./schemas/example/v2.0
./schemas/example/v2.0/schema.json
./schemas/hyper
Expand All @@ -61,6 +65,8 @@
./unidentified/example/schemas/no-schema-nor-id.json
./unidentified/example/schemas/no-schema.json
./unidentified/example/schemas/string.json
./unidentified/example/schemas/yaml.yaml.json
./unidentified/example/schemas/yml.yml.json
./unidentified/example/v2.0
./unidentified/example/v2.0/schema.json
./unidentified/hyper
Expand Down
6 changes: 6 additions & 0 deletions test/sandbox/manifest-ee.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
./bundles/example/schemas/no-schema-nor-id.json
./bundles/example/schemas/no-schema.json
./bundles/example/schemas/string.json
./bundles/example/schemas/yaml.yaml.json
./bundles/example/schemas/yml.yml.json
./bundles/example/v2.0
./bundles/example/v2.0/schema.json
./bundles/hyper
Expand Down Expand Up @@ -65,6 +67,8 @@
./schemas/example/schemas/no-schema-nor-id.json
./schemas/example/schemas/no-schema.json
./schemas/example/schemas/string.json
./schemas/example/schemas/yaml.yaml.json
./schemas/example/schemas/yml.yml.json
./schemas/example/v2.0
./schemas/example/v2.0/schema.json
./schemas/hyper
Expand All @@ -87,6 +91,8 @@
./unidentified/example/schemas/no-schema-nor-id.json
./unidentified/example/schemas/no-schema.json
./unidentified/example/schemas/string.json
./unidentified/example/schemas/yaml.yaml.json
./unidentified/example/schemas/yml.yml.json
./unidentified/example/v2.0
./unidentified/example/v2.0/schema.json
./unidentified/hyper
Expand Down
3 changes: 3 additions & 0 deletions test/sandbox/schemas/example/folder/yaml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$schema: 'http://json-schema.org/draft-07/schema#'
$id: https://example.com/schemas/yaml.yaml
type: string
3 changes: 3 additions & 0 deletions test/sandbox/schemas/example/folder/yml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$schema: 'http://json-schema.org/draft-07/schema#'
$id: https://example.com/schemas/yml.yml
type: string

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 7 additions & 13 deletions vendor/jsontoolkit/src/jsonschema/resolver.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3211437

Please sign in to comment.