Repository navigation
Description
Part of #138
Popularity: 1/7 corpus consumers (wolf)
Related issues
- Related fork issues & PRs: 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 —
$defsnaming), Triage & Design Notes (Fork-Only): Speculative Schema / Contract APIs #136 (Speculative Schema / Contract APIs), PR Add SchemaName, SchemaExtension, IntegerBounds, TupleBounds, OmitEmptyRequired, and flattened optional<variant> anyOf #121 (SchemaName).
Problem
rfl::json::to_schema<T>() only returns a standalone serialized std::string containing a single root $ref and a private definitions map hardcoded to #/definitions/ (v0.21) or #/$defs/ (Draft 2020-12). There is no public programmatic schema-builder API to:
- Emit structured schema objects (
rfl::Generic/schema::Definition) directly without stringifying and re-parsing. - Accumulate shared component definitions across many request/response types into a single OpenAPI 3.1
components.schemasmap. - Configure the
$refprefix (e.g.#/components/schemas/instead of#/$defs/or#/definitions/). - Customize
$defs/ component schema names (avoiding C++-mangled identifiers likewolf__api__AppListResponse).
Observed Downstream Workarounds
games-on-whales/wolf (ac2461f6)
-
~64 standalone
rfl::json::to_schema<T>()string generations:
src/moonlight-server/api/unix_socket_server.cpp#L34-L321invokesrfl::json::to_schema<...>()per endpoint request and response status code and stores each schema as an opaquestd::stringinAPIDescription::json_schema. -
Round-trip JSON re-parsing and manual definitions hoisting in
openapi_schema():
Insrc/moonlight-server/api/openapi.cpp#L17-L21and#L39-L57,wolfdefines a privateJSONSchemastruct solely to deserializeto_schema<T>()'s output back intorfl::Genericobjects and copy every entry intocomponents.schemas:struct JSONSchema { std::string $schema; std::string $ref; std::map<std::string, rfl::Object<rfl::Generic>> definitions; }; ... auto set_json_schema = [&schema](const std::string schema_str) { JSONSchema request_schema = rfl::json::read<JSONSchema>(schema_str).value(); // add all definitions to components for (const auto &[name, cur_schema] : request_schema.definitions) { schema.components.schemas[name] = cur_schema; } auto reference = rfl::Generic::Object(); reference["$ref"] = request_schema.$ref; ... };
-
Global
std::regex_replaceover the final serialized OpenAPI JSON:
src/moonlight-server/api/openapi.cpp#L95-L99:auto final_json = rfl::json::write(schema); // we have to replace all #/definitions/ with #/components/schemas/ final_json = std::regex_replace(final_json, std::regex("#/definitions/"), "#/components/schemas/"); return final_json;
Because this workaround hardcodes
definitionsand#/definitions/, upgradingreflect-cppfrom Draft-07 (v0.21.0) to Draft 2020-12 ($defs/#/$defs/) immediately throws onrfl::json::read<JSONSchema>(schema_str).value()and leaves dangling#/$defs/references in the generated OpenAPI document. -
C++-mangled component schema names in generated OpenAPI spec:
docs/modules/dev/partials/spec.json#L24-L52and#L179expose raw mangled C++ names such aswolf__api__AppListResponseandrfl__Reflector_wolf__core__events__App___ReflType.
Root Cause in reflect-cpp
rfl::json::to_schema<T, Ps...>()(include/rfl/json/to_schema.hpp) creates a freshparsing::schema::Definition, populates its localdefinitions_map, and immediately serializes it tostd::stringwith a hardcoded$refprefix (#/$defs/in modernreflect-cpp,#/definitions/inv0.21.0).- No public multi-type schema registry/builder or
$ref-prefix option exists for OpenAPI 3.1 / JSON-RPC / multi-endpoint consumers.
Suggested Direction
- Expose a lightweight structured schema-builder / shared-definitions API (or
to_schema_generic<T>()+ configurable$refprefix such as#/components/schemas/), allowing multi-endpoint APIs to register multiple C++ types into one shared schema map without JSON round-tripping or regex post-processing. - Support out-of-band schema naming (
SchemaName/ unified metadata trait per Prototype JSON Schema extensibility ($defs naming, custom wrapper x-* hooks, integer width bounds, title/examples) #100 / Triage & Design Notes (Fork-Only): Speculative Schema / Contract APIs #136) so OpenAPIcomponents.schemaskeys do not leak mangled C++ namespaces.
Upstream Overlap
- Better support for custom schemas munich-data-engineering/reflect-cpp#552 (open, partial) — "Better support for custom schemas" over
rfl::parsing::schema::Definition/Type(focused on Zod/tagged-union/enum IR rather than OpenAPI$refprefixes or multi-type schema builders). - [json schema] Use non-deprecated $defs; Add $comment option munich-data-engineering/reflect-cpp#523 (merged, partial/context, post-
wolfv0.21.0) — Switched generated schemas from deprecateddefinitions/#/definitions/to$defs/#/$defs/, which is the exact upstream change that breakswolf's hardcoded#/definitions/regex workaround. - Unfiled upstream: Programmatic multi-type OpenAPI schema builder, configurable
$refprefix (#/components/schemas/), and custom component schema naming.
Activity