diff --git a/include/rfl/parsing/NamedTupleParser.hpp b/include/rfl/parsing/NamedTupleParser.hpp index c4e2fd312..835e7abc7 100644 --- a/include/rfl/parsing/NamedTupleParser.hpp +++ b/include/rfl/parsing/NamedTupleParser.hpp @@ -2,6 +2,7 @@ #define RFL_PARSING_NAMEDTUPLEPARSER_HPP_ #include +#include #include #include #include @@ -353,11 +354,26 @@ struct NamedTupleParser { make_nested_error(std::string(current_name), stream.str())); } else if constexpr (!internal::has_default_val_v) { - if constexpr (!std::is_const_v) { - ::new (rfl::get<_i>(_view)) ValueType(); + using NonConstT = std::remove_const_t; + const auto ptr = const_cast(rfl::get<_i>(_view)); + if constexpr (std::is_nothrow_default_constructible_v) { + ::new (ptr) NonConstT(); } else { - using NonConstT = std::remove_const_t; - ::new (const_cast(rfl::get<_i>(_view))) NonConstT(); + // The default value can throw, for instance a Validator whose + // default value is invalid. + try { + ::new (ptr) NonConstT(); + } catch (std::exception& e) { + constexpr auto current_name = + internal::nth_element_t<_i, FieldTypes...>::name(); + _errors->emplace_back(make_nested_error( + std::string(current_name), + "Field named '" + std::string(current_name) + + "' not found, and its default value could not be " + "constructed: " + + e.what())); + return; + } } std::get<_i>(*_set) = true; } diff --git a/include/rfl/parsing/ParserSkip.hpp b/include/rfl/parsing/ParserSkip.hpp index 6d748ba04..5b31767f6 100644 --- a/include/rfl/parsing/ParserSkip.hpp +++ b/include/rfl/parsing/ParserSkip.hpp @@ -1,6 +1,7 @@ #ifndef RFL_PARSING_PARSER_SKIP_HPP_ #define RFL_PARSING_PARSER_SKIP_HPP_ +#include #include #include @@ -39,8 +40,14 @@ struct ParserSkip { static Result> read(const R& _r, const InputVarType& _var) noexcept { if constexpr (_skip_deserialization) { - return internal::Skip( - std::remove_cvref_t()); + // The default value can throw, for instance a Validator whose default + // value is invalid. + try { + return internal::Skip( + std::remove_cvref_t()); + } catch (std::exception& e) { + return error(e.what()); + } } else { const auto to_skip = [&](auto&& _t) { return internal::Skip( diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index bc48a8700..dab0c7fdf 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -1,6 +1,7 @@ #ifndef RFL_PARSING_TUPLEREADER_HPP_ #define RFL_PARSING_TUPLEREADER_HPP_ +#include #include #include #include @@ -80,9 +81,25 @@ class TupleReader { static_cast(_i), "Field " + std::to_string(_i) + " was required, but missing."); return; - } else { + } else if constexpr (std::is_nothrow_default_constructible_v< + CurrentType>) { ::new (&(rfl::get<_i>(*tuple_))) CurrentType(); ++num_set_; + } else { + // The default value can throw, for instance a Validator whose + // default value is invalid. + try { + ::new (&(rfl::get<_i>(*tuple_))) CurrentType(); + ++num_set_; + } catch (std::exception& e) { + *_err = make_nested_error( + static_cast(_i), + "Field " + std::to_string(_i) + + " was missing, and its default value could not be " + "constructed: " + + e.what()); + return; + } } } handle_missing_fields_impl<_i + 1>(_err); diff --git a/tests/json/test_skip_throwing_default.cpp b/tests/json/test_skip_throwing_default.cpp new file mode 100644 index 000000000..652eff917 --- /dev/null +++ b/tests/json/test_skip_throwing_default.cpp @@ -0,0 +1,107 @@ +#include + +#include +#include +#include +#include +#include +#include + +namespace test_skip_throwing_default { + +using Positive = rfl::Validator>; + +using Path = std::vector; + +struct WithSkip { + rfl::Skip s; + int y; +}; + +struct SkipLast { + int y; + rfl::Skip s; +}; + +/// Not required, because its ReflectionType is a std::optional, and counts +/// the instances that are alive. +struct ThrowingDefault { + using ReflectionType = std::optional; + + static inline int alive = 0; + + ThrowingDefault() { throw std::runtime_error("no default"); } + ThrowingDefault(const ReflectionType&) { ++alive; } + ThrowingDefault(const ThrowingDefault&) { ++alive; } + ~ThrowingDefault() { --alive; } + + ReflectionType reflection() const { return std::nullopt; } +}; + +struct WithThrowingDefault { + int y; + ThrowingDefault t; +}; + +TEST(json, test_skip_throwing_default) { + const auto res = rfl::json::read(R"({"y": 1, "s": 5})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Failed to parse field 's': Value expected to be greater than or " + "equal to 1, but got 0."); +} + +TEST(json, test_skip_throwing_default_field_absent) { + const auto res = rfl::json::read(R"({"y": 1})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Field named 's' 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 details = res.error().details(); + ASSERT_EQ(details.size(), 1); + EXPECT_EQ(details[0].path, (Path{"s"})); + EXPECT_EQ(details[0].message, res.error().what()); +} + +TEST(json, test_skip_throwing_default_field_absent_no_field_names) { + const auto res = rfl::json::read(R"([1])"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Field named 's' not found, and its default value could not be " + "constructed: Value expected to be greater than or equal to 1, " + "but got 0."); +} + +TEST(json, test_skip_throwing_default_tuple_element_absent) { + const auto res = + rfl::json::read>>(R"([1])"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Field 1 was missing, and its default value could not be " + "constructed: Value expected to be greater than or equal to 1, " + "but got 0."); + const auto details = res.error().details(); + ASSERT_EQ(details.size(), 1); + EXPECT_EQ(details[0].path, (Path{size_t{1}})); +} + +// No destructor may run for the field that could not be constructed. +TEST(json, test_skip_throwing_default_does_not_destroy_the_field) { + const auto res = rfl::json::read(R"({"y": 1})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Field named 't' not found, and its default value could not be " + "constructed: no default"); + EXPECT_EQ(ThrowingDefault::alive, 0); + + const auto tup = + rfl::json::read>(R"([1])"); + ASSERT_FALSE(tup); + EXPECT_EQ(ThrowingDefault::alive, 0); + + ASSERT_TRUE(rfl::json::read(R"({"y": 1, "t": 2})")); + EXPECT_EQ(ThrowingDefault::alive, 0); +} + +} // namespace test_skip_throwing_default