Skip to content

API/JSON Schema: rfl::json::to_schema omits C++ default member initializer values (forces Generic round-trip post-processor) #166

Backlog

Description

@clockblocker

Part of #138
Popularity: 1/7 corpus consumers (slang-server)

Related issues

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 that std::optional fields like build do not get "default": null, while scalar/container/nested-struct defaults do) and shipped in clients/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

Activity

  1. added
    known-downstream-workarounds
    on Nov 29, 2026
  2. changed the title [-]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)[/+] on Nov 29, 2026
  3. clockblocker commented on Nov 29, 2026

    @clockblocker
    OwnerAuthor

    Tracking & PR Cross-Reference

    Status: Tier-2 (upstream discussion first) — prototype parked on discuss/ssot.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    known-downstream-workarounds

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions