Repository navigation
Fix rfl::DefaultVal in NamedTuples and TaggedUnion schemas #786
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
Merged
liuzicheng1987
merged 1 commit into
munich-data-engineering:main
from
clockblocker:default-val-fixes-main
Nov 19, 2026
+191
−15
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
tests/json/test_default_val_without_default_construction.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <optional> | ||
| #include <rfl.hpp> | ||
| #include <rfl/json.hpp> | ||
| #include <string> | ||
|
|
||
| // rfl::DefaultVal in places where the struct around it is not | ||
| // default-constructed before it is read: rfl::NamedTuple and the schema of an | ||
| // rfl::TaggedUnion alternative. | ||
| namespace test_default_val_without_default_construction { | ||
|
|
||
| struct NoDefaultConstructor { | ||
| using ReflectionType = int; | ||
| NoDefaultConstructor(int _v) : v_(_v) {} | ||
| int reflection() const { return v_; } | ||
| int v_; | ||
| }; | ||
|
|
||
| // A NamedTuple has no in-class initializers, so a missing rfl::DefaultVal | ||
| // field is value-initialized. It is constructed before it is used. | ||
| TEST(json, test_default_val_named_tuple_missing_field) { | ||
| using WithString = | ||
| rfl::NamedTuple<rfl::Field<"n", int>, | ||
| rfl::Field<"s", rfl::DefaultVal<std::string>>, | ||
| rfl::Field<"opt", std::optional<int>>>; | ||
| const auto str = rfl::json::read<WithString>(R"({"n":3})"); | ||
| ASSERT_TRUE(str) << str.error().what(); | ||
| EXPECT_EQ(rfl::get<"s">(str.value()).value(), ""); | ||
| EXPECT_FALSE(rfl::get<"opt">(str.value())); | ||
| } | ||
|
|
||
| // If the value cannot be value-initialized, or its constructor throws, the | ||
| // missing field is an error. | ||
| TEST(json, test_default_val_named_tuple_missing_field_without_zero_value) { | ||
| using NoZero = | ||
| rfl::NamedTuple<rfl::Field<"o", rfl::DefaultVal<NoDefaultConstructor>>, | ||
| rfl::Field<"z", int>>; | ||
| const auto no_zero = rfl::json::read<NoZero>(R"({"z":1})"); | ||
| ASSERT_FALSE(no_zero); | ||
| EXPECT_EQ(no_zero.error().what(), std::string("Field named 'o' not found.")); | ||
|
|
||
| using Invalid = rfl::NamedTuple< | ||
| rfl::Field<"v", rfl::DefaultVal<rfl::Validator<int, rfl::Minimum<1>>>>, | ||
| rfl::Field<"z", int>>; | ||
| const auto invalid = rfl::json::read<Invalid>(R"({"z":1})"); | ||
| ASSERT_FALSE(invalid); | ||
| EXPECT_EQ(invalid.error().what(), | ||
| std::string("Field named 'v' not found, and its default value " | ||
| "could not be constructed: Value expected to be " | ||
| "greater than or equal to 1, but got 0.")); | ||
|
|
||
| const auto present = rfl::json::read<Invalid>(R"({"v":3,"z":1})"); | ||
| ASSERT_TRUE(present) << present.error().what(); | ||
| EXPECT_EQ(rfl::get<"v">(present.value()).value().value(), 3); | ||
| } | ||
|
|
||
| // The schema reads the defaults of an rfl::TaggedUnion alternative from a | ||
| // default-constructed instance of that alternative. | ||
| struct DefaultAlternative { | ||
| rfl::DefaultVal<std::string> output = std::string("out.csv"); | ||
| int k = 0; | ||
| }; | ||
|
|
||
| struct OtherAlternative { | ||
| std::string q; | ||
| }; | ||
|
|
||
| TEST(json, test_default_val_tagged_union_schema) { | ||
| EXPECT_EQ( | ||
| (rfl::json::to_schema< | ||
| rfl::TaggedUnion<"type", DefaultAlternative, OtherAlternative>>()), | ||
| R"({"$schema":"https://json-schema.org/draft/2020-12/schema","anyOf":[{"$ref":"#/$defs/test_default_val_without_default_construction__DefaultAlternative__tagged"},{"$ref":"#/$defs/test_default_val_without_default_construction__OtherAlternative__tagged"}],"$defs":{"test_default_val_without_default_construction__DefaultAlternative__tagged":{"type":"object","properties":{"type":{"type":"string","enum":["DefaultAlternative"]},"output":{"type":"string","default":"out.csv"},"k":{"type":"integer"}},"required":["type","k"]},"test_default_val_without_default_construction__OtherAlternative__tagged":{"type":"object","properties":{"type":{"type":"string","enum":["OtherAlternative"]},"q":{"type":"string"}},"required":["type","q"]}}})"); | ||
| } | ||
|
|
||
| } // namespace test_default_val_without_default_construction |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am pretty sure this is UB...
"Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior."
https://en.cppreference.com/cpp/language/const_cast
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. It was not a UB, but the cast itself was unnecessary.
As far as I understand, this cast was ~the same as this piece in the doc:
AI-authored:
The
constis only on the pointer:value_initialize_default_valtakesconst ValueType*, while the field itself isn't const (a NamedTuple with a const field doesn't compile). The storage is the rawbufinNamedTupleParser::read, with no object in it yet, so placement-new creates an object and doesn't modify one.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separately:
I've asked Jarvis to scan the codebase searching for the problematic "const modification through a non-const access" and this spot seems to have it.
Filed a PR to fix it.