Skip to content

fix(yaml): return error on non-scalar mapping keys instead of silently skipping - #219

Open
clockblocker wants to merge 1 commit into
yaml-recursive-alias-guardfrom
queue/fix-yaml-non-scalar-keys
Open

clockblocker wants to merge 1 commit into
yaml-recursive-alias-guardfrom
queue/fix-yaml-non-scalar-keys

Conversation

@clockblocker

@clockblocker clockblocker commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

AI-authored below this line.

Stacked on: #72 (yaml-recursive-alias-guard)

Problem

Reading a YAML mapping caught key-conversion exceptions with continue, silently dropping entries whose keys were sequences, maps, or collection aliases.

That caused silent data loss in maps and rfl::Generic, and let malformed keys bypass rfl::NoExtraFields.

struct Config { std::string name; };

// Before: silently skips "[1, 2]: foo" even under NoExtraFields
rfl::yaml::read<Config, rfl::NoExtraFields>("name: ok\n[1, 2]: foo\n"); // Ok: {name: "ok"}

// After: rejects non-scalar keys with an explicit error (and path when nested)
rfl::yaml::read<Config, rfl::NoExtraFields>("name: ok\n[1, 2]: foo\n");
// Error: YAML mapping key must be a scalar.

Summary

  • Rejects sequence, map, and collection-alias keys with an explicit error while preserving scalar aliases, merge keys (<<), and YAML null keys (null, ~, : val).
  • Passes zero-copy std::string_view keys from Scalar() instead of allocating per key; stacks on PR #801.

Consumer Impact

Consumer Location Pain point solved
openmoq/moqx YamlDuplicateKeys.cpp#L23-L111 Built a custom YAML::EventHandler pre-pass to reject non-scalar keys before rfl::yaml::read.
For Agents

…y skipping

Parent: munich-data-engineering#801 (`yaml::Reader::read_object` recursive alias guard).

`rfl::yaml::Reader::read_object` previously wrapped `p.first.as<std::string>()`
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`).
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.

1 participant