From 6599153387a103398f67b649b16c6f6a163eca5e Mon Sep 17 00:00:00 2001 From: Denis Popov Date: Sun, 29 Nov 2026 12:11:42 -0800 Subject: [PATCH] fix(yaml): return error on non-scalar mapping keys instead of silently skipping Parent: #801 (`yaml::Reader::read_object` recursive alias guard). `rfl::yaml::Reader::read_object` previously wrapped `p.first.as()` and `_object_reader.read(...)` in `catch (std::exception& e) { continue; }`, silently dropping mapping entries whose keys were sequences, maps, or aliases to collections (e.g. `[1, 2]: foo` or `{a: 1}: bar`). This caused silent data loss in maps / `rfl::Generic` and silently bypassed `rfl::NoExtraFields`. Validate the mapping key inside the `try` block and return an explicit `rfl::Error` ("YAML mapping key must be a scalar.") whenever a key is neither a scalar nor YAML null, propagating any `yaml-cpp` exception as an `rfl::Error` instead of skipping. Move `_object_reader.read(...)` outside the `try` block (matching all other structured readers) and pass a zero-copy `std::string_view` directly from `p.first.Scalar()` (or `"null"` for YAML null keys `~` / `null` / empty `: val`). Scalar aliases and merge keys (`<<`) continue to work identically. Addresses downstream workaround in `openmoq/moqx` (`src/config/YamlDuplicateKeys.cpp:23-111`, `test/config/LoaderTest.cpp:542-650`). --- include/rfl/yaml/Reader.hpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/include/rfl/yaml/Reader.hpp b/include/rfl/yaml/Reader.hpp index 4ab9e62ae..ea210e8a0 100644 --- a/include/rfl/yaml/Reader.hpp +++ b/include/rfl/yaml/Reader.hpp @@ -194,12 +194,19 @@ struct Reader { return Error("Recursive YAML alias detected."); } for (const auto& p : _obj.node_) { + std::string_view key; try { - const auto k = p.first.as(); - _object_reader.read(std::string_view(k), InputVarType(p.second)); - } catch (std::exception& e) { - continue; + if (p.first.IsScalar()) { + key = p.first.Scalar(); + } else if (p.first.IsNull()) { + key = "null"; + } else { + return Error("YAML mapping key must be a scalar."); + } + } catch (const std::exception& e) { + return Error(e.what()); } + _object_reader.read(key, InputVarType(p.second)); } return std::nullopt; }