From 17bfb3598245d79c4d61ba386f72f085d9f95efc Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sun, 11 Jan 2026 16:15:15 +0100 Subject: [PATCH] Made sure TaggedUnion can be used in combination with rfl::NoExtraFields; fixes #567 --- include/rfl/parsing/Parser_tagged_union.hpp | 19 +++++------- ...test_tagged_union_with_no_extra_fields.cpp | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 tests/json/test_tagged_union_with_no_extra_fields.cpp diff --git a/include/rfl/parsing/Parser_tagged_union.hpp b/include/rfl/parsing/Parser_tagged_union.hpp index 8821dc5c..7fd5c326 100644 --- a/include/rfl/parsing/Parser_tagged_union.hpp +++ b/include/rfl/parsing/Parser_tagged_union.hpp @@ -154,18 +154,13 @@ struct Parser, return Error(stream.str()); }; - if constexpr (no_field_names_) { - using T = tagged_union_wrapper_no_ptr_t), AlternativeType>>; - *_result = Parser::read(_r, _var) - .transform(get_fields) - .transform(to_tagged_union) - .transform_error(embellish_error); - } else { - *_result = Parser::read(_r, _var) - .transform(to_tagged_union) - .transform_error(embellish_error); - } + using T = tagged_union_wrapper_no_ptr_t), AlternativeType>>; + + *_result = Parser::read(_r, _var) + .transform(get_fields) + .transform(to_tagged_union) + .transform_error(embellish_error); *_match_found = true; } diff --git a/tests/json/test_tagged_union_with_no_extra_fields.cpp b/tests/json/test_tagged_union_with_no_extra_fields.cpp new file mode 100644 index 00000000..e2b2bc5c --- /dev/null +++ b/tests/json/test_tagged_union_with_no_extra_fields.cpp @@ -0,0 +1,29 @@ +#include +#include + +#include "write_and_read.hpp" + +namespace test_tagged_union_with_no_extra_fields { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; + +TEST(json, test_tagged_union_with_no_extra_fields) { + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read( + r, R"({"shape":"Rectangle","height":10.0,"width":5.0})"); +} +} // namespace test_tagged_union_with_no_extra_fields