endpoint-gen 1.13.1

Schema-first code generator for WebSocket RPC services: turns declarative RON endpoint definitions into Rust models, docs, MCP tool schemas, and optional OpenAPI 3.1 / AsyncAPI 3.0 documents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! OpenAPI 3.1 emission.
//!
//! **This document is a projection for tooling, not a description of a running
//! HTTP surface.** The server speaks WebSocket, carrying `{method, seq, params}`
//! frames; there are no URLs. The paths here are synthesized so that OpenAPI
//! tooling — client generators, doc renderers, OpenAPI→MCP bridges — has
//! something to chew on. Generating an HTTP client from this and pointing it at
//! the server will not work. The AsyncAPI document (see [`crate::asyncapi`]) is
//! the authoritative description of the wire protocol.
//!
//! That warning is repeated in the emitted `info.description` and in
//! `docs/openapi-README.md`, because an undocumented synthetic path map is worse
//! than no document at all: it looks usable.
//!
//! Conventions (PLAN-2.1 §3.1), which mirror the RON namespace `(service_name,
//! service_id)`:
//!
//! ```text
//! POST /{serviceName}/{endpoint_snake_name}
//!   operationId: {serviceName}_{endpoint_snake_name}
//!   tags:        [{serviceName}]
//! ```

use convert_case::{Case, Casing};
use endpoint_libs::model::{EndpointSchema, SchemaComponents, TypeRegistry, apply_meta};
use eyre::{Context, Result};
use serde_json::{Value, json};

use crate::definitions::{ErrorCodeSchema, GenService};
use crate::docs::Data;
use crate::spec_common::{
    ERROR_ENVELOPE, build_registry, collect_components, document_schemas, document_title, error_code_list,
    visible_services,
};

/// The security scheme name used for every operation.
const SESSION_TOKEN_SCHEME: &str = "sessionToken";

/// Builds the document. Separated from writing so tests can assert on the value
/// without touching a filesystem.
pub fn build_openapi(data: &Data, public_only: bool) -> Result<Value> {
    let registry = build_registry(data);
    let services = visible_services(data, public_only);

    let components = collect_components(&services, &registry)?;

    let mut paths = serde_json::Map::new();
    let mut tags = Vec::new();

    for service in &services {
        tags.push(json!({
            "name": service.name,
            "description": format!("Endpoints of the `{}` service (service id {}).", service.name, service.id),
        }));

        for element in &service.endpoints {
            let schema = &element.schema;
            let path = operation_path(&service.name, &schema.name);
            let operation = build_operation(
                service,
                schema,
                element.frontend_facing,
                &components,
                &registry,
                &data.error_codes,
            )
            .with_context(|| format!("endpoint {} ({})", schema.name, schema.code))?;

            paths.insert(path, json!({ "post": operation }));
        }
    }

    Ok(json!({
        "openapi": "3.1.0",
        "info": {
            "title": document_title(data),
            "version": "1.0.0",
            "description": INFO_DESCRIPTION,
        },
        // A relative "/" rather than a plausible https:// URL. OpenAPI defaults
        // to this when `servers` is omitted, but linters flag an absent
        // `servers` as an error, and a real-looking host would invite exactly
        // the mistake info.description warns about.
        "servers": [{
            "url": "/",
            "description": "Not a real server. This document is a projection for tooling; \
                            the transport is a WebSocket. See info.description.",
        }],
        "tags": tags,
        "paths": Value::Object(paths),
        "components": {
            "schemas": document_schemas(&components),
            "securitySchemes": {
                SESSION_TOKEN_SCHEME: {
                    "type": "apiKey",
                    "in": "header",
                    "name": "Sec-WebSocket-Protocol",
                    "description": "Auth token passed as a WebSocket subprotocol; see AuthController.",
                }
            },
        },
    }))
}

