-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbundle.cc
More file actions
505 lines (437 loc) · 19.6 KB
/
Copy pathbundle.cc
File metadata and controls
505 lines (437 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#include <sourcemeta/blaze/bundle.h>
#include <sourcemeta/blaze/foundation.h>
#include <sourcemeta/blaze/frame.h>
#include "helpers.h"
#include <cassert> // assert
#include <functional> // std::reference_wrapper
#include <string> // std::string
#include <tuple> // std::tuple
#include <unordered_map> // std::unordered_map
#include <unordered_set> // std::unordered_set
#include <utility> // std::move
#include <vector> // std::vector
namespace {
auto is_skippable_metaschema_reference(
const sourcemeta::blaze::BundleMode mode,
const sourcemeta::core::WeakPointer &pointer,
const std::string &destination) -> bool {
assert(!pointer.empty());
assert(pointer.back().is_property());
if (pointer.back().to_property() != "$schema") {
return false;
}
return mode == sourcemeta::blaze::BundleMode::References ||
sourcemeta::blaze::is_official_schema(destination);
}
auto dependencies_internal(
const sourcemeta::core::JSON &schema,
const sourcemeta::blaze::SchemaWalker &walker,
const sourcemeta::blaze::SchemaResolver &resolver,
const sourcemeta::blaze::DependencyCallback &callback,
std::string_view default_dialect, std::string_view default_id,
const sourcemeta::blaze::SchemaFrame::Paths &paths,
std::unordered_set<std::string> &visited) -> void {
sourcemeta::blaze::SchemaFrame frame{
sourcemeta::blaze::SchemaFrame::Mode::References};
frame.analyse(schema, walker, resolver, default_dialect, default_id, paths);
const auto origin{sourcemeta::blaze::identify(schema, resolver,
default_dialect, default_id)};
std::vector<
std::tuple<sourcemeta::core::JSON, sourcemeta::core::JSON::String>>
found;
frame.for_each_unresolved_reference([&](const auto &pointer,
const auto &reference) {
// We don't want to report official schemas, as we can expect
// virtually all implementations to understand them out of the box
if (is_skippable_metaschema_reference(
sourcemeta::blaze::BundleMode::NonOfficialMetaschemas, pointer,
reference.destination)) {
return;
}
if (reference.base.empty()) {
throw sourcemeta::blaze::SchemaReferenceError(
reference.destination, sourcemeta::core::to_pointer(pointer),
"Could not resolve schema reference");
}
// To not infinitely loop on circular references
if (visited.contains(std::string{reference.base})) {
return;
}
// If we can't find the destination but there is a base and we can
// find the base, then we are facing an unresolved fragment
if (frame.traverse(reference.base).has_value()) {
throw sourcemeta::blaze::SchemaReferenceError(
reference.destination, sourcemeta::core::to_pointer(pointer),
"Could not resolve schema reference");
}
assert(!reference.base.empty());
const auto &identifier{reference.base};
auto remote{resolver(identifier)};
if (!remote.has_value()) {
throw sourcemeta::blaze::SchemaResolutionError(
identifier, "Could not resolve the reference to an external schema");
}
if (!sourcemeta::blaze::is_schema(remote.value())) {
throw sourcemeta::blaze::SchemaReferenceError(
identifier, sourcemeta::core::to_pointer(pointer),
"The JSON document is not a valid JSON Schema");
}
const auto remote_base_dialect{sourcemeta::blaze::base_dialect(
remote.value(), resolver, default_dialect)};
if (!remote_base_dialect.has_value()) {
throw sourcemeta::blaze::SchemaReferenceError(
identifier, sourcemeta::core::to_pointer(pointer),
"The JSON document is not a valid JSON Schema");
}
callback(origin, pointer, identifier, remote.value());
found.emplace_back(std::move(remote).value(),
sourcemeta::core::JSON::String{identifier});
visited.emplace(identifier);
});
for (const auto &entry : found) {
dependencies_internal(std::get<0>(entry), walker, resolver, callback,
default_dialect, std::get<1>(entry),
{sourcemeta::core::empty_weak_pointer}, visited);
}
}
auto embed_schema(sourcemeta::core::JSON &root,
const sourcemeta::core::Pointer &container,
const std::string_view identifier,
sourcemeta::core::JSON &&target) -> void {
auto *current{&root};
for (const auto &token : container) {
if (token.is_property()) {
current->assign_if_missing(token.to_property(),
sourcemeta::core::JSON::make_object());
current = ¤t->at(token.to_property());
} else {
assert(current->is_array() && current->size() >= token.to_index());
current = ¤t->at(token.to_index());
}
}
if (!current->is_object()) {
throw sourcemeta::blaze::SchemaError(
"Could not bundle to a container path that is not an object");
}
std::string key{identifier};
// Ensure we get a definitions entry that does not exist
while (current->defines(key)) {
key += "/x";
}
current->assign(key, std::move(target));
}
auto elevate_embedded_resources(
sourcemeta::core::JSON &remote, sourcemeta::core::JSON &root,
const sourcemeta::core::Pointer &container,
const sourcemeta::blaze::SchemaBaseDialect remote_dialect,
const sourcemeta::blaze::SchemaResolver &resolver,
std::string_view default_dialect,
std::unordered_map<sourcemeta::core::JSON::String,
sourcemeta::core::JSON::String> &bundled) -> void {
const auto keyword{sourcemeta::blaze::definitions_keyword(remote_dialect)};
const sourcemeta::core::JSON::String keyword_string{keyword};
if (keyword.empty() || !remote.is_object() ||
!remote.defines(keyword_string) ||
!remote.at(keyword_string).is_object()) {
return;
}
auto &defs{remote.at(keyword_string)};
// Navigate to the root container once, as it doesn't change per entry
const sourcemeta::core::JSON *root_container{&root};
bool container_exists{true};
for (const auto &token : container) {
if (!token.is_property() || !root_container->is_object() ||
!root_container->defines(token.to_property())) {
container_exists = false;
break;
}
root_container = &root_container->at(token.to_property());
}
std::vector<sourcemeta::core::JSON::String> to_extract;
std::vector<sourcemeta::core::JSON::String> to_remove;
for (const auto &entry : defs.as_object()) {
const auto &key{entry.first};
const auto &value{entry.second};
const auto entry_dialect{
sourcemeta::blaze::base_dialect(value, resolver, default_dialect)};
const auto effective_entry_dialect{entry_dialect.value_or(remote_dialect)};
const auto identifier{
sourcemeta::blaze::identify(value, effective_entry_dialect)};
if (identifier.empty() || identifier != key ||
!sourcemeta::core::URI{identifier}.is_absolute()) {
continue;
}
const sourcemeta::core::JSON::String identifier_string{identifier};
if (bundled.contains(identifier_string)) {
if (container_exists && root_container->is_object()) {
for (const auto &root_entry : root_container->as_object()) {
if (!root_entry.first.starts_with(identifier_string)) {
continue;
}
const auto stored_dialect{sourcemeta::blaze::base_dialect(
root_entry.second, resolver, default_dialect)};
const auto effective_stored_dialect{stored_dialect.has_value()
? stored_dialect.value()
: remote_dialect};
const auto stored_id{sourcemeta::blaze::identify(
root_entry.second, effective_stored_dialect)};
if (stored_id != identifier_string) {
continue;
}
if (root_entry.second != value) {
throw sourcemeta::blaze::SchemaError(
"Conflicting embedded resources with the same identifier");
}
break;
}
}
to_remove.emplace_back(key);
} else {
to_extract.emplace_back(key);
bundled.emplace(identifier_string, identifier_string);
}
}
for (const auto &key : to_extract) {
auto value{std::move(defs.at(key))};
defs.erase(key);
embed_schema(root, container, key, std::move(value));
}
for (const auto &key : to_remove) {
defs.erase(key);
}
if (defs.empty()) {
remote.erase(sourcemeta::core::JSON::String{keyword});
}
}
auto bundle_schema(sourcemeta::core::JSON &root,
const sourcemeta::core::Pointer &container,
sourcemeta::core::JSON &subschema,
const sourcemeta::blaze::SchemaWalker &walker,
const sourcemeta::blaze::SchemaResolver &resolver,
const sourcemeta::blaze::BundleMode mode,
std::string_view default_dialect,
std::string_view default_id,
const sourcemeta::blaze::SchemaFrame::Paths &paths,
std::unordered_map<sourcemeta::core::JSON::String,
sourcemeta::core::JSON::String> &bundled,
const std::size_t depth = 0) -> void {
// Create a fresh frame for each schema we analyze to avoid key collisions
// between different schemas that have references at the same pointer paths
sourcemeta::blaze::SchemaFrame frame{
sourcemeta::blaze::SchemaFrame::Mode::References};
if (depth == 0) {
frame.analyse(
subschema, walker, resolver, default_dialect, default_id,
// We only want to frame in "wrapper" mode for the top level object
paths);
} else {
frame.analyse(subschema, walker, resolver, default_dialect, default_id);
}
std::vector<std::tuple<sourcemeta::core::JSON, sourcemeta::core::JSON::String,
sourcemeta::blaze::SchemaBaseDialect>>
deferred;
std::vector<
std::pair<sourcemeta::core::Pointer, sourcemeta::core::JSON::String>>
ref_rewrites;
frame.for_each_unresolved_reference([&](const auto &pointer,
const auto &reference) {
// We don't want to bundle official schemas, as we can expect
// virtually all implementations to understand them out of the box.
// Depending on the bundling strategy, we may skip meta-schemas entirely
if (is_skippable_metaschema_reference(mode, pointer,
reference.destination)) {
return;
}
// If we can't find the destination but there is a base and we can
// find base, then we are facing an unresolved fragment
if (!reference.base.empty() && frame.traverse(reference.base).has_value()) {
throw sourcemeta::blaze::SchemaReferenceError(
reference.destination, sourcemeta::core::to_pointer(pointer),
"Could not resolve schema reference");
}
if (reference.base.empty()) {
throw sourcemeta::blaze::SchemaReferenceError(
reference.destination, sourcemeta::core::to_pointer(pointer),
"Could not resolve schema reference");
}
assert(!reference.base.empty());
const sourcemeta::core::JSON::String identifier{reference.base};
if (bundled.contains(identifier)) {
const auto &mapped_id{bundled.at(identifier)};
if (mapped_id != identifier) {
sourcemeta::core::URI rewrite_uri{mapped_id};
if (reference.fragment.has_value()) {
rewrite_uri.fragment(reference.fragment.value());
}
ref_rewrites.emplace_back(sourcemeta::core::to_pointer(pointer),
rewrite_uri.recompose());
}
return;
}
auto remote{resolver(identifier)};
if (!remote.has_value()) {
if (frame.traverse(identifier).has_value()) {
throw sourcemeta::blaze::SchemaReferenceError(
reference.destination, sourcemeta::core::to_pointer(pointer),
"Could not resolve schema reference");
}
throw sourcemeta::blaze::SchemaResolutionError(
identifier, "Could not resolve the reference to an external schema");
}
if (!sourcemeta::blaze::is_schema(remote.value())) {
throw sourcemeta::blaze::SchemaReferenceError(
identifier, sourcemeta::core::to_pointer(pointer),
"The JSON document is not a valid JSON Schema");
}
const auto remote_base_dialect{sourcemeta::blaze::base_dialect(
remote.value(), resolver, default_dialect)};
if (!remote_base_dialect.has_value()) {
throw sourcemeta::blaze::SchemaReferenceError(
identifier, sourcemeta::core::to_pointer(pointer),
"The JSON document is not a valid JSON Schema");
}
auto remote_id =
sourcemeta::blaze::identify(remote.value(), resolver, default_dialect);
// If the reference has a fragment, verify it exists in the remote
// schema
if (reference.fragment.has_value()) {
// TODO: The fact that we have to re-frame on each loop pass to check
// for this is probably insanely slow
sourcemeta::blaze::SchemaFrame remote_frame{
sourcemeta::blaze::SchemaFrame::Mode::Locations};
remote_frame.analyse(remote.value(), walker, resolver, default_dialect,
identifier);
if (!remote_frame.traverse(reference.destination).has_value()) {
throw sourcemeta::blaze::SchemaReferenceError(
reference.destination, sourcemeta::core::to_pointer(pointer),
"Could not resolve schema reference");
}
}
sourcemeta::core::JSON::String effective_id{
remote_id.empty() ? sourcemeta::core::JSON::String{identifier}
: sourcemeta::core::JSON::String{remote_id}};
if (remote.value().is_object()) {
sourcemeta::blaze::reidentify(remote.value(), effective_id,
remote_base_dialect.value());
}
if (effective_id != identifier) {
sourcemeta::core::URI rewrite_uri{effective_id};
if (reference.fragment.has_value()) {
rewrite_uri.fragment(reference.fragment.value());
}
ref_rewrites.emplace_back(sourcemeta::core::to_pointer(pointer),
rewrite_uri.recompose());
}
bundled.emplace(identifier, effective_id);
bundled.emplace(effective_id, effective_id);
deferred.emplace_back(std::move(remote).value(), std::move(effective_id),
remote_base_dialect.value());
});
for (auto &[rewrite_pointer, rewrite_value] : ref_rewrites) {
sourcemeta::core::set(subschema, rewrite_pointer,
sourcemeta::core::JSON{rewrite_value});
}
for (auto &[remote, effective_id, remote_dialect] : deferred) {
bundle_schema(root, container, remote, walker, resolver, mode,
default_dialect, effective_id, paths, bundled, depth + 1);
elevate_embedded_resources(remote, root, container, remote_dialect,
resolver, default_dialect, bundled);
embed_schema(root, container, effective_id, std::move(remote));
}
}
} // namespace
namespace sourcemeta::blaze {
auto dependencies(const sourcemeta::core::JSON &schema,
const SchemaWalker &walker, const SchemaResolver &resolver,
const DependencyCallback &callback,
std::string_view default_dialect, std::string_view default_id,
const SchemaFrame::Paths &paths) -> void {
std::unordered_set<std::string> visited;
dependencies_internal(schema, walker, resolver, callback, default_dialect,
default_id, paths, visited);
}
// TODO: Refactor this function to internally rely on the `.dependencies()`
// function
auto bundle(sourcemeta::core::JSON &schema, const SchemaWalker &walker,
const SchemaResolver &resolver, const BundleMode mode,
std::string_view default_dialect, std::string_view default_id,
const std::optional<sourcemeta::core::Pointer> &default_container,
const SchemaFrame::Paths &paths) -> void {
// Pre-scan the schema to find any already-embedded schemas and mark them
// as bundled to avoid re-embedding them. This includes the root schema itself
// and any schemas already embedded within it
std::unordered_map<sourcemeta::core::JSON::String,
sourcemeta::core::JSON::String>
bundled;
SchemaFrame initial_frame{SchemaFrame::Mode::Locations};
initial_frame.analyse(schema, walker, resolver, default_dialect, default_id,
paths);
initial_frame.for_each_resource_uri([&bundled](const auto &uri) {
bundled.emplace(sourcemeta::core::JSON::String{uri},
sourcemeta::core::JSON::String{uri});
});
if (default_container.has_value()) {
// This is undefined behavior
assert(!default_container.value().empty());
bundle_schema(schema, default_container.value(), schema, walker, resolver,
mode, default_dialect, default_id, paths, bundled);
return;
}
// If the schema identifier is implicit, add it to the top-level of the
// bundled schema. Otherwise, potential relative references based on this
// implicit base URI will likely not resolve unless end users happen to
// know that this implicit base URI is.
if (!default_id.empty() &&
identify(schema, resolver, default_dialect).empty()) {
reidentify(schema, default_id, resolver, default_dialect);
}
const auto schema_base_dialect{
base_dialect(schema, resolver, default_dialect)};
if (!schema_base_dialect.has_value()) {
throw SchemaError(
"Could not determine how to perform bundling in this dialect");
}
const auto container_keyword{
definitions_keyword(schema_base_dialect.value())};
if (container_keyword.empty()) {
SchemaFrame frame{SchemaFrame::Mode::References};
frame.analyse(schema, walker, resolver, default_dialect, default_id);
if (frame.standalone()) {
return;
}
throw SchemaError(
"Could not determine how to perform bundling in this dialect");
}
if (ref_overrides_adjacent_keywords(schema_base_dialect.value()) &&
schema.is_object() && schema.defines("$ref")) {
if (schema.size() == 1) {
const auto is_draft3{schema_base_dialect.value() ==
SchemaBaseDialect::JSON_Schema_Draft_3 ||
schema_base_dialect.value() ==
SchemaBaseDialect::JSON_Schema_Draft_3_Hyper};
auto branches{sourcemeta::core::JSON::make_array()};
branches.push_back(schema);
schema.at("$ref").into(std::move(branches));
schema.rename("$ref", is_draft3 ? "extends" : "allOf");
} else {
throw SchemaError(
"Cannot bundle a JSON Schema Draft 7 or older with a top-level "
"`$ref` (which overrides sibling keywords) without introducing "
"undefined behavior");
}
}
bundle_schema(schema, {sourcemeta::core::JSON::String{container_keyword}},
schema, walker, resolver, mode, default_dialect, default_id,
paths, bundled);
}
auto bundle(const sourcemeta::core::JSON &schema, const SchemaWalker &walker,
const SchemaResolver &resolver, const BundleMode mode,
std::string_view default_dialect, std::string_view default_id,
const std::optional<sourcemeta::core::Pointer> &default_container,
const SchemaFrame::Paths &paths) -> sourcemeta::core::JSON {
sourcemeta::core::JSON copy = schema;
bundle(copy, walker, resolver, mode, default_dialect, default_id,
default_container, paths);
return copy;
}
} // namespace sourcemeta::blaze