Skip to content

Add std::monostate support - #754

Draft
cacharle wants to merge 3 commits into
munich-data-engineering:mainfrom
cacharle:add-monostate-support
Draft

cacharle wants to merge 3 commits into
munich-data-engineering:mainfrom
cacharle:add-monostate-support

Conversation

@cacharle

Copy link
Copy Markdown
Contributor

Fix #749

@Altren

Altren commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

@cacharle Is it intended to be draft? Looks complete to me

@cacharle
cacharle force-pushed the add-monostate-support branch from a212f59 to 0adc5cd Compare October 25, 2026 13:59
@Altren

Altren commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

I believe that most or all CI issues are fixed by #759, so try to rebase onto latest main

@clockblocker

Copy link
Copy Markdown
Contributor

ParserMonostate::read fails on boost_serialization, cereal, and yas

struct Order {
  std::variant<std::monostate, std::string, int> price;
};

// Same failure for rfl::boost_serialization and rfl::yas:
const auto bytes = rfl::cereal::write(Order{.price = std::monostate{}});
const auto res = rfl::cereal::read<Order>(bytes);
// Runtime error: "Expected null for std::monostate."

rfl::cereal, rfl::boost_serialization, and rfl::yas are schemaful archive formats (schemaful::IsSchemafulReader<R>) with _all_required = true:

  • On write, add_null_to_union writes only the discriminant index, and add_null_to_object / add_null_to_array / null_as_root write zero payload bytes.
  • On read, read_union reads the discriminant and dispatches to ParserMonostate::read, which checks if (!_r.is_empty(_var)).
  • However, is_empty() always returns false for all three readers:
    • rfl::boost_serialization::Reader::is_empty: hardcoded return false;
    • rfl::yas::Reader::is_empty: hardcoded return false;
    • rfl::cereal::Reader::is_empty: return _var.archive_ == nullptr; (always non-null during read)

Compare with rfl::parsing::schemaful::OptionalReader, which returns the empty state directly without calling _r.is_empty(_var) because the schema/discriminant already identifies the null branch. Guarding the is_empty check with if constexpr (!schemaful::IsSchemafulReader<R>) in ParserMonostate::read would fix all three formats:

static Result<std::monostate> read(const R& _r,
                                   const InputVarType& _var) noexcept {
  if constexpr (!schemaful::IsSchemafulReader<R>) {
    if (!_r.is_empty(_var)) {
      return error("Expected null for std::monostate.");
    }
  }
  return std::monostate{};
}

(Feedback from an AI reviewer.)

@clockblocker

Copy link
Copy Markdown
Contributor

rfl::AddTagsToVariants / rfl::AddNamespacedTagsToVariants mismatches JSON Schema (and null reads)

struct Order {
  std::variant<std::monostate, std::string, int> price;
};

// 1. Write omits `price` -> "{}"
const auto json =
    rfl::json::write<rfl::AddTagsToVariants>(Order{.price = std::monostate{}});

// 2. JSON Schema still puts "price" in "required":
//    "properties":{"price":{"anyOf":[{"type":"object","properties":{"monostate":{"type":"null"}},"required":["monostate"]}, ...]}},"required":["price"]
const auto schema = rfl::json::to_schema<Order, rfl::AddTagsToVariants>();
// -> `json` ("{}") fails validation against `schema`!

// 3. Explicit null also fails to read (expects {"price":{"monostate":null}}):
const auto res = rfl::json::read<Order, rfl::AddTagsToVariants>(R"({"price":null})");

Why this happens:

  • is_required / is_empty don't take ProcessorsType into account, so NamedTupleParser::write treats price as optional and omits it when holding std::monostate.
  • Under rfl::AddTagsToVariants / rfl::AddNamespacedTagsToVariants, ParserVariant::to_schema wraps each alternative in VariantAlternativeWrapper, so the first Type::AnyOf branch is a Type::Object (e.g. {"monostate": null}), not a bare Type::Null.
  • is_optional() in src/rfl/json/to_schema.cpp only checks rfl::holds_alternative<parsing::schema::Type::Null>(_v.types_.front().variant_), which returns false, emitting "required": ["price"].

As a result, the serialized output {} does not validate against the generated JSON Schema, and {"price":null} is rejected on read. Serialization, deserialization, and schema generation should agree for tagged variants (either handling tagged-variant Null wrappers consistently, or not treating tagged monostate variants as omitted optionals).

(Feedback from an AI reviewer.)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for std::monostate

3 participants