Skip to content

API/JSON Schema: no programmatic/composable OpenAPI schema builder or configurable $ref prefix (forces JSON re-parsing, regex #/definitions/ rewriting, and Draft-07 -> 2020-12 breakage) #182

Backlog

Description

@clockblocker

Part of #138
Popularity: 1/7 corpus consumers (wolf)

Related issues

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:

  1. Emit structured schema objects (rfl::Generic / schema::Definition) directly without stringifying and re-parsing.
  2. Accumulate shared component definitions across many request/response types into a single OpenAPI 3.1 components.schemas map.
  3. Configure the $ref prefix (e.g. #/components/schemas/ instead of #/$defs/ or #/definitions/).
  4. Customize $defs / component schema names (avoiding C++-mangled identifiers like wolf__api__AppListResponse).

Observed Downstream Workarounds

games-on-whales/wolf (ac2461f6)

  1. ~64 standalone rfl::json::to_schema<T>() string generations:
    src/moonlight-server/api/unix_socket_server.cpp#L34-L321 invokes rfl::json::to_schema<...>() per endpoint request and response status code and stores each schema as an opaque std::string in APIDescription::json_schema.

  2. Round-trip JSON re-parsing and manual definitions hoisting in openapi_schema():
    In src/moonlight-server/api/openapi.cpp#L17-L21 and #L39-L57, wolf defines a private JSONSchema struct solely to deserialize to_schema<T>()'s output back into rfl::Generic objects and copy every entry into components.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;
      ...
    };
  3. Global std::regex_replace over 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 definitions and #/definitions/, upgrading reflect-cpp from Draft-07 (v0.21.0) to Draft 2020-12 ($defs / #/$defs/) immediately throws on rfl::json::read<JSONSchema>(schema_str).value() and leaves dangling #/$defs/ references in the generated OpenAPI document.

  4. C++-mangled component schema names in generated OpenAPI spec:
    docs/modules/dev/partials/spec.json#L24-L52 and #L179 expose raw mangled C++ names such as wolf__api__AppListResponse and rfl__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 fresh parsing::schema::Definition, populates its local definitions_ map, and immediately serializes it to std::string with a hardcoded $ref prefix (#/$defs/ in modern reflect-cpp, #/definitions/ in v0.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

Upstream Overlap

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