/// Writes `docs/openapi.json`.
pub fn gen_openapi(data: &Data, public_only: bool) -> Result<()> {
    let docs_dir = data.project_root.join("docs");
    std::fs::create_dir_all(&docs_dir)?;

    let document = build_openapi(data, public_only)?;
    let filename = docs_dir.join("openapi.json");
    let file = std::fs::File::create(&filename)
        .with_context(|| format!("Failed to create OpenAPI file: {}", filename.display()))?;
    serde_json::to_writer_pretty(file, &document)?;
    Ok(())
}

/// The synthetic-path warning. Non-negotiable — see the module docs.
const INFO_DESCRIPTION: &str = "\
PROJECTION FOR TOOLING — NOT A SERVABLE HTTP API.

This document is generated from declarative RON endpoint definitions
(endpoint-libs / endpoint-gen). The production transport is a persistent
WebSocket carrying {method, seq, params} frames, plus MCP JSON-RPC 2.0 on the
same socket. There are no HTTP paths; the ones below are synthesized as
/{serviceName}/{endpoint_snake_name} so that OpenAPI tooling has a stable
handle on each operation.

Generating an HTTP client from this document and pointing it at the server
will NOT work. For an authoritative description of the wire protocol, use the
AsyncAPI document (docs/asyncapi.json).

Vendor extensions: x-endpoint-code (the wire method code), x-roles (RBAC roles
required), x-frontend-facing, x-stream-response, x-error-codes.";

/// `POST /{serviceName}/{endpoint_snake_name}`.
///
/// Endpoint names are only conventionally unique across services — the RON
/// namespace is `(service_name, service_id)` — so the path must carry the
/// service. A flat `/rpc/{Name}` scheme collides the moment two services reuse
/// a name, and `/rpc/` carries no information.
fn operation_path(service_name: &str, endpoint_name: &str) -> String {
    format!("/{}/{}", service_name, endpoint_name.to_case(Case::Snake))
}

fn operation_id(service_name: &str, endpoint_name: &str) -> String {
    format!("{}_{}", service_name, endpoint_name.to_case(Case::Snake))
}

#[allow(clippy::too_many_arguments)]
fn build_operation(
    service: &GenService,
    schema: &EndpointSchema,
    frontend_facing: bool,
    components: &SchemaComponents,
    registry: &TypeRegistry,
    error_codes: &[ErrorCodeSchema],
) -> Result<Value> {
    let mut operation = serde_json::Map::new();

    operation.insert("operationId".into(), json!(operation_id(&service.name, &schema.name)));
    operation.insert("tags".into(), json!([service.name]));

    // `summary` is the first line, `description` the whole thing. Renderers show
    // summary in the operation list and description on the detail page.
    if !schema.description.is_empty() {
        let summary = schema.description.lines().next().unwrap_or_default().trim();
        if !summary.is_empty() {
            operation.insert("summary".into(), json!(summary));
        }
        operation.insert("description".into(), json!(schema.description));
    }

    operation.insert("x-endpoint-code".into(), json!(schema.code));
    operation.insert("x-frontend-facing".into(), json!(frontend_facing));
    if !schema.roles.is_empty() {
        operation.insert("x-roles".into(), json!(schema.roles));
    }

    if schema.stream_response.is_some() {
        operation.insert("x-stream-response".into(), json!(true));
        let note = "This endpoint streams: the server may send multiple response \
                    frames for one request. That has no HTTP equivalent and is not \
                    represented in the responses below — see the AsyncAPI document.";
        let described = match operation.get("description") {
            Some(Value::String(existing)) => format!("{existing}\n\n{note}"),
            _ => note.to_string(),
        };
        operation.insert("description".into(), json!(described));
    }

    operation.insert(
        "security".into(),
        json!([{ SESSION_TOKEN_SCHEME: Vec::<String>::new() }]),
    );

    if !schema.parameters.is_empty() {
        let request = components.request_schema(schema, registry)?;
        operation.insert(
            "requestBody".into(),
            json!({
                "required": true,
                "content": { "application/json": { "schema": request } },
            }),
        );
    }

    operation.insert(
        "responses".into(),
        build_responses(schema, components, registry, error_codes)?,
    );

    let mut operation = Value::Object(operation);
    apply_meta(&mut operation, &schema.meta, &format!("endpoint {}", schema.name))?;
    Ok(operation)
}

