Skip to content

Commit 5fbc407

Browse files
authored
Don't emit duplicated unevaluatedItems annotations in SimpleOutput (#421)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 2c03d3c commit 5fbc407

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/compiler/compile_output_simple.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ auto SimpleOutput::operator()(
4646
if (type == EvaluationType::Post) {
4747
Location location{instance_location, std::move(effective_evaluate_path),
4848
step.keyword_location};
49-
this->annotations_[std::move(location)].push_back(annotation);
49+
const auto match{this->annotations_.find(location)};
50+
if (match == this->annotations_.cend()) {
51+
this->annotations_[std::move(location)].push_back(annotation);
52+
53+
// To avoid emitting the exact same annotation more than once
54+
// This is right now mostly because of `unevaluatedItems`
55+
} else if (match->second.back() != annotation) {
56+
match->second.push_back(annotation);
57+
}
5058
}
5159

5260
return;

test/compiler/compiler_output_simple_test.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,35 @@ TEST(Compiler_output_simple, annotations_success_7) {
738738
sourcemeta::core::JSON{0});
739739
}
740740

741+
TEST(Compiler_output_simple, annotations_success_8) {
742+
const sourcemeta::core::JSON schema{sourcemeta::core::parse_json(R"JSON({
743+
"$schema": "https://json-schema.org/draft/2020-12/schema",
744+
"unevaluatedItems": true
745+
})JSON")};
746+
747+
const auto schema_template{sourcemeta::blaze::compile(
748+
schema, sourcemeta::core::schema_official_walker,
749+
sourcemeta::core::schema_official_resolver,
750+
sourcemeta::blaze::default_schema_compiler,
751+
sourcemeta::blaze::Mode::Exhaustive)};
752+
753+
const sourcemeta::core::JSON instance{
754+
sourcemeta::core::parse_json("[ 1, 2, 3, 4 ]")};
755+
756+
sourcemeta::blaze::SimpleOutput output{instance};
757+
sourcemeta::blaze::Evaluator evaluator;
758+
const auto result{
759+
evaluator.validate(schema_template, instance, std::ref(output))};
760+
EXPECT_TRUE(result);
761+
762+
EXPECT_ANNOTATION_COUNT(output, 1);
763+
764+
EXPECT_ANNOTATION_ENTRY(output, "", "/unevaluatedItems", "#/unevaluatedItems",
765+
1);
766+
EXPECT_ANNOTATION_VALUE(output, "", "/unevaluatedItems", "#/unevaluatedItems",
767+
0, sourcemeta::core::JSON{true});
768+
}
769+
741770
TEST(Compiler_output_simple, annotations_failure_1) {
742771
const sourcemeta::core::JSON schema{sourcemeta::core::parse_json(R"JSON({
743772
"$schema": "https://json-schema.org/draft/2020-12/schema",

0 commit comments

Comments
 (0)