Skip to content

Commit e11bc2e

Browse files
authored
Respect ordering of --resolve arguments to avoid meta-schema ambiguity (#674)
See: #672 Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 1b8811e commit e11bc2e

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

src/input.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ inline auto for_each_json(const std::vector<std::string_view> &arguments,
309309
? configuration.value().absolute_path
310310
: current_path,
311311
blacklist, extensions, result, options);
312+
std::sort(result.begin(), result.end(),
313+
[](const auto &left, const auto &right) { return left < right; });
312314
} else {
313315
std::unordered_set<std::string> seen_configurations;
314316
for (const auto &entry : arguments) {
@@ -328,13 +330,14 @@ inline auto for_each_json(const std::vector<std::string_view> &arguments,
328330

329331
const auto extensions{parse_extensions(options, std::nullopt)};
330332
for (const auto &entry : arguments) {
333+
const auto before{result.size()};
331334
handle_json_entry(entry, blacklist, extensions, result, options);
335+
std::sort(
336+
result.begin() + static_cast<std::ptrdiff_t>(before), result.end(),
337+
[](const auto &left, const auto &right) { return left < right; });
332338
}
333339
}
334340

335-
std::sort(result.begin(), result.end(),
336-
[](const auto &left, const auto &right) { return left < right; });
337-
338341
return result;
339342
}
340343

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ add_jsonschema_test_unix(validate/fail_resolve_invalid_id)
110110
add_jsonschema_test_unix(validate/fail_resolve_invalid_recursive_ref)
111111
add_jsonschema_test_unix(validate/fail_resolve_missing_core_vocabulary)
112112
add_jsonschema_test_unix(validate/fail_resolve_relative_metaschema)
113+
add_jsonschema_test_unix(validate/pass_resolve_custom_metaschema_ordering)
113114
add_jsonschema_test_unix(validate/pass_jsonl_bigint)
114115
add_jsonschema_test_unix(validate/fail_trace)
115116
add_jsonschema_test_unix(validate/fail_trace_fast)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
TMP="$(mktemp -d)"
7+
clean() { rm -rf "$TMP"; }
8+
trap clean EXIT
9+
10+
cat << 'EOF' > "$TMP/schema.json"
11+
{
12+
"$schema": "https://json-schema.org/draft/2020-12/schema",
13+
"$ref": "https://example.com/my-schema"
14+
}
15+
EOF
16+
17+
cat << 'EOF' > "$TMP/zz-meta.json"
18+
{
19+
"$schema": "https://json-schema.org/draft/2020-12/schema",
20+
"$id": "https://example.com/my-meta",
21+
"$vocabulary": {
22+
"https://json-schema.org/draft/2020-12/vocab/core": true,
23+
"https://json-schema.org/draft/2020-12/vocab/applicator": true,
24+
"https://json-schema.org/draft/2020-12/vocab/validation": true
25+
}
26+
}
27+
EOF
28+
29+
cat << 'EOF' > "$TMP/aa-schema.json"
30+
{
31+
"$schema": "https://example.com/my-meta",
32+
"$id": "https://example.com/my-schema",
33+
"type": "string"
34+
}
35+
EOF
36+
37+
cat << 'EOF' > "$TMP/instance.json"
38+
"hello"
39+
EOF
40+
41+
"$1" validate "$TMP/schema.json" "$TMP/instance.json" \
42+
--resolve "$TMP/zz-meta.json" \
43+
--resolve "$TMP/aa-schema.json" --debug 2>"$TMP/stderr.txt"
44+
45+
cat << EOF > "$TMP/expected.txt"
46+
debug: Detecting schema resources from file: $(realpath "$TMP")/zz-meta.json
47+
debug: Importing schema into the resolution context: file://$(realpath "$TMP")/zz-meta.json
48+
debug: Importing schema into the resolution context: https://example.com/my-meta
49+
debug: Detecting schema resources from file: $(realpath "$TMP")/aa-schema.json
50+
debug: Importing schema into the resolution context: file://$(realpath "$TMP")/aa-schema.json
51+
debug: Importing schema into the resolution context: https://example.com/my-schema
52+
ok: $(realpath "$TMP")/instance.json
53+
matches $(realpath "$TMP")/schema.json
54+
EOF
55+
56+
diff "$TMP/stderr.txt" "$TMP/expected.txt"

0 commit comments

Comments
 (0)