/// `200` plus a `default` error response.
///
/// The wire protocol has no status codes, so inventing per-error HTTP statuses
/// would be fiction. The envelope is the contract; `x-error-codes` lists what
/// this endpoint may return, resolved against the global catalog.
fn build_responses(
    schema: &EndpointSchema,
    components: &SchemaComponents,
    registry: &TypeRegistry,
    error_codes: &[ErrorCodeSchema],
) -> Result<Value> {
    let response_schema = components.response_schema(schema, registry)?;

    let mut error_response = json!({
        "description": "Error envelope. The wire protocol has no status codes; \
                        failures arrive as this object.",
        "content": {
            "application/json": {
                "schema": { "$ref": format!("#/components/schemas/{ERROR_ENVELOPE}") }
            }
        },
    });

    if let Some(listed) = error_code_list(schema, error_codes) {
        error_response["x-error-codes"] = listed;
    }

    Ok(json!({
        "200": {
            "description": "Success.",
            "content": { "application/json": { "schema": response_schema } },
        },
        "default": error_response,
    }))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::definitions::{EndpointSchemaElement, RustGenConfig};
    use endpoint_libs::model::{EndpointErrorCodeRef, EndpointErrorSchema, Field, Type, collect_refs};
    use std::path::PathBuf;

    fn element(schema: EndpointSchema, frontend_facing: bool) -> EndpointSchemaElement {
        EndpointSchemaElement {
            frontend_facing,
            config: RustGenConfig::default(),
            schema,
        }
    }

    fn data_with(services: Vec<GenService>) -> Data {
        Data {
            project_name: "api.example.com".into(),
            project_root: PathBuf::from("/tmp/api.example.com"),
            output_dir: PathBuf::from("/tmp/api.example.com/generated"),
            services,
            enums: vec![],
            structs: vec![],
            error_codes: vec![ErrorCodeSchema::new("BadRequest", 400, "The request was malformed.")],
        }
    }

    fn sample_data() -> Data {
        let login = EndpointSchema::new(
            "UserLogin",
            10000,
            vec![
                Field::new("user_name", Type::String),
                Field::new("cursor", Type::Optional(Box::new(Type::String))),
            ],
            vec![Field::new("access_token", Type::String)],
        )
        .with_description("Logs a user in.\nReturns a session token.")
        .with_errors(vec![
            EndpointErrorSchema::new("PasswordTooShort", EndpointErrorCodeRef::new("BadRequest"))
                .with_message("Password too short"),
        ]);

        let internal = EndpointSchema::new("AdminPurge", 40000, vec![], vec![]).with_description("Purges everything.");

        data_with(vec![GenService::new(
            "userApi".into(),
            1,
            vec![element(login, true), element(internal, false)],
        )])
    }

    #[test]
    fn paths_and_operation_ids_follow_the_service_convention() {
        let doc = build_openapi(&sample_data(), false).unwrap();
        let op = &doc["paths"]["/userApi/user_login"]["post"];

        assert_eq!(op["operationId"], "userApi_user_login");
        assert_eq!(op["tags"], json!(["userApi"]));
        assert_eq!(op["x-endpoint-code"], 10000);
    }

    #[test]
    fn summary_is_the_first_line_description_is_the_whole_thing() {
        let doc = build_openapi(&sample_data(), false).unwrap();
        let op = &doc["paths"]["/userApi/user_login"]["post"];

        assert_eq!(op["summary"], "Logs a user in.");
        assert_eq!(op["description"], "Logs a user in.\nReturns a session token.");
    }

    #[test]
    fn optional_parameters_are_not_required() {
        let doc = build_openapi(&sample_data(), false).unwrap();
        let schema =
            &doc["paths"]["/userApi/user_login"]["post"]["requestBody"]["content"]["application/json"]["schema"];
        assert_eq!(schema["required"], json!(["userName"]));
    }

    #[test]
    fn errors_become_a_default_response_not_invented_statuses() {
        let doc = build_openapi(&sample_data(), false).unwrap();
        let responses = &doc["paths"]["/userApi/user_login"]["post"]["responses"];

        assert!(responses["200"].is_object());
        assert!(responses["default"].is_object());
        assert!(
            responses.get("400").is_none(),
            "must not invent per-error HTTP statuses"
        );

        let listed = &responses["default"]["x-error-codes"][0];
        assert_eq!(listed["name"], "PasswordTooShort");
        assert_eq!(listed["code"], "BadRequest");
        // Resolved against the global catalog.
        assert_eq!(listed["value"], 400);
        assert_eq!(listed["description"], "The request was malformed.");
    }

    #[test]
    fn public_only_drops_exactly_the_non_frontend_facing_operations() {
        let full = build_openapi(&sample_data(), false).unwrap();
        assert!(full["paths"]["/userApi/admin_purge"].is_object());

        let public = build_openapi(&sample_data(), true).unwrap();
        assert!(public["paths"]["/userApi/user_login"].is_object());
        assert!(
            public["paths"].get("/userApi/admin_purge").is_none(),
            "public-only document must drop internal operations"
        );
    }

    #[test]
    fn every_ref_resolves_and_no_defs_survive() {
        let registry_struct = Type::struct_(
            "Wallet",
            vec![
                Field::new("id", Type::Int64),
                Field::new("owner", Type::StructRef("Wallet".into())),
            ],
        );
        let mut data = data_with(vec![GenService::new(
            "walletApi".into(),
            2,
            vec![element(
                EndpointSchema::new(
                    "GetWallet",
                    20000,
                    vec![],
                    vec![Field::new("wallet", Type::StructRef("Wallet".into()))],
                )
                .with_description("Gets a wallet."),
                true,
            )],
        )]);
        data.structs = vec![crate::definitions::StructElement {
            config: RustGenConfig::default(),
            inner: registry_struct,
        }];

        let doc = build_openapi(&data, false).unwrap();

        let mut refs = vec![];
        collect_refs(&doc, &mut refs);
        assert!(!refs.is_empty());

        let schemas = doc["components"]["schemas"].as_object().unwrap();
        for target in &refs {
            let name = target
                .strip_prefix("#/components/schemas/")
                .unwrap_or_else(|| panic!("ref {target} is not a components ref"));
            assert!(schemas.contains_key(name), "dangling ref: {target}");
        }

        let serialised = serde_json::to_string(&doc).unwrap();
        assert!(!serialised.contains("$defs"), "no $defs may survive into the document");
    }

    #[test]
    fn servers_is_present_but_not_a_real_host() {
        // Linters error on absent `servers`; a plausible https:// URL would
        // invite the mistake info.description exists to prevent.
        let doc = build_openapi(&sample_data(), false).unwrap();
        assert_eq!(doc["servers"][0]["url"], "/");
        assert!(
            doc["servers"][0]["description"]
                .as_str()
                .unwrap()
                .contains("Not a real server")
        );
    }

    #[test]
    fn the_synthetic_path_warning_is_present() {
        let doc = build_openapi(&sample_data(), false).unwrap();
        let description = doc["info"]["description"].as_str().unwrap();
        assert!(description.contains("NOT A SERVABLE HTTP API"));
        assert!(description.contains("AsyncAPI"));
    }

    #[test]
    fn every_operation_carries_security() {
        let doc = build_openapi(&sample_data(), false).unwrap();
        let op = &doc["paths"]["/userApi/user_login"]["post"];
        assert_eq!(op["security"], json!([{ "sessionToken": [] }]));
        assert!(doc["components"]["securitySchemes"]["sessionToken"].is_object());
    }
}