Repository navigation
Description
Part of #138
Popularity: 1/7 corpus consumers (slang-server)
Related issues
- Related fork issues: Map (Lane 4): Schema & Contract Fidelity #88 (
Map (Lane 4): Schema & Contract Fidelity), Prototype JSON Schema extensibility ($defs naming, custom wrapper x-* hooks, integer width bounds, title/examples) #100 (Prototype JSON Schema extensibility), rfl::json::to_schema<T, rfl::DefaultIfMissing>: emit "type" before "default" on Boolean and normalize annotation order #115 (rfl::json::to_schema<T, rfl::DefaultIfMissing>), Triage & Design Notes (Fork-Only): Speculative Schema / Contract APIs #136.
Observed Downstream Workaround (hudson-trading/slang-server @ 1b413a46)
slang-server generates clients/vscode/resources/config.schema.json directly from its Config struct (include/Config.h, which has default member initializers such as maxFileSize = 1024 * 1024, indexingThreads = 0, buildRelativePaths = false, docCommentFormat = DocCommentFormat::markdown, and nested InlayHints defaults).
Because rfl::json::to_schema<Config, rfl::DefaultIfMissing>() does not emit JSON Schema "default" annotations for C++ default member initializers, slang-server (commit 0aae31c, PR #636) parses the generated schema JSON back into rfl::Generic, serializes a default-constructed Config{} via rfl::to_generic(Config{}), recursively traverses #/$defs/... and properties, injects "default" onto each property, and re-serializes the schema:
src/gen_config_main.cpp#L17-L44:using Object = rfl::Generic::Object; auto schema = rfl::json::read<rfl::Generic>( rfl::json::to_schema<Config, rfl::DefaultIfMissing>()) .value(); auto& definitions = std::get<Object>(std::get<Object>(schema.get()).at("$defs").get()); auto addDefaults = [&](auto&& self, rfl::Generic& node, const rfl::Generic& defaults) -> void { const auto* values = std::get_if<Object>(&defaults.get()); if (!values) return; auto* definition = &std::get<Object>(node.get()); if (auto reference = definition->get("$ref")) { auto name = reference->to_string().value().substr( std::string_view("#/$defs/").size()); definition = &std::get<Object>(definitions.at(name).get()); } if (!definition->count("properties")) return; auto& properties = std::get<Object>(definition->at("properties").get()); for (const auto& [name, value] : *values) { auto& property = properties.at(name); std::get<Object>(property.get())["default"] = value; self(self, property, value); } }; addDefaults(addDefaults, schema, rfl::to_generic(Config{})); std::cout << rfl::json::write(schema, rfl::json::pretty | YYJSON_WRITE_PRETTY_TWO_SPACES) << '\n';
- Tested explicitly in
tests/scripts/test_config_schema.py#L32-L52(including verifying thatstd::optionalfields likebuilddo not get"default": null, while scalar/container/nested-struct defaults do) and shipped inclients/vscode/resources/config.schema.json#L14-L250.
Root Cause & Suggested Direction
rfl::json::to_schema<T, Processors...>() operates purely on static type structure and does not inspect a default-constructed T{} (even when rfl::DefaultIfMissing is passed or an opt-in schema option is requested).
Provide an opt-in mechanism (e.g. a to_schema overload accepting a prototype const T& default_value or a schema option/processor for std::default_initializable<T>) that natively emits "default" annotations for non-nullopt default-initialized fields and nested $defs without requiring consumers to round-trip the schema through rfl::Generic.
Upstream Overlap
- [Feature Request] Add support to JSON Schema "default" keyword munich-data-engineering/reflect-cpp#396 (open, exact) — "[Feature Request] Add support to JSON Schema
defaultkeyword" for plain C++ default member initializers (bool value1 = false; double value2 = 1.0;); maintainer flagged it as "tricky" and it remains open. - WIP: respects DefaultVal in schema and adds default value to schema. fixes #679, #396 munich-data-engineering/reflect-cpp#680 (merged, partial, post-
slang-serverpin; followed by fixNamedTupleParser::to_schema()for non-DefaultValfields munich-data-engineering/reflect-cpp#730 and Fix rfl::DefaultVal in NamedTuples and TaggedUnion schemas munich-data-engineering/reflect-cpp#786) — Added JSON Schema"default"emission viarfl::to_generic, but only for fields wrapped inrfl::DefaultVal<T>, not plain C++ default member initializers underrfl::DefaultIfMissing.
Activity
[-]JSON Schema (slang-server): rfl::json::to_schema omits C++ default member initializer values (forces Generic round-trip post-processor)[/-][+]API/JSON Schema: rfl::json::to_schema omits C++ default member initializer values (forces Generic round-trip post-processor)[/+]clockblocker commentedon Nov 29, 2026
Tracking & PR Cross-Reference
Status: Tier-2 (upstream discussion first) — prototype parked on
discuss/ssot.rfl::DefaultIfMissing, scored 41/100; see Draft PR Upstream PR Queue (sorted by mergeability score) #209 parked table).munich-data-engineering/reflect-cpp#396(maintainer flagged as "tricky") and merged upstream PRmunich-data-engineering/reflect-cpp#680(maintainer pointed towardrfl::to_generic/rfl::Genericfor default value extraction). Needs upstream maintainer design alignment before submitting.