-
Notifications
You must be signed in to change notification settings - Fork 153
Fix crash when validating null values in objects with additionalProperties #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #include <iostream> | ||
| #include <nlohmann/json-schema.hpp> | ||
|
|
||
| using nlohmann::json; | ||
| using nlohmann::json_schema::json_validator; | ||
|
|
||
| // Test: object with additionalProperties containing null value should validate | ||
| // additionalProperties causes traversal into the object where null value triggers the bug | ||
| static const json schema = R"( | ||
| { | ||
| "properties": { | ||
| "x": {"type": "object", "additionalProperties": {}} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. additionalProperties is defaulting to be an empty schema. So can be removed.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why, but if I remove this, the test succeeds even without the fix.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty schema is seen as maybe seen as false. So additionalProperties are not allowed. Not sure whether this is expected.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely something wrong here,
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤷♂️ Without the fix:
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's late, but I read a little bit further: The default-patch handling seems flawed: if (instance.is_null()) {
patch.add(nlohmann::json::json_pointer{}, default_value_);
}I don't understand how the, seemingly, global instance patch, can use the root-pointer. Such a patch, when applied, will overwrite everything from the instance. This is what you see in your test. The incoherence of your test result regarding I think removing the if as you did, or at least change it: if (instance.is_null() && default_value_) {
patch.add(ptr, default_value_);
}This would still create a patch for a possible default value of the instance is null. So this is wrong. Actually a default value would need to be added only if the json-path does not exist in the validated instance. As it is stands, default values patch creation seems flawed... Not sure what to do, is the original author still available to check and explain? @Finkman This change 59c9d62 was adding these lines. |
||
| } | ||
| })"_json; | ||
|
|
||
| int main(void) | ||
| { | ||
| json_validator validator{}; | ||
| validator.set_root_schema(schema); | ||
|
|
||
| json instance = R"({"x": {"nested": null}})"_json; | ||
|
|
||
| const auto patch = validator.validate(instance); | ||
| const auto result = instance.patch(patch); | ||
|
|
||
| // null value should be preserved | ||
| if (!result["x"]["nested"].is_null()) { | ||
| std::cerr << "Failed: nested should be null, got: " << result["x"]["nested"].dump() << std::endl; | ||
| return 1; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.