From fc8e765c35e655d3dabc651d17045eec4d4fda26 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Tue, 17 Nov 2026 19:57:28 +0100 Subject: [PATCH] Read structs with defaults that hold a Validator without a valid default Co-Authored-By: Claude Opus 5.5 --- docs/content/default_val.md | 34 +- docs/content/processors.md | 9 +- include/rfl/Validator.hpp | 44 +- include/rfl/internal/DeferredDefaults.hpp | 87 +++ include/rfl/parsing/NamedTupleParser.hpp | 57 +- include/rfl/parsing/Parser_default.hpp | 188 +++++- include/rfl/parsing/ViewReaderWithDefault.hpp | 2 + ...ReaderWithDefaultAndStrippedFieldNames.hpp | 2 + include/rfl/parsing/check_default_value.hpp | 266 ++++++++ src/reflectcpp.cpp | 1 + src/rfl/internal/DeferredDefaults.cpp | 10 + tests/json/test_default_construct_error.cpp | 7 +- .../json/test_default_val_with_validator.cpp | 240 +++++++ ...test_default_val_with_validator_limits.cpp | 608 ++++++++++++++++++ 14 files changed, 1518 insertions(+), 37 deletions(-) create mode 100644 include/rfl/internal/DeferredDefaults.hpp create mode 100644 include/rfl/parsing/check_default_value.hpp create mode 100644 src/rfl/internal/DeferredDefaults.cpp create mode 100644 tests/json/test_default_val_with_validator.cpp create mode 100644 tests/json/test_default_val_with_validator_limits.cpp diff --git a/docs/content/default_val.md b/docs/content/default_val.md index 817860bb4..81ea3bb80 100644 --- a/docs/content/default_val.md +++ b/docs/content/default_val.md @@ -52,41 +52,51 @@ const auto homer = rfl::json::read(R"(["Homer"])").va // homer.town == "" ``` -## All fields need a valid default value +## Fields without a valid default value -A struct that contains an `rfl::DefaultVal` is default-constructed before it is read. The fields found in the input are then written into it. This means that every field of the struct needs a valid default value, even a required field that is in the input. +A struct that contains an `rfl::DefaultVal` is default-constructed before it is read, so that the fields missing from the input keep their default values. The fields found in the input are then replaced by the values read from it. -This matters when the default value of a field's type is not valid, for instance when the field has a validator: +Some fields have no valid default value, for instance a validator that an empty vector does not satisfy: ```cpp +using Foods = rfl::Validator, rfl::Size>>; + struct Person { rfl::DefaultVal name = "Homer Simpson"; - rfl::Validator, rfl::Size>> favorite_foods; + Foods favorite_foods; }; -// Fails, even though favorite_foods is in the input. -rfl::json::read(R"({"favorite_foods":["donuts","pizza"]})"); +const auto homer = rfl::json::read(R"({"favorite_foods":["donuts","pizza"]})"); ``` -An empty vector does not satisfy `rfl::Size>`, so `Person{}` throws an exception and you get the following error: +`Person{}` throws, but `read` then constructs the struct again with the check of such a validator deferred until the input has been read. If `favorite_foods` is in the input, its invalid default value has been replaced. If it is missing, you get the error `Field named 'favorite_foods' not found.` Outside of `read`, `Person{}` still throws. + +A field whose default value is invalid must not be missing, even where a missing field would otherwise get its default value, that is, for an `rfl::DefaultVal` or with the `rfl::DefaultIfMissing` processor: + +``` +Field named 'favorite_foods' not found, and its default value is invalid: Size validation failed: Value expected to be greater than or equal to 1, but got 0. +``` + +While the struct is constructed again, every validator that is default-constructed on the same thread keeps its invalid value instead of throwing, wherever it is: in a field, in a nested struct or container, in a temporary, or in a copy such as `rfl::DefaultVal other_foods = favorite_foods;`. After reading, `read` checks the validators inside every field that is missing from the input, and fails with the error above if one of them is invalid. `read` looks into structs, containers, `std::optional`, smart pointers, variants, tuples, the wrappers of reflect-cpp and classes with a `reflection()` method. If a missing field holds a class it cannot look into, you get the error `Field named '...' not found, and its default value could not be checked: ...`. + +If the struct holds fewer invalid validators than were default-constructed, an invalid value has been used up on the way, for instance a temporary in `int port = Port().value();`, or it is inside a class that `read` cannot look into. A copy can make up for a used-up value, though, and a default member initializer that reads a validator, as in `int count = favorite_foods->size();`, gets its invalid value without an error. A default member initializer that catches the exception of such a validator in order to fall back to another value gets no exception while validation is deferred, so the field must then be in the input. The struct cannot be read, and you get the error below, if an invalid value has been used up, or if `Person{}` throws for another reason, including a validator that is given an invalid value explicitly (as in `Foods favorite_foods = std::vector();`): ``` Could not default-construct Person, which is required because it contains rfl::DefaultVal. Give every field a valid default value. Original error: Size validation failed: Value expected to be greater than or equal to 1, but got 0. ``` -To avoid this, give the field a valid default value: +In all of these cases, you can give the field a valid default value: ```cpp struct Person { rfl::DefaultVal name = "Homer Simpson"; - rfl::Validator, rfl::Size>> favorite_foods = - std::vector({"none"}); + Foods favorite_foods = std::vector({"none"}); }; ``` -The field is still required. If `favorite_foods` is missing in the input, you get the error `Field named 'favorite_foods' not found.` The default value is only used for a missing field if you pass the `rfl::DefaultIfMissing` processor. +The field is still required. The default value is only used for a missing field if you pass the `rfl::DefaultIfMissing` processor. -The same restriction applies to every struct that is read with the `rfl::DefaultIfMissing` processor, whether it contains an `rfl::DefaultVal` or not. +All of this applies to every struct that is read with the `rfl::DefaultIfMissing` processor, whether it contains an `rfl::DefaultVal` or not. In JSON schemas, structs with `rfl::DefaultVal` keep their valid default values; any field whose default value holds an invalid validator or a class that cannot be checked gets no default value and is listed as required (unless `rfl::DefaultIfMissing` omits `required` for the whole schema). If an invalid value has been used up, the schema keeps no default values and marks those fields required. If `Person{}` throws for another reason, `rfl::json::to_schema()` throws that exception. If such a struct is nested inside another struct, the program ends with `std::terminate`, as it does without this feature. An `rfl::NamedTuple` has no in-class initializers. A missing `rfl::DefaultVal` field in it is set to `T{}`. If `T` has no default constructor, or `T{}` throws (for instance a validator that rejects its zero value), the field must be present in the input. diff --git a/docs/content/processors.md b/docs/content/processors.md index a431bb4e6..014c44dab 100644 --- a/docs/content/processors.md +++ b/docs/content/processors.md @@ -310,9 +310,12 @@ have gotten had you read the following JSON string: Because you have not passed a default value to town, the default value of the type is used instead. -Note that the struct is default-constructed before it is read. Every field -therefore needs a valid default value, even if the field is in the input. -This is explained in the documentation on `rfl::DefaultVal`. +Note that the struct is default-constructed before it is read. A field +whose default value is invalid, such as a validator that the default value +of its type does not satisfy, can be read, but it must not be missing: you +get the error `Field named '...' not found, and its default value is +invalid: ...`. The documentation on `rfl::DefaultVal` explains this and the +cases in which such a struct cannot be read at all. ### `rfl::DurationsAsCount` diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp index 9be58bf61..103aa5143 100644 --- a/include/rfl/Validator.hpp +++ b/include/rfl/Validator.hpp @@ -9,7 +9,9 @@ #include "AllOf.hpp" #include "Result.hpp" +#include "internal/DeferredDefaults.hpp" #include "internal/HasValidation.hpp" +#include "internal/cold.hpp" namespace rfl { @@ -52,8 +54,10 @@ struct Validator { } /// Default constructor - validates a default-constructed T. - /// @throws std::exception if validation of the default value fails - Validator() : value_(ValidationType::validate(T()).value()) {} + /// @throws std::exception if validation of the default value fails, unless + /// the parser is default-constructing a struct to read into, see + /// internal::DeferredDefaults. + Validator() : value_(valid_or_deferred()) {} /// Move constructor. /// @param _other The validator to move from @@ -197,6 +201,42 @@ struct Validator { Validator(AlreadyValidated, T&& _value) : value_(std::move(_value)) {} + /// Returns the validated default value. If it is invalid, this throws, + /// unless the parser defers its validation, see internal::DeferredDefaults. + static T valid_or_deferred() { + if constexpr (std::is_copy_constructible_v) { + auto val = T(); + auto res = ValidationType::validate(val); + if (!res) [[unlikely]] { + return deferred_or_throw(res.error(), std::move(val)); + } + return *std::move(res); + } else { + const auto saved_num = internal::DeferredDefaults::current_num(); + auto res = ValidationType::validate(T()); + if (!res) [[unlikely]] { + return deferred_or_throw_reconstruct(res.error(), saved_num); + } + return *std::move(res); + } + } + + RFL_COLD static T deferred_or_throw(const Error& _err, T&& _val) { + if (!internal::DeferredDefaults::defer(_err)) { + Result(error(_err)).value(); + } + return std::move(_val); + } + + RFL_COLD static T deferred_or_throw_reconstruct(const Error& _err, + size_t _saved_num) { + internal::DeferredDefaults::reset_num(_saved_num); + if (!internal::DeferredDefaults::defer(_err)) { + Result(error(_err)).value(); + } + return T(); + } + /// Wraps the result of a validation without throwing if it failed. static Result wrap_validated(Result&& _res) { return std::move(_res).transform( diff --git a/include/rfl/internal/DeferredDefaults.hpp b/include/rfl/internal/DeferredDefaults.hpp new file mode 100644 index 000000000..2fd201d5c --- /dev/null +++ b/include/rfl/internal/DeferredDefaults.hpp @@ -0,0 +1,87 @@ +#ifndef RFL_INTERNAL_DEFERREDDEFAULTS_HPP_ +#define RFL_INTERNAL_DEFERREDDEFAULTS_HPP_ + +#include +#include + +#include "../Result.hpp" +#include "../common.hpp" + +namespace rfl::internal { + +/// Parser::read_struct_with_default default-constructs the struct it reads +/// into, to obtain the default values of the fields that are missing from the +/// input. If an rfl::Validator in it has an invalid default value, that +/// throws, even if the input contains the field. The parser then constructs +/// the struct again while a DeferredDefaults is active on this thread. Every +/// Validator that is default-constructed on this thread in the meantime keeps +/// its invalid default value instead of throwing, and is counted here. The +/// parser then checks with parsing::check_default_value that the struct holds +/// at least that many invalid Validators, so that none has been used up on +/// the way, and that none of them is in a field that is missing from the +/// input. +class DeferredDefaults { + public: + /// Makes _deferred the active DeferredDefaults of this thread for as long + /// as the Scope lives. A null _deferred turns deferring off. + class Scope { + public: + explicit Scope(DeferredDefaults* _deferred) noexcept : prev_(active()) { + active() = _deferred; + } + + ~Scope() { active() = prev_; } + + Scope(const Scope&) = delete; + Scope& operator=(const Scope&) = delete; + + private: + DeferredDefaults* prev_; + }; + + /// Called by rfl::Validator() if its default value is invalid. Returns + /// whether the Validator keeps the value instead of throwing. + static bool defer(const Error& _error) { + auto* deferred = active(); + if (!deferred) { + return false; + } + if (!deferred->first_error_) { + deferred->first_error_ = _error; + } + ++deferred->num_; + return true; + } + + /// Returns the current deferred count on this thread, or 0 when inactive. + static size_t current_num() noexcept { + auto* deferred = active(); + return deferred ? deferred->num_ : 0; + } + + /// Resets the deferred count on this thread to _num when active. + static void reset_num(size_t _num) noexcept { + if (auto* deferred = active()) { + deferred->num_ = _num; + } + } + + /// The number of Validators that have kept an invalid default value. + size_t num() const noexcept { return num_; } + + /// Why the first of them is invalid. + const std::optional& first_error() const noexcept { + return first_error_; + } + + private: + RFL_API static DeferredDefaults*& active() noexcept; + + private: + size_t num_ = 0; + std::optional first_error_; +}; + +} // namespace rfl::internal + +#endif diff --git a/include/rfl/parsing/NamedTupleParser.hpp b/include/rfl/parsing/NamedTupleParser.hpp index 90b062edb..1eac6c113 100644 --- a/include/rfl/parsing/NamedTupleParser.hpp +++ b/include/rfl/parsing/NamedTupleParser.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -187,23 +188,24 @@ struct NamedTupleParser { * @param _r The reader to use. * @param _var The input variable to read from. * @param _view The view to read into. + * @param _found If not null, receives which fields the input contains. * @return An optional error. */ static std::optional read_view_with_default( - const R& _r, const InputVarType& _var, - NamedTuple* _view) noexcept { + const R& _r, const InputVarType& _var, NamedTuple* _view, + std::array* _found = nullptr) noexcept { if constexpr (_no_field_names) { auto arr = _r.to_array(_var); if (!arr) [[unlikely]] { return arr.error(); } - return read_object_or_array_with_default(_r, *arr, _view); + return read_object_or_array_with_default(_r, *arr, _view, _found); } else { auto obj = _r.to_object(_var); if (!obj) [[unlikely]] { return obj.error(); } - return read_object_or_array_with_default(_r, *obj, _view); + return read_object_or_array_with_default(_r, *obj, _view, _found); } } @@ -233,14 +235,19 @@ struct NamedTupleParser { * @brief Generates the schema for the named tuple. * * @param _definitions The map of definitions to add to. + * @param _view A default-constructed instance to take the default values of + * rfl::DefaultVal fields from. + * @param _no_default The fields of _view whose default value is invalid. + * They get no default value and are required. * @return The schema type. */ template static schema::Type to_schema( std::map* _definitions, - View* _view = nullptr) noexcept { + View* _view = nullptr, + const std::array* _no_default = nullptr) noexcept { SchemaType schema; - build_schema(_definitions, &schema, _view, + build_schema(_definitions, &schema, _view, _no_default, std::make_integer_sequence()); return schema::Type{schema}; } @@ -287,13 +294,18 @@ struct NamedTupleParser { static void add_field_to_schema( std::map* _definitions, SchemaType* _schema, - View* _view) noexcept { + View* _view, + const std::array* _no_default) noexcept { using F = internal::nth_element_t<_i, FieldTypes...>; using U = std::remove_cvref_t; if constexpr (!internal::is_skip_v && !internal::is_extra_fields_v) { // Add default value here auto s = Parser::to_schema(_definitions); - if constexpr (!std::is_same_v && internal::CanEmitDefault) { + if (_no_default && std::get<_i>(*_no_default)) [[unlikely]] { + // Its default value is invalid, so reading requires the field. + remove_default_val(&s); + } else if constexpr (!std::is_same_v && + internal::CanEmitDefault) { s.variant_.visit([&](auto& value) { if constexpr (std::is_same_v, schema::Type::DefaultVal>) { @@ -317,12 +329,34 @@ struct NamedTupleParser { (add_field_to_object<_is>(_w, _tup, _ptr), ...); } + /// Replaces the DefaultVal node in _schema by the type it holds, which + /// makes the field required. Description and Deprecated wrap the DefaultVal + /// node in one of their own. + static void remove_default_val(schema::Type* _schema) { + std::optional inner; + _schema->variant_.visit([&](auto& _s) { + using S = std::remove_cvref_t; + if constexpr (std::is_same_v) { + inner = *_s.type_; + } else if constexpr (std::is_same_v || + std::is_same_v) { + remove_default_val(&*_s.type_); + } + }); + if (inner) { + *_schema = std::move(*inner); + } + } + template static void build_schema(std::map* _definitions, SchemaType* _schema, View* _view, + const std::array* _no_default, std::integer_sequence) noexcept { - (add_field_to_schema(_definitions, _schema, _view), ...); + (add_field_to_schema(_definitions, _schema, _view, + _no_default), + ...); if constexpr (NamedTupleType::pos_extra_fields() != -1) { using F = internal::nth_element_t read_object_or_array_with_default( const R& _r, const InputObjectOrArrayType& _obj_or_arr, - NamedTupleType* _view) noexcept { + NamedTupleType* _view, std::array* _found) noexcept { FieldErrors errors; const auto reader = ViewReaderWithDefaultType(&_r, _view, &errors); if constexpr (_no_field_names) { @@ -514,6 +548,9 @@ struct NamedTupleParser { return err; } } + if (_found) [[unlikely]] { + *_found = reader.found(); + } if constexpr (internal::has_default_val_v && !internal::default_if_missing_v) { if (!should_stop_on_field_error(errors)) { diff --git a/include/rfl/parsing/Parser_default.hpp b/include/rfl/parsing/Parser_default.hpp index 80d98b31a..e261b8168 100644 --- a/include/rfl/parsing/Parser_default.hpp +++ b/include/rfl/parsing/Parser_default.hpp @@ -1,12 +1,19 @@ #ifndef RFL_PARSING_PARSER_DEFAULT_HPP_ #define RFL_PARSING_PARSER_DEFAULT_HPP_ +#include +#include +#include #include +#include #include +#include #include +#include #include "../Result.hpp" #include "../always_false.hpp" +#include "../internal/DeferredDefaults.hpp" #include "../internal/HasFromReflectionMethod.hpp" #include "../internal/always_inline.hpp" #include "../internal/cold.hpp" @@ -25,6 +32,8 @@ #include "../internal/to_ptr_named_tuple.hpp" #include "../to_view.hpp" #include "AreReaderAndWriter.hpp" +#include "check_default_value.hpp" +#include "FieldErrors.hpp" #include "MapParser.hpp" #include "Parent.hpp" #include "ParserArray.hpp" @@ -72,6 +81,8 @@ #include "schemaful/IsSchemafulReader.hpp" #include "schemaful/IsSchemafulWriter.hpp" #include "tagged_union_wrapper_no_ptr.hpp" +#include "to_single_error_message.hpp" +#include "wrap_error.hpp" namespace rfl::parsing { @@ -675,12 +686,8 @@ struct Parser { "A struct with rfl::DefaultVal fields must be " "default-constructible, because the default values " "come from a default-constructed instance."); - auto defaults = DefaultsType{}; - auto view = ProcessorsType::template process( - to_view(defaults)); (*_definitions)[name] = - Parser::to_schema( - _definitions, &view); + schema_with_defaults(_definitions); } else { (*_definitions)[name] = Parser::to_schema( @@ -691,6 +698,42 @@ struct Parser { return Type{Type::Reference{name}}; } + /// The schema of U with the default values of its rfl::DefaultVal fields, + /// which are taken from a default-constructed instance. If constructing it + /// throws because the default value of a Validator is invalid, it is + /// constructed again with such Validators deferred, as read_struct_with_default + /// does (see internal::DeferredDefaults). The fields whose default value is + /// invalid, or cannot be checked, get no default value and are required, as + /// reading requires them. If an invalid default value has been used up while + /// constructing, the schema has no default values at all. Anything else that + /// throws is passed on. + template + static schema::Type schema_with_defaults( + std::map* _definitions) { + internal::DeferredDefaults deferred; + auto defaults = default_construct(&deferred); + auto view = ProcessorsType::template process(to_view(defaults)); + using ViewType = decltype(view); + if (deferred.num() == 0) [[likely]] { + return Parser::to_schema( + _definitions, &view); + } + if (!holds_all_deferred(deferred, view)) { + auto all_no_default = std::array{}; + all_no_default.fill(true); + return Parser::to_schema( + _definitions, &view, &all_no_default); + } + auto no_default = std::array{}; + for_each_field(view, [&](size_t _i, const auto& _field) { + auto check = DefaultValueCheck{}; + check_default_value(_field, &check); + no_default[_i] = check.invalid || check.unchecked; + }); + return Parser::to_schema( + _definitions, &view, &no_default); + } + template static schema::Type make_validated( std::map* _definitions) { @@ -736,16 +779,37 @@ struct Parser { "A struct with rfl::DefaultVal fields must be " "default-constructible, because the default values come " "from a default-constructed instance."); + internal::DeferredDefaults deferred; try { // This might fail, for instance if the default value of a field does not - // satisfy its validator. The outer handler is for this line only. - auto t = T{}; + // satisfy its validator and validation cannot be deferred. The outer + // handler is for this line only. + auto t = default_construct(&deferred); try { auto view = ProcessorsType::template process(to_view(t)); using ViewType = decltype(view); + const bool any_deferred = deferred.num() != 0; + if (any_deferred && !holds_all_deferred(deferred, view)) [[unlikely]] { + return error(default_construction_error( + deferred.first_error()->what().c_str())); + } + auto found = std::array{}; + bool extra_was_empty = false; + if constexpr (ViewType::pos_extra_fields() != -1) { + extra_was_empty = + rfl::get(view)->empty(); + } auto err = Parser::read_view_with_default( - _r, _var, &view); + _r, _var, &view, any_deferred ? &found : nullptr); + if (!err && any_deferred) [[unlikely]] { + if constexpr (ViewType::pos_extra_fields() != -1) { + if (extra_was_empty) { + found[ViewType::pos_extra_fields()] = true; + } + } + err = check_deferred_defaults(view, found); + } if (err) [[unlikely]] { return error(std::move(*err)); } @@ -758,6 +822,114 @@ struct Parser { } } + /// Returns U{}. If that throws, for instance because the default value of a + /// Validator is invalid, we try again with such Validators deferred to + /// _deferred (see internal::DeferredDefaults). + template + static U default_construct(internal::DeferredDefaults* _deferred) { + // Whether that has been necessary before, so that the first try would + // throw again. This is only a shortcut, so relaxed is enough. + static std::atomic defer_right_away = false; + if (!defer_right_away.load(std::memory_order_relaxed)) { + try { + // A read inside a default member initializer of a struct that we are + // constructing with deferred Validators must not defer its own. + const auto not_deferred = internal::DeferredDefaults::Scope(nullptr); + return U{}; + } catch (std::exception&) { + } + } + struct SetDeferRightAway { + const internal::DeferredDefaults* deferred; + std::atomic* flag; + int uncaught; + ~SetDeferRightAway() { + if (std::uncaught_exceptions() == uncaught && deferred->num() != 0) { + flag->store(true, std::memory_order_relaxed); + } + } + }; + const auto scope = internal::DeferredDefaults::Scope(_deferred); + const auto on_exit = SetDeferRightAway{_deferred, &defer_right_away, + std::uncaught_exceptions()}; + return U{}; + } + + /// Whether the struct still holds as many invalid Validators as have been + /// deferred. If one of them has been used up while constructing it, for + /// instance a temporary in a default member initializer, or is inside a + /// type that check_default_value cannot look into, we cannot tell where its + /// value went. Copies can make up for a used-up one, though. + template + static bool holds_all_deferred(const internal::DeferredDefaults& _deferred, + const ViewType& _view) { + auto check = DefaultValueCheck{.stop_early = false}; + for_each_field(_view, [&](size_t, const auto& _field) { + check_default_value(_field, &check); + }); + return check.num_invalid >= _deferred.num(); + } + + /// Returns an error if the default value of a field that is missing from + /// the input holds a Validator whose value is invalid, or a type that + /// check_default_value cannot look into. The fields that the input contains + /// have been replaced by values that were read as a whole (see + /// ViewReaderWithDefault and ViewReaderWithDefaultAndStrippedFieldNames). + template + static std::optional check_deferred_defaults( + const ViewType& _view, const std::array& _found) { + constexpr auto names = field_names( + std::make_index_sequence()); + FieldErrors errors; + for_each_field(_view, [&](size_t _i, const auto& _field) { + if (_found[_i] || should_stop_on_field_error(errors)) { + return; + } + auto check = DefaultValueCheck{}; + check_default_value(_field, &check); + if (!check.invalid && !check.unchecked) [[likely]] { + return; + } + const auto name = std::string(names[_i]); + const auto why = check.invalid + ? "is invalid: " + check.invalid->what() + : "could not be checked: " + *check.unchecked; + errors.emplace_back(make_nested_error( + name, "Field named '" + name + "' not found, and its default value " + + why)); + }); + if constexpr (internal::fail_fast_v) { + return std::move(errors).error(); + } else { + if (errors.empty()) { + return std::nullopt; + } + return to_single_error(std::move(errors)); + } + } + + template + static constexpr auto field_names(std::index_sequence<_is...>) { + return std::array{ + tuple_element_t<_is, typename ViewType::Fields>::name()...}; + } + + /// Calls _f(i, field) for every field i of _view that points into the + /// struct. + template + static void for_each_field(const ViewType& _view, const F& _f) { + [&](std::index_sequence<_is...>) { + const auto call = [&](std::integral_constant) { + using Type = + typename tuple_element_t<_i, typename ViewType::Fields>::Type; + if constexpr (std::is_pointer_v) { + _f(_i, *rfl::get<_i>(_view)); + } + }; + (call(std::integral_constant{}), ...); + }(std::make_index_sequence()); + } + /// The error returned by read_struct_with_default when T{} throws. On its /// own, the exception reads as if the input was wrong, so we name the struct /// and explain why it was default-constructed. diff --git a/include/rfl/parsing/ViewReaderWithDefault.hpp b/include/rfl/parsing/ViewReaderWithDefault.hpp index c2e034322..a024021df 100644 --- a/include/rfl/parsing/ViewReaderWithDefault.hpp +++ b/include/rfl/parsing/ViewReaderWithDefault.hpp @@ -88,6 +88,8 @@ class ViewReaderWithDefault { _errors->emplace_back(wrap_field_error(name, std::move(res).error())); return; } + // Parser::check_deferred_defaults relies on the field being replaced as + // a whole. if constexpr (std::is_pointer_v) { move_to(rfl::get(*_view), &(*res)); } else { diff --git a/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp b/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp index 72512ff66..5547de0bd 100644 --- a/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp +++ b/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp @@ -91,6 +91,8 @@ class ViewReaderWithDefaultAndStrippedFieldNames { _errors->emplace_back(wrap_field_error(name, std::move(res).error())); return; } + // Parser::check_deferred_defaults relies on the field being replaced as + // a whole. if constexpr (std::is_pointer_v) { move_to(rfl::get(*_view), &(*res)); } else { diff --git a/include/rfl/parsing/check_default_value.hpp b/include/rfl/parsing/check_default_value.hpp new file mode 100644 index 000000000..447425e77 --- /dev/null +++ b/include/rfl/parsing/check_default_value.hpp @@ -0,0 +1,266 @@ +#ifndef RFL_PARSING_CHECK_DEFAULT_VALUE_HPP_ +#define RFL_PARSING_CHECK_DEFAULT_VALUE_HPP_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../Generic.hpp" +#include "../Result.hpp" +#include "../apply.hpp" +#include "../internal/has_reflection_method_v.hpp" +#include "../internal/has_reflection_type_v.hpp" +#include "../internal/has_reflector.hpp" +#include "../internal/is_bitset.hpp" +#include "../internal/is_default_val_v.hpp" +#include "../internal/is_deprecated.hpp" +#include "../internal/is_description.hpp" +#include "../internal/is_field.hpp" +#include "../internal/is_flatten_field.hpp" +#include "../internal/is_literal.hpp" +#include "../internal/is_named_tuple.hpp" +#include "../internal/is_validator.hpp" +#include "../num_fields.hpp" +#include "../to_view.hpp" +#include "../type_name_t.hpp" +#include "ParserAtomic.hpp" +#include "ParserBox.hpp" +#include "ParserCommented.hpp" +#include "ParserDuration.hpp" +#include "ParserExpected.hpp" +#include "ParserFilepath.hpp" +#include "ParserOptional.hpp" +#include "ParserPair.hpp" +#include "ParserPositional.hpp" +#include "ParserRef.hpp" +#include "ParserReferenceWrapper.hpp" +#include "ParserRename.hpp" +#include "ParserResult.hpp" +#include "ParserRflArray.hpp" +#include "ParserRflTuple.hpp" +#include "ParserRflVariant.hpp" +#include "ParserSharedPtr.hpp" +#include "ParserShort.hpp" +#include "ParserSkip.hpp" +#include "ParserTaggedUnion.hpp" +#include "ParserTimePoint.hpp" +#include "ParserTuple.hpp" +#include "ParserUniquePtr.hpp" +#include "ParserVariant.hpp" + +namespace rfl::parsing { + +/// What check_default_value has found in a default value. +struct DefaultValueCheck { + /// Whether to stop at the first invalid Validator or unchecked type. + bool stop_early = true; + + /// The number of Validators whose value is invalid. + size_t num_invalid = 0; + + /// Why the first of them is invalid. + std::optional invalid; + + /// The first type that we could not look into, so that we cannot tell + /// whether it holds an invalid Validator. + std::optional unchecked; + + /// Visited pointees of raw/shared pointers to avoid cycles and double-counts. + std::unordered_set visited; + + bool done() const noexcept { return stop_early && (invalid || unchecked); } +}; + +/// Types that cannot hold an rfl::Validator, so there is no need to look +/// into them. +template +constexpr bool holds_no_validator_v = + !std::is_pointer_v && + (std::is_arithmetic_v || std::is_enum_v || + std::is_null_pointer_v || std::is_member_pointer_v || + std::is_same_v || std::is_same_v || + std::is_same_v || std::is_same_v || + std::is_same_v || std::is_same_v || + is_duration_v || is_time_point_v || is_filepath_v || + internal::is_literal_v || internal::is_bitset_v || + (std::is_empty_v && !internal::is_validator_v)); + +/// Whether U is a reflect-cpp wrapper whose inner value can be inspected via +/// .value() by const reference without copying. +template +constexpr bool is_value_wrapper_v = + !std::is_pointer_v && + (is_skip_v || internal::is_default_val_v || is_rename_v || + internal::is_flatten_field_v || is_short_v || is_positional_v || + internal::is_field_v || internal::is_description_v || + internal::is_deprecated_v || is_commented_v); + +/// Looks for rfl::Validators whose value is invalid inside _value and records +/// them in _check. The parser uses it when a Validator has kept an invalid +/// default value (see internal::DeferredDefaults). It walks into structs, +/// containers, optionals, pointers, variants, tuples, the wrappers of +/// reflect-cpp, and classes with a reflection() method or an rfl::Reflector. +/// Any other type is recorded as unchecked, unless it cannot hold a Validator +/// at all (see holds_no_validator_v). +template +void check_default_value(const T& _value, DefaultValueCheck* _check) { + using U = std::remove_cvref_t; + + if (_check->done()) { + return; + } + + const auto in_each = [_check](const auto&... _vs) { + (check_default_value(_vs, _check), ...); + }; + + const auto in_alternative = [_check](const auto& _v) { + check_default_value(_v, _check); + }; + + if constexpr (std::is_pointer_v) { + using Pointee = std::remove_pointer_t; + if (!_value || std::is_function_v) { + return; + } + if constexpr (std::is_void_v) { + _check->unchecked = type_name_t().str(); + } else if (_check->visited.insert(static_cast(_value)).second) { + check_default_value(*_value, _check); + } + + } else if constexpr (internal::is_validator_v) { + const auto res = U::ValidationType::validate(_value.value()); + if (!res) { + ++_check->num_invalid; + if (!_check->invalid) { + _check->invalid = res.error(); + } + } + check_default_value(_value.value(), _check); + + } else if constexpr (holds_no_validator_v) { + return; + + } else if constexpr (is_optional_v) { + if (_value) { + check_default_value(*_value, _check); + } + + } else if constexpr (is_unique_ptr_v) { + if (_value) { + if constexpr (requires { *_value; } && + !std::is_void_v>) { + check_default_value(*_value, _check); + } else { + _check->unchecked = type_name_t().str(); + } + } + + } else if constexpr (is_shared_ptr_v) { + if (_value) { + if constexpr (requires { *_value; } && + !std::is_void_v>) { + if (_check->visited.insert(static_cast(_value.get())) + .second) { + check_default_value(*_value, _check); + } + } else { + _check->unchecked = type_name_t().str(); + } + } + + } else if constexpr (is_box_v) { + if (_value.get()) { + check_default_value(*_value, _check); + } + + } else if constexpr (is_ref_v) { + if (_value.get() && + _check->visited.insert(static_cast(_value.get())).second) { + check_default_value(*_value, _check); + } + + } else if constexpr (is_result_v || is_expected_v) { + if (_value) { + if constexpr (requires { *_value; } && + !std::is_void_v>) { + check_default_value(*_value, _check); + } + } else if constexpr (requires { _value.error(); }) { + check_default_value(_value.error(), _check); + } + + } else if constexpr (is_reference_wrapper_v) { + check_default_value(_value.get(), _check); + + } else if constexpr (is_atomic_v) { + check_default_value(_value.load(), _check); + + } else if constexpr (is_variant_v) { + if (!_value.valueless_by_exception()) { + std::visit(in_alternative, _value); + } + + } else if constexpr (is_rfl_variant_v) { + _value.visit(in_alternative); + + } else if constexpr (is_tagged_union_v) { + _value.variant().visit(in_alternative); + + } else if constexpr (is_tuple_v || is_pair_v) { + std::apply(in_each, _value); + + } else if constexpr (is_rfl_tuple_v) { + rfl::apply(in_each, _value); + + } else if constexpr (internal::is_named_tuple_v) { + rfl::apply(in_each, _value.values()); + + } else if constexpr (is_rfl_array_v) { + check_default_value(_value.arr_, _check); + + } else if constexpr (is_value_wrapper_v) { + check_default_value(_value.value(), _check); + + } else if constexpr (std::ranges::range) { + using Element = std::remove_cvref_t>; + if constexpr (!holds_no_validator_v) { + for (const auto& element : _value) { + check_default_value(element, _check); + } + } + + } else if constexpr (internal::has_write_reflector && + requires(const U& _u) { Reflector::from(_u); }) { + check_default_value(Reflector::from(_value), _check); + + } else if constexpr (internal::has_reflection_type_v && + internal::has_reflection_method_v && + requires(const U& _u) { _u.reflection(); }) { + check_default_value(_value.reflection(), _check); + + } else if constexpr (!internal::has_read_reflector && + std::is_class_v && std::is_aggregate_v) { + if constexpr (rfl::num_fields <= 128) { + rfl::apply([&](const auto&... _fields) { in_each(*_fields.value()...); }, + to_view(_value).fields()); + } else { + _check->unchecked = type_name_t().str(); + } + + } else { + _check->unchecked = type_name_t().str(); + } +} + +} // namespace rfl::parsing + +#endif diff --git a/src/reflectcpp.cpp b/src/reflectcpp.cpp index ea76c27ca..454de81c8 100644 --- a/src/reflectcpp.cpp +++ b/src/reflectcpp.cpp @@ -32,6 +32,7 @@ SOFTWARE. #include "rfl/Generic.cpp" #include "rfl/env/Writer.cpp" #include "rfl/generic/Writer.cpp" +#include "rfl/internal/DeferredDefaults.cpp" #include "rfl/internal/strings/strings.cpp" #include "rfl/parsing/schema/Type.cpp" #include "rfl/parsing/schemaful/tuple_to_object.cpp" diff --git a/src/rfl/internal/DeferredDefaults.cpp b/src/rfl/internal/DeferredDefaults.cpp new file mode 100644 index 000000000..29aa63f26 --- /dev/null +++ b/src/rfl/internal/DeferredDefaults.cpp @@ -0,0 +1,10 @@ +#include "rfl/internal/DeferredDefaults.hpp" + +namespace rfl::internal { + +DeferredDefaults*& DeferredDefaults::active() noexcept { + static thread_local DeferredDefaults* deferred = nullptr; + return deferred; +} + +} // namespace rfl::internal diff --git a/tests/json/test_default_construct_error.cpp b/tests/json/test_default_construct_error.cpp index 4157eadd9..6ed4667b2 100644 --- a/tests/json/test_default_construct_error.cpp +++ b/tests/json/test_default_construct_error.cpp @@ -11,9 +11,12 @@ namespace test_default_construct_error { using Foods = rfl::Validator, rfl::Size>>; +// Reading defers only the check of a default-constructed Validator (see +// test_default_val_with_validator.cpp), never that of an invalid value that +// is given explicitly. struct Person { rfl::DefaultVal name = "Homer Simpson"; - Foods favorite_foods; + Foods favorite_foods = std::vector(); }; struct PersonWithValidDefault { @@ -23,7 +26,7 @@ struct PersonWithValidDefault { struct Pet { std::string name; - Foods favorite_foods; + Foods favorite_foods = std::vector(); }; struct Family { diff --git a/tests/json/test_default_val_with_validator.cpp b/tests/json/test_default_val_with_validator.cpp new file mode 100644 index 000000000..abd2d13f9 --- /dev/null +++ b/tests/json/test_default_val_with_validator.cpp @@ -0,0 +1,240 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace test_default_val_with_validator { + +using Path = std::vector; + +using Foods = + rfl::Validator, rfl::Size>>; +using Positive = rfl::Validator>; + +const std::string foods_error = + "Size validation failed: Value expected to be greater than or equal to 1, " + "but got 0."; +const std::string positive_error = + "Value expected to be greater than or equal to 1, but got 0."; + +struct Person { + rfl::DefaultVal name = "Homer Simpson"; + Foods favorite_foods; +}; + +TEST(json, test_default_val_with_validator) { + const auto homer = + rfl::json::read(R"({"favorite_foods": ["donuts", "pizza"]})"); + ASSERT_TRUE(homer) << homer.error().what(); + EXPECT_EQ(homer->name.value(), "Homer Simpson"); + EXPECT_EQ(homer->favorite_foods.value(), + std::vector({"donuts", "pizza"})); + + const auto empty = rfl::json::read(R"({})"); + ASSERT_FALSE(empty); + EXPECT_EQ(empty.error().what(), "Field named 'favorite_foods' not found."); + + const auto invalid = rfl::json::read(R"({"favorite_foods": []})"); + ASSERT_FALSE(invalid); + EXPECT_EQ(invalid.error().what(), + "Failed to parse field 'favorite_foods': " + foods_error); + + // Reading does not make the default value valid elsewhere. + EXPECT_THROW((void)Person{}, std::exception); + EXPECT_THROW((void)Foods{}, std::exception); +} + +struct Reversed { + Foods favorite_foods; + rfl::DefaultVal name = "Homer Simpson"; +}; + +TEST(json, test_default_val_with_validator_reversed) { + const auto res = rfl::json::read( + R"({"name": "Bart", "favorite_foods": ["pizza"]})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(rfl::json::write(*res), + R"({"favorite_foods":["pizza"],"name":"Bart"})"); +} + +// A missing field keeps its default value, so with DefaultIfMissing or +// DefaultVal that value must be valid. +struct WithDefaultVal { + rfl::DefaultVal id; + rfl::DefaultVal retries; +}; + +TEST(json, test_default_val_with_validator_missing) { + const auto with_dim = + rfl::json::read(R"({})"); + ASSERT_FALSE(with_dim); + EXPECT_EQ(with_dim.error().what(), + "Field named 'favorite_foods' not found, and its default value is " + "invalid: " + + foods_error); + ASSERT_EQ(with_dim.error().details().size(), 1u); + EXPECT_EQ(with_dim.error().details()[0].path, Path({"favorite_foods"})); + + const auto present = rfl::json::read( + R"({"favorite_foods": ["donuts"]})"); + ASSERT_TRUE(present) << present.error().what(); + EXPECT_EQ(present->name.value(), "Homer Simpson"); + + const auto missing = rfl::json::read(R"({"id": 1})"); + ASSERT_FALSE(missing); + EXPECT_EQ(missing.error().what(), + "Field named 'retries' not found, and its default value is " + "invalid: " + + positive_error); + + const auto res = rfl::json::read(R"({"retries": 3})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(res->retries.value().value(), 3); +} + +struct Family { + rfl::DefaultVal town = "Springfield"; + Person head; + std::vector children; +}; + +TEST(json, test_default_val_with_validator_nested) { + const auto res = rfl::json::read( + R"({"head": {"favorite_foods": ["donuts"]}, + "children": [{"name": "Bart", "favorite_foods": ["pizza"]}]})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(res->head.name.value(), "Homer Simpson"); + EXPECT_EQ(res->children.at(0).favorite_foods->at(0), "pizza"); + + const auto missing = + rfl::json::read(R"({"children": []})"); + ASSERT_FALSE(missing); + EXPECT_EQ(missing.error().what(), + "Field named 'head' not found, and its default value is invalid: " + + foods_error); + + // The nested struct is read as a whole, so its own missing field is + // reported, with the full path. + const auto inner_missing = + rfl::json::read(R"({"head": {}})"); + ASSERT_FALSE(inner_missing); + ASSERT_EQ(inner_missing.error().details().size(), 1u); + EXPECT_EQ(inner_missing.error().details()[0].path, + Path({"head", "favorite_foods"})); +} + +struct Flat { + Positive f2; +}; + +struct Containers { + rfl::DefaultVal id; + std::array array; + std::optional optional; + std::variant variant; + rfl::TaggedUnion<"type", Person, Flat> tagged; + rfl::Flatten flat; + rfl::Rename<"renamed", Positive> rename; + rfl::ExtraFields extra; +}; + +TEST(json, test_default_val_with_validator_containers) { + const auto res = rfl::json::read( + R"({"array": [1, 2], "variant": 3, + "tagged": {"type": "Person", "favorite_foods": ["donuts"]}, + "f2": 4, "renamed": 5, "other": 6})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(rfl::json::write(*res), + R"({"id":0,"array":[1,2],"variant":3,)" + R"("tagged":{"type":"Person","name":"Homer Simpson",)" + R"("favorite_foods":["donuts"]},"f2":4,"renamed":5,"other":6})"); + + const auto missing = rfl::json::read( + R"({"array": [1, 2], "variant": 3, "f2": 4, "renamed": 5})"); + ASSERT_FALSE(missing); + EXPECT_EQ(missing.error().what(), + "Field named 'tagged' not found, and its default value is " + "invalid: " + + foods_error); + + const auto invalid_extra = rfl::json::read( + R"({"array": [1, 2], "variant": 3, + "tagged": {"type": "Flat", "f2": 1}, + "f2": 4, "renamed": 5, "other": 0})"); + ASSERT_FALSE(invalid_extra); + EXPECT_EQ(invalid_extra.error().what(), + "Failed to parse field 'other': " + positive_error); +} + +// rfl::Variant() moves its first alternative into place from a temporary. +struct WithRflVariant { + rfl::DefaultVal id; + rfl::Variant v; +}; + +TEST(json, test_default_val_with_validator_rfl_variant) { + const auto res = rfl::json::read(R"({"v": 3})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(rfl::json::write(*res), R"({"id":0,"v":3})"); + + const auto missing = + rfl::json::read(R"({})"); + ASSERT_FALSE(missing); + EXPECT_EQ(missing.error().what(), + "Field named 'v' not found, and its default value is invalid: " + + positive_error); + + EXPECT_THROW((rfl::Variant()), std::exception); + const auto str = rfl::Variant(); + EXPECT_EQ(str.index(), 0); + EXPECT_EQ(rfl::get(str), ""); +} + +TEST(json, test_default_val_with_validator_no_field_names) { + const auto res = rfl::json::read( + R"(["Bart", ["pizza"]])"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(res->name.value(), "Bart"); + + const auto missing = + rfl::json::read( + R"(["Bart"])"); + ASSERT_FALSE(missing); + EXPECT_EQ(missing.error().what(), + "Field named 'favorite_foods' not found, and its default value is " + "invalid: " + + foods_error); +} + +struct Snake { + rfl::DefaultVal user_id; + Positive retry_count; +}; + +TEST(json, test_default_val_with_validator_processors) { + using Processors = + rfl::Processors>; + const auto res = rfl::json::read( + R"({"type": "Snake", "retryCount": 2})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(res->retry_count.value(), 2); + + const auto missing = + rfl::json::read( + R"({"type": "Snake"})"); + ASSERT_FALSE(missing); + EXPECT_EQ(missing.error().details().at(0).path, Path({"retryCount"})); + + const auto fail_fast = + rfl::json::read(R"({})"); + ASSERT_FALSE(fail_fast); + EXPECT_EQ(fail_fast.error().details().size(), 1u); +} + +} // namespace test_default_val_with_validator diff --git a/tests/json/test_default_val_with_validator_limits.cpp b/tests/json/test_default_val_with_validator_limits.cpp new file mode 100644 index 000000000..ce2d1a438 --- /dev/null +++ b/tests/json/test_default_val_with_validator_limits.cpp @@ -0,0 +1,608 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// What does not change, and the limits of reading structs that contain a +// Validator without a valid default value. +namespace test_default_val_with_validator_limits { + +using Path = std::vector; + +using Foods = + rfl::Validator, rfl::Size>>; +using Positive = rfl::Validator>; + +const std::string positive_error = + "Value expected to be greater than or equal to 1, but got 0."; + +std::string construction_error(const std::string& _type) { + return "Could not default-construct " + "test_default_val_with_validator_limits::" + + _type + + ", which is required because it contains rfl::DefaultVal. Give every " + "field a valid default value. Original error: " + + positive_error; +} + +struct Person { + rfl::DefaultVal name = "Homer Simpson"; + Foods favorite_foods; +}; + +// A default member initializer that catches the exception of a Validator +// still sees it, as long as the struct can be default-constructed. If it +// cannot, every Validator that is default-constructed while the struct is +// constructed again keeps its invalid value, wherever it is, so the fallback +// is not used. Whether the Validator is returned through a temporary does not +// matter. +inline Positive port_or_fallback() { + try { + return Positive(); + } catch (std::exception&) { + return Positive(8080); + } +} + +// Deferring does not touch the copy and move operations of Validator. +static_assert(std::is_trivially_copyable_v); + +struct Fallback { + rfl::DefaultVal host = "localhost"; + Positive port = port_or_fallback(); +}; + +struct FallbackAndInvalid { + Positive port = port_or_fallback(); + Positive retries; +}; + +TEST(json, test_default_val_with_validator_limits_fallback) { + const auto res = rfl::json::read(R"({})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(rfl::json::write(*res), R"({"host":"localhost","port":8080})"); + + const auto res2 = rfl::json::read( + R"({"retries": 2})"); + ASSERT_FALSE(res2); + EXPECT_EQ(std::string(res2.error().what()), + "Field named 'port' not found, and its default value is " + "invalid: " + + positive_error); + + // If the input contains the field, it does not matter. + const auto res3 = rfl::json::read( + R"({"port": 443, "retries": 2})"); + ASSERT_TRUE(res3) << res3.error().what(); + EXPECT_EQ(rfl::json::write(*res3), R"({"port":443,"retries":2})"); +} + +// A read inside a default member initializer has its own invalid default +// values deferred, not those of the struct around it. +struct WithNestedRead { + Person person = + rfl::json::read(R"({"favorite_foods": ["beer"]})").value(); + rfl::DefaultVal retries; +}; + +TEST(json, test_default_val_with_validator_limits_nested_read) { + const auto res = rfl::json::read( + R"({"retries": 2})"); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(res->person.favorite_foods->at(0), "beer"); + EXPECT_EQ(res->retries.value().value(), 2); +} + +// Validators that are default-constructed on other threads still throw. +TEST(json, test_default_val_with_validator_limits_threads) { + std::atomic done = false; + std::atomic constructed = 0; + auto other = std::thread([&] { + while (!done) { + try { + Foods foods; + ++constructed; + } catch (std::exception&) { + } + } + }); + auto readers = std::vector(); + std::atomic failed_reads = 0; + for (int i = 0; i < 2; ++i) { + readers.emplace_back([&] { + for (int j = 0; j < 2000; ++j) { + if (!rfl::json::read(R"({"favorite_foods": ["donuts"]})")) { + ++failed_reads; + } + } + }); + } + for (auto& reader : readers) { + reader.join(); + } + done = true; + other.join(); + EXPECT_EQ(failed_reads, 0); + EXPECT_EQ(constructed, 0); +} + +// The regressions found in the review of an earlier attempt to fix upstream +// issue #700. +struct WithMap { + rfl::DefaultVal x; + std::map m; +}; + +struct NoDefaultConstructor { + NoDefaultConstructor(int _v) : v_(_v) {} + using ReflectionType = int; + int reflection() const { return v_; } + int v_; +}; + +struct NotDefaultConstructible { + NoDefaultConstructor n; + Positive p; +}; + +static_assert(!std::is_default_constructible_v); + +struct ConstValidator { + const rfl::Validator> age; + rfl::DefaultVal name = "Maggie"; +}; + +TEST(json, test_default_val_with_validator_limits_earlier_regressions) { + EXPECT_FALSE(rfl::json::read(R"({"m": {"0": "zero"}})")); + const auto map = rfl::json::read(R"({"m": {"1": "one"}})"); + ASSERT_TRUE(map) << map.error().what(); + EXPECT_EQ(map->m.begin()->first.value(), 1); + + const auto ndc = + rfl::json::read(R"({"n": 3, "p": 4})"); + ASSERT_TRUE(ndc) << ndc.error().what(); + EXPECT_EQ(ndc->p.value(), 4); + + const auto con = rfl::json::read(R"({"age": 8})"); + ASSERT_TRUE(con) << con.error().what(); + EXPECT_EQ(rfl::json::write(*con), R"({"age":8,"name":"Maggie"})"); +} + +// A class that is not reflectable, kept out of reading by rfl::Skip. Reading +// cannot look into it, so with a deferred Validator in the struct, the +// skipped field cannot be checked and reading fails, as it did before. +class Cache { + public: + int hits() const { return hits_; } + + private: + int hits_ = 0; + std::string last_; +}; + +struct WithCache { + rfl::DefaultVal port = 8080; + rfl::Skip cache; + Positive retries; +}; + +struct Schema { + rfl::DefaultVal nullable = false; + std::optional> all_of; + std::optional> properties; + std::unique_ptr items; +}; + +TEST(json, test_default_val_with_validator_limits_other_types) { + const auto cache = rfl::json::read(R"({"retries": 1})"); + ASSERT_FALSE(cache); + EXPECT_EQ(cache.error().what(), + "Field named 'cache' not found, and its default value could not be " + "checked: test_default_val_with_validator_limits::Cache"); + + const auto schema = rfl::json::read( + R"({"properties": {"a": {"nullable": true}}, "items": {}})"); + ASSERT_TRUE(schema) << schema.error().what(); + EXPECT_TRUE(schema->properties->at("a").nullable.value()); + EXPECT_FALSE(schema->items->nullable.value()); +} + +// The JSON schema keeps the valid default values of a struct whose +// Validator has no valid default value. That Validator gets no default value. +struct WithPerson { + int id; + Person person; +}; + +const std::string person_schema = + R"("test_default_val_with_validator_limits__Person":{"type":"object","properties":{"name":{"type":"string","default":"Homer Simpson"},"favorite_foods":{"type":"array","items":{"type":"string"},"minItems":1}},"required":["favorite_foods"]})"; + +TEST(json, test_default_val_with_validator_limits_to_schema) { + EXPECT_EQ( + rfl::json::to_schema(), + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_default_val_with_validator_limits__Person","$defs":{)" + + person_schema + "}}"); +} + +TEST(json, test_default_val_with_validator_limits_to_schema_nested) { + EXPECT_EQ( + rfl::json::to_schema(), + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_default_val_with_validator_limits__WithPerson","$defs":{)" + + person_schema + + R"(,"test_default_val_with_validator_limits__WithPerson":{"type":"object","properties":{"id":{"type":"integer"},"person":{"$ref":"#/$defs/test_default_val_with_validator_limits__Person"}},"required":["id","person"]}}})"); +} + +// Two Validators without a valid default value, one rfl::DefaultVal with a +// valid one, and one rfl::DefaultVal whose default value is invalid. That one +// gets no default value and is required, as reading requires it. +struct Server { + rfl::DefaultVal retries = 3; + Positive port; + rfl::Validator>> host; + rfl::DefaultVal threads; +}; + +TEST(json, test_default_val_with_validator_limits_to_schema_several) { + EXPECT_EQ( + rfl::json::to_schema(), + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_default_val_with_validator_limits__Server","$defs":{"test_default_val_with_validator_limits__Server":{"type":"object","properties":{"retries":{"type":"integer","default":3},"port":{"minimum":1,"type":"integer"},"host":{"type":"string","minLength":1},"threads":{"minimum":1,"type":"integer"}},"required":["port","host","threads"]}}})"); + + const auto server = + rfl::json::read(R"({"port":80,"host":"h","threads":2})"); + ASSERT_TRUE(server) << server.error().what(); + EXPECT_EQ(server.value().retries.value(), 3); + EXPECT_FALSE(rfl::json::read(R"({"port":80,"host":"h"})")); +} + +// [[no_unique_address]] can place a field in the tail padding of another. +struct InTailPadding { + rfl::Validator> a; + int8_t c = 0; +}; + +struct SharesPadding { + [[no_unique_address]] InTailPadding inner; + rfl::DefaultVal p; +}; + +TEST(json, test_default_val_with_validator_limits_no_unique_address) { + const auto res = + rfl::json::read(R"({"inner": {"a": 1, "c": 2}})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().details().at(0).path, Path({"p"})); + + const auto res2 = + rfl::json::read(R"({"inner": {"a": 1, "c": 2}, "p": 3})"); + ASSERT_TRUE(res2) << res2.error().what(); + EXPECT_EQ(res2->p.value().value(), 3); +} + +// A field in the input replaces the default value as a whole. Should a field +// ever be read into its default value instead, the invalid default of p would +// survive here. +struct Inner { + Positive p; + rfl::DefaultVal q; +}; + +struct Outer { + rfl::DefaultVal x; + Inner inner; +}; + +TEST(json, test_default_val_with_validator_limits_found_fields_are_replaced) { + const auto res = + rfl::json::read(R"({"inner": {"q": 1}})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().details().at(0).path, Path({"inner", "p"})); +} + +// A Validator that is default-constructed elsewhere while the struct is +// constructed, such as a temporary or on the heap, keeps its invalid value as +// well. The fields that hold it must then be in the input. +struct OnTheHeap { + rfl::DefaultVal x; + std::vector v = std::vector(1); +}; + +struct FromTemporary { + rfl::DefaultVal x; + rfl::DefaultVal p = Positive(); +}; + +struct InRflTuple { + rfl::DefaultVal x; + rfl::Tuple t; +}; + +TEST(json, test_default_val_with_validator_limits_not_in_place) { + const auto heap = rfl::json::read(R"({"v": [2]})"); + ASSERT_TRUE(heap) << heap.error().what(); + EXPECT_EQ(heap->v.at(0).value(), 2); + const auto heap_missing = + rfl::json::read(R"({"x": 1})"); + ASSERT_FALSE(heap_missing); + EXPECT_EQ(heap_missing.error().what(), + "Field named 'v' not found, and its default value is invalid: " + + positive_error); + + const auto temp = rfl::json::read(R"({"p": 2})"); + ASSERT_TRUE(temp) << temp.error().what(); + EXPECT_EQ(temp->p.value().value(), 2); + EXPECT_FALSE(rfl::json::read(R"({"x": 1})")); + + const auto tuple = rfl::json::read(R"({"t": [2, 3]})"); + ASSERT_TRUE(tuple) << tuple.error().what(); + EXPECT_EQ(rfl::json::write(*tuple), R"({"x":0,"t":[2,3]})"); + EXPECT_FALSE(rfl::json::read(R"({"x": 1})")); +} + +// A field that is never read keeps its default value. +struct SkipsValidator { + rfl::DefaultVal x; + rfl::Skip s; +}; + +TEST(json, test_default_val_with_validator_limits_skip) { + const auto res = rfl::json::read(R"({"x": 1})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Field named 's' not found, and its default value is invalid: " + + positive_error); + + const auto supplied = rfl::json::read(R"({"x": 1, "s": 5})"); + ASSERT_FALSE(supplied); +} + +// A copy of a Validator whose default value is invalid is invalid as well, +// so it must be in the input like the original, wherever it is. +struct Copies { + Positive a; + rfl::DefaultVal b = a; +}; + +struct CopiesToHeap { + rfl::DefaultVal x; + Positive a; + std::vector v = {a}; +}; + +TEST(json, test_default_val_with_validator_limits_copies) { + EXPECT_THROW((void)Copies{}, std::exception); + const auto res = rfl::json::read(R"({"a": 5})"); + ASSERT_FALSE(res); + EXPECT_EQ(res.error().what(), + "Field named 'b' not found, and its default value is invalid: " + + positive_error); + + const auto res2 = rfl::json::read(R"({"a": 5, "b": 6})"); + ASSERT_TRUE(res2) << res2.error().what(); + EXPECT_EQ(res2->b.value().value(), 6); + + const auto heap = rfl::json::read(R"({"a": 5, "v": [6]})"); + ASSERT_TRUE(heap) << heap.error().what(); + EXPECT_EQ(heap->v.at(0).value(), 6); + const auto heap_missing = + rfl::json::read(R"({"a": 5})"); + ASSERT_FALSE(heap_missing); + EXPECT_EQ(heap_missing.error().what(), + "Field named 'v' not found, and its default value is invalid: " + + positive_error); +} + +// An invalid default value that a default member initializer has used up, +// such as a temporary, is not in the struct any more, so reading cannot check +// it and fails as before. +struct FromTemporaryValue { + rfl::DefaultVal x; + int port = Positive().value(); +}; + +// The same holds for an invalid default value inside a class that reading +// cannot look into. +class Opaque { + public: + using ReflectionType = int; + Opaque() = default; + explicit Opaque(int _v) : p_(_v) {} + int get() const { return p_.value(); } + + private: + Positive p_; +}; + +struct WithOpaque { + rfl::DefaultVal x; + rfl::DefaultVal o; +}; + +// A missing field of such a class cannot be checked either, even if it holds +// no Validator. +class Port { + public: + using ReflectionType = int; + Port() = default; + explicit Port(int _v) : v_(_v) {} + int get() const { return v_; } + + private: + int v_ = 80; +}; + +struct WithPort { + Positive retries; + rfl::DefaultVal port; +}; + +TEST(json, test_default_val_with_validator_limits_unchecked) { + EXPECT_THROW((void)FromTemporaryValue{}, std::exception); + const auto temp = rfl::json::read( + R"({"x": 1, "port": 2})"); + ASSERT_FALSE(temp); + EXPECT_EQ(temp.error().what(), construction_error("FromTemporaryValue")); + + EXPECT_THROW((void)WithOpaque{}, std::exception); + const auto opaque = rfl::json::read(R"({"x": 1, "o": 3})"); + ASSERT_FALSE(opaque); + EXPECT_EQ(opaque.error().what(), construction_error("WithOpaque")); + + const auto port = rfl::json::read(R"({"retries": 1})"); + ASSERT_FALSE(port); + EXPECT_EQ(port.error().what(), + "Field named 'port' not found, and its default value could not be " + "checked: test_default_val_with_validator_limits::Port"); + const auto port_present = + rfl::json::read(R"({"retries": 1, "port": 8080})"); + ASSERT_TRUE(port_present) << port_present.error().what(); + EXPECT_EQ(port_present->port.value().get(), 8080); +} + +// The gaps: a default member initializer that reads a Validator gets its +// invalid value, and a copy of an invalid Validator makes up for a used-up +// temporary, which then goes unnoticed. +struct ReadsValidator { + Positive a; + int b = a.value() * 2; +}; + +struct CopyAndTemporary { + Positive a; + rfl::DefaultVal b = a; + int port = Positive().value(); +}; + +TEST(json, test_default_val_with_validator_limits_gaps) { + const auto reads = + rfl::json::read(R"({"a": 5})"); + ASSERT_TRUE(reads) << reads.error().what(); + EXPECT_EQ(reads->b, 0); + + const auto copy = rfl::json::read( + R"({"a": 5, "b": 6})"); + ASSERT_TRUE(copy) << copy.error().what(); + EXPECT_EQ(copy->port, 0); +} + +struct NonCopyableSkip { + rfl::DefaultVal x = 1; + rfl::Skip> handle; + rfl::Skip ptr = nullptr; + rfl::Skip ctx = nullptr; +}; + +struct WithRawPositivePtr { + rfl::DefaultVal x = 1; + Positive* ptr = nullptr; +}; + +struct NonMovableSchema { + rfl::DefaultVal x = 1; + std::atomic counter{0}; +}; + +struct NestedInner { + Positive x; +}; + +struct NonZeroNestedInner { + static rfl::Result validate(NestedInner _i) { + return _i.x.value() > 0 ? rfl::Result(_i) + : rfl::error("bad inner"); + } +}; + +struct NestedValidatorStruct { + rfl::DefaultVal id = 0; + rfl::Validator v; +}; + +struct WithExtraPort { + rfl::DefaultVal x = 1; + Positive p; + rfl::ExtraFields extra; +}; + +struct CycleNode { + std::shared_ptr next; +}; + +inline std::shared_ptr make_cycle() { + auto a = std::make_shared(); + auto b = std::make_shared(); + a->next = b; + b->next = a; + return a; +} + +struct SharedCycleNode { + rfl::DefaultVal id = 0; + Positive p; + rfl::Skip> cycle = make_cycle(); +}; + +struct SharedAlias { + rfl::DefaultVal x = 0; + std::shared_ptr a = std::make_shared(); + std::shared_ptr b = a; + int port = Positive().value(); +}; + +TEST(json, test_default_val_with_validator_limits_regressions) { + // Non-copyable rfl::Skip, raw Positive*, and nullptr void* must compile. + const auto ncs = rfl::json::read(R"({})"); + ASSERT_TRUE(ncs) << ncs.error().what(); + EXPECT_EQ(ncs->x.value(), 1); + + const auto raw_ptr = + rfl::json::read(R"({"ptr": null})"); + ASSERT_TRUE(raw_ptr) << raw_ptr.error().what(); + EXPECT_EQ(raw_ptr->ptr, nullptr); + + // Non-movable struct with DefaultVal must still generate a schema. + const auto nms = rfl::json::to_schema(); + EXPECT_NE(nms.find(R"("default":1)"), std::string::npos) << nms; + + // Nested Validator must not double-count inner deferred Validators. + const auto nested = + rfl::json::read(R"({"v": {"x": 5}})"); + ASSERT_TRUE(nested) << nested.error().what(); + EXPECT_EQ(nested->v.value().x.value(), 5); + EXPECT_FALSE( + (rfl::json::read(R"({})"))); + + // Input-parsed ExtraFields elements must not be rejected as unchecked defaults. + const auto extra = rfl::json::read(R"({"p": 1, "k": 80})"); + ASSERT_TRUE(extra) << extra.error().what(); + EXPECT_EQ(extra->extra.at("k").get(), 80); + + // Cyclic shared_ptr graph must not stack-overflow. + const auto cyc = rfl::json::read(R"({"p": 7})"); + ASSERT_TRUE(cyc) << cyc.error().what(); + EXPECT_EQ(cyc->p.value(), 7); + // Break cycle before destruction to avoid leaking under ASan/LSan. + cyc->cycle.value()->next.reset(); + + // Two shared_ptrs to one invalid Validator must not double-count to mask a + // discarded temporary. + const auto alias = + rfl::json::read(R"({"a": 1, "b": 2, "port": 3})"); + ASSERT_FALSE(alias); + + // When !holds_all_deferred, to_schema must still mark DefaultVal fields + // required (no DefaultVal wrapper left behind). + const auto temp_schema = rfl::json::to_schema(); + EXPECT_NE(temp_schema.find(R"("required":["x","port"])"), std::string::npos) + << temp_schema; +} + +} // namespace test_default_val_with_validator_limits