Skip to content

Commit 67686fa

Browse files
committed
Fix crash when validating null values in objects with additionalProperties
Remove erroneous code that added a patch with empty JSON pointer for any null instance. Null is a valid JSON value and should not trigger a patch.
1 parent fb270d5 commit 67686fa

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/json-validator.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,6 @@ class type_schema : public schema
588588
else_->validate(ptr, instance, patch, e);
589589
}
590590
}
591-
if (instance.is_null()) {
592-
patch.add(nlohmann::json::json_pointer{}, default_value_);
593-
}
594591
}
595592

596593
protected:

test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,7 @@ add_test(NAME issue-255-error-message-limit-precision COMMAND issue-255-error-me
9696
add_executable(issue-105-verbose-combination-errors issue-105-verbose-combination-errors.cpp)
9797
target_link_libraries(issue-105-verbose-combination-errors nlohmann_json_schema_validator)
9898
add_test(NAME issue-105-verbose-combination-errors COMMAND issue-105-verbose-combination-errors)
99+
100+
add_executable(issue-null-values-in-object issue-null-values-in-object.cpp)
101+
target_link_libraries(issue-null-values-in-object nlohmann_json_schema_validator)
102+
add_test(NAME issue-null-values-in-object COMMAND issue-null-values-in-object)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
#include <nlohmann/json-schema.hpp>
3+
4+
using nlohmann::json;
5+
using nlohmann::json_schema::json_validator;
6+
7+
// Test: object with additionalProperties containing null value should validate
8+
// additionalProperties causes traversal into the object where null value triggers the bug
9+
static const json schema = R"(
10+
{
11+
"properties": {
12+
"x": {"type": "object", "additionalProperties": {}}
13+
}
14+
})"_json;
15+
16+
int main(void)
17+
{
18+
json_validator validator{};
19+
validator.set_root_schema(schema);
20+
21+
json instance = R"({"x": {"nested": null}})"_json;
22+
23+
const auto patch = validator.validate(instance);
24+
const auto result = instance.patch(patch);
25+
26+
// null value should be preserved
27+
if (!result["x"]["nested"].is_null()) {
28+
std::cerr << "Failed: nested should be null, got: " << result["x"]["nested"].dump() << std::endl;
29+
return 1;
30+
}
31+
32+
return 0;
33+
}

0 commit comments

Comments
 (0)