Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/index/index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ static auto index_main(const std::string_view &program,
// (6) Resolve all detected schemas in parallel
/////////////////////////////////////////////////////////////////////////////

sourcemeta::one::Resolver resolver;
sourcemeta::one::Resolver resolver{configuration.url};
resolver.reserve(detected_schemas.size());

// Phase 1: populate resolver from cache for unchanged source files.
Expand Down Expand Up @@ -460,15 +460,15 @@ static auto index_main(const std::string_view &program,
// Phase 2: resolve uncached schemas and commit to cache
sourcemeta::core::parallel_for_each(
uncached_schemas.begin(), uncached_schemas.end(),
[&configuration, &resolver, &mutex, &entries, &uncached_schemas,
[&resolver, &mutex, &entries, &uncached_schemas,
&app](const auto &detected_ref, const auto threads, const auto cursor) {
const auto &detected{detected_ref.get()};
print_progress(mutex, threads, "Resolving",
detected.path.filename().string(), cursor,
uncached_schemas.size());
const auto result{resolver.add(
configuration.url, detected.collection_relative_path,
detected.collection.get(), detected.path, detected.mtime)};
const auto result{resolver.add(detected.collection_relative_path,
detected.collection.get(), detected.path,
detected.mtime)};

{
const auto &resolved{result.second.get()};
Expand Down
7 changes: 4 additions & 3 deletions src/resolver/include/sourcemeta/one/resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ namespace sourcemeta::one {

class Resolver {
public:
Resolver() = default;
explicit Resolver(std::string_view server_url);

// Just to prevent mistakes
Resolver(const Resolver &) = delete;
auto operator=(const Resolver &) -> Resolver & = delete;
Expand Down Expand Up @@ -51,8 +52,7 @@ class Resolver {
std::pair<std::reference_wrapper<const sourcemeta::core::JSON::String>,
std::reference_wrapper<const Entry>>;

auto add(const sourcemeta::core::JSON::String &server_url,
const std::filesystem::path &collection_relative_path,
auto add(const std::filesystem::path &collection_relative_path,
const Configuration::Collection &collection,
const std::filesystem::path &path,
const std::filesystem::file_time_type mtime) -> Result;
Expand All @@ -73,6 +73,7 @@ class Resolver {
private:
Views views;
std::shared_mutex mutex;
std::string_view server_url;
};

} // namespace sourcemeta::one
Expand Down
34 changes: 20 additions & 14 deletions src/resolver/resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ static auto
normalise_ref(const sourcemeta::one::Configuration::Collection &collection,
const sourcemeta::core::URI &base, sourcemeta::core::JSON &schema,
const sourcemeta::core::JSON::String &keyword,
const sourcemeta::core::JSON::String &reference) -> void {
const sourcemeta::core::JSON::String &reference,
const std::string_view url) -> void {
// We never want to mess with internal references.
// We assume those are always well formed
if (reference.starts_with('#')) {
Expand All @@ -76,7 +77,9 @@ normalise_ref(const sourcemeta::one::Configuration::Collection &collection,
// If we have a match in the configuration resolver, then trust that.
const auto match{collection.resolve.find(reference)};
if (match != collection.resolve.cend()) {
schema.assign(keyword, sourcemeta::core::JSON{match->second});
sourcemeta::core::URI target{url};
target.append_path(match->second);
schema.assign(keyword, sourcemeta::core::JSON{target.path().value_or("")});
return;
}

Expand All @@ -100,6 +103,8 @@ normalise_ref(const sourcemeta::one::Configuration::Collection &collection,

namespace sourcemeta::one {

Resolver::Resolver(const std::string_view url) : server_url{url} {}

auto Resolver::operator()(
std::string_view raw_identifier,
const std::function<void(const std::filesystem::path &)> &callback) const
Expand Down Expand Up @@ -211,7 +216,8 @@ auto Resolver::operator()(
const auto maybe_ref{subschema.try_at("$ref", ref_hash)};
if (maybe_ref && maybe_ref->is_string()) {
normalise_ref(*result->second.collection, entry.second.base,
subschema, "$ref", maybe_ref->to_string());
subschema, "$ref", maybe_ref->to_string(),
this->server_url);
}

if (entry.second.base_dialect ==
Expand All @@ -221,7 +227,7 @@ auto Resolver::operator()(
if (maybe_dynamic_ref && maybe_dynamic_ref->is_string()) {
normalise_ref(*result->second.collection, entry.second.base,
subschema, "$dynamicRef",
maybe_dynamic_ref->to_string());
maybe_dynamic_ref->to_string(), this->server_url);
}
}
}
Expand All @@ -242,8 +248,7 @@ auto Resolver::operator()(
return schema;
}

auto Resolver::add(const sourcemeta::core::JSON::String &server_url,
const std::filesystem::path &collection_relative_path,
auto Resolver::add(const std::filesystem::path &collection_relative_path,
const Configuration::Collection &collection,
const std::filesystem::path &path,
const std::filesystem::file_time_type mtime) -> Result {
Expand Down Expand Up @@ -304,8 +309,9 @@ auto Resolver::add(const sourcemeta::core::JSON::String &server_url,
/////////////////////////////////////////////////////////////////////////////
// (3) Determine the new URI of the schema, from the one base URI
/////////////////////////////////////////////////////////////////////////////
const auto new_identifier{
rebase(collection, identifier, server_url, collection_relative_path)};
const auto new_identifier{rebase(collection, identifier,
std::string{this->server_url},
collection_relative_path)};
// Otherwise we have things like "../" that should not be there
assert(new_identifier.find("..") == std::string::npos);

Expand All @@ -320,11 +326,11 @@ auto Resolver::add(const sourcemeta::core::JSON::String &server_url,
}
const auto is_official_dialect{
sourcemeta::core::is_known_schema(raw_dialect)};
auto current_dialect{is_official_dialect
? std::string{raw_dialect}
: rebase(collection,
normalise_identifier(raw_dialect),
server_url, collection_relative_path)};
auto current_dialect{
is_official_dialect
? std::string{raw_dialect}
: rebase(collection, normalise_identifier(raw_dialect),
std::string{this->server_url}, collection_relative_path)};
// Otherwise we messed things up
assert(!current_dialect.ends_with("#.json"));

Expand All @@ -339,7 +345,7 @@ auto Resolver::add(const sourcemeta::core::JSON::String &server_url,
new_identifier,
Entry{.path = path,
.relative_path = sourcemeta::core::URI{new_identifier}
.relative_to(server_url)
.relative_to(std::string{this->server_url})
.recompose(),
.mtime = mtime,
.evaluate = evaluate,
Expand Down
2 changes: 1 addition & 1 deletion test/cli/index/common/url-with-path-resolve.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cat << EOF > "$TMP/one.json"
"baseUri": "https://other.example.com",
"path": "./schemas",
"resolve": {
"https://external.example.com/types.json": "/schemas/example/types.json"
"https://external.example.com/types.json": "/example/types.json"
}
}
}
Expand Down
Loading
Loading