structured-proxy 8.1.1

Universal gRPC→REST transcoding proxy — config-driven, works with any gRPC service
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use super::*;

#[test]
fn test_extract_path_params() {
    assert_eq!(
        extract_path_params("/v1/profiles/{profile_id}"),
        vec!["profile_id"]
    );
    assert_eq!(
        extract_path_params("/v1/profiles/{profile_id}/devices/{device_id}"),
        vec!["profile_id", "device_id"]
    );
    assert!(extract_path_params("/v1/auth/login").is_empty());
}

#[test]
fn path_param_with_a_field_template_names_the_field() {
    // `{name=shelves/*}` binds the field `name`, not a field called
    // `name=shelves/*`.
    assert_eq!(extract_path_params("/v1/{name=shelves/*}"), vec!["name"]);
    assert_eq!(extract_path_params("/v1/files/{path=**}"), vec!["path"]);
}

#[test]
fn test_docs_html_contains_scalar() {
    let html = docs_html("/openapi.json", "Test API");
    assert!(html.contains("@scalar/api-reference"));
    assert!(html.contains("/openapi.json"));
    assert!(html.contains("Test API"));
}

#[test]
fn test_well_known_schemas() {
    // Verify well-known type mappings are correct.
    let pool = DescriptorPool::global();
    if let Some(ts) = pool.get_message_by_name("google.protobuf.Timestamp") {
        let schema = well_known_schema(&ts);
        assert_eq!(schema["type"], "string");
        assert_eq!(schema["format"], "date-time");
    }
}

fn config() -> OpenApiConfig {
    OpenApiConfig {
        enabled: true,
        path: "/openapi.json".into(),
        docs_path: "/docs".into(),
        title: Some("Test API".into()),
        version: Some("0.1.0".into()),
    }
}

#[test]
fn test_generate_empty_pool() {
    let pool = DescriptorPool::new();
    let spec = generate(&pool, &config(), &[], &RpcSelection::default());

    assert_eq!(spec["openapi"], "3.0.3");
    assert_eq!(spec["info"]["title"], "Test API");
    assert_eq!(spec["info"]["version"], "0.1.0");
    assert!(spec["paths"].as_object().unwrap().is_empty());
}

#[test]
fn test_field_to_schema_primitives() {
    // Test via JSON output structure.
    let schema = json!({ "type": "string" });
    assert_eq!(schema["type"], "string");

    let int_schema = json!({ "type": "integer", "format": "int32" });
    assert_eq!(int_schema["format"], "int32");

    let i64_schema = json!({ "type": "string", "format": "int64", "description": "64-bit integer (string-encoded)" });
    assert_eq!(i64_schema["type"], "string");
    assert_eq!(i64_schema["format"], "int64");
}

// --- specs generated from annotated descriptors --------------------------------

const HTTP_PROTO: &str = r#"
syntax = "proto3";
package google.api;
message HttpRule {
  string selector = 1;
  oneof pattern {
    string get = 2;
    string put = 3;
    string post = 4;
    string delete = 5;
    string patch = 6;
    CustomHttpPattern custom = 8;
  }
  string body = 7;
  string response_body = 12;
  repeated HttpRule additional_bindings = 11;
}
message CustomHttpPattern {
  string kind = 1;
  string path = 2;
}
"#;

const ANNOTATIONS_PROTO: &str = r#"
syntax = "proto3";
package google.api;
import "google/api/http.proto";
import "google/protobuf/descriptor.proto";
extend google.protobuf.MethodOptions {
  HttpRule http = 72295728;
}
"#;

const HTTPBODY_PROTO: &str = r#"
syntax = "proto3";
package google.api;
import "google/protobuf/any.proto";
message HttpBody {
  string content_type = 1;
  bytes data = 2;
  repeated google.protobuf.Any extensions = 3;
}
"#;

const API_PROTO: &str = r#"
syntax = "proto3";
package test.v1;
import "google/api/annotations.proto";
import "google/api/httpbody.proto";

message Item {
  string name = 1;
  int32 count = 2;
  Note note = 3;
}
message Note {
  string text = 1;
}
message Filter {
  string q = 1;
}
message Search {
  Filter filter = 1;
}
message Upload {
  string name = 1;
  google.api.HttpBody file = 2;
}
message Tree {
  string name = 1;
  Tree child = 2;
}

service Api {
  rpc Head(Item) returns (Item) {
    option (google.api.http) = { custom: { kind: "HEAD", path: "/v1/items/{name}" } };
  }
  rpc Verify(Item) returns (Item) {
    option (google.api.http) = { custom: { kind: "*", path: "/v1/verify" } };
  }
  rpc Propfind(Item) returns (Item) {
    option (google.api.http) = { custom: { kind: "PROPFIND", path: "/v1/dav" } };
  }
  rpc Create(Item) returns (Item) {
    option (google.api.http) = {
      post: "/v1/items"
      body: "*"
      additional_bindings { put: "/v1/items/{name}" body: "note" }
      additional_bindings { post: "/v1/items:touch" }
      additional_bindings { get: "/v1/items/{name}/note" response_body: "note" }
    };
  }
  rpc Walk(Tree) returns (Tree) {
    option (google.api.http) = { get: "/v1/tree" };
  }
  rpc Plant(Tree) returns (Tree) {
    option (google.api.http) = { post: "/v1/tree" body: "*" };
  }
  rpc Put(Upload) returns (google.api.HttpBody) {
    option (google.api.http) = { put: "/v1/uploads/{name}" body: "file" };
  }
  rpc Find(Search) returns (Item) {
    option (google.api.http) = { get: "/v1/find" };
  }
  rpc Raw(google.api.HttpBody) returns (google.api.HttpBody) {
    option (google.api.http) = { post: "/v1/raw" body: "*" };
  }
  rpc Archive(Item) returns (Item) {
    option (google.api.http) = { post: "/v1/items/{name}:archive" };
  }
  rpc Broken(Item) returns (Item) {
    option (google.api.http) = { get: "/v1/broken/a{name}b" };
  }
  rpc Shelved(Item) returns (Item) {
    option (google.api.http) = { get: "/v1/shelf/{name=foo%3Abar/*}/books" };
  }
  rpc Shadowed(Item) returns (Item) {
    option (google.api.http) = { get: "/v1/shelf/{name=**}" };
  }
}
"#;

struct TestProtos;

impl protox::file::FileResolver for TestProtos {
    fn open_file(&self, name: &str) -> Result<protox::file::File, protox::Error> {
        let source = match name {
            "google/api/http.proto" => HTTP_PROTO,
            "google/api/annotations.proto" => ANNOTATIONS_PROTO,
            "google/api/httpbody.proto" => HTTPBODY_PROTO,
            "test/v1/api.proto" => API_PROTO,
            _ => return protox::file::GoogleFileResolver::new().open_file(name),
        };
        protox::file::File::from_source(name, source)
    }
}

fn spec(aliases: &[AliasConfig]) -> Value {
    spec_of(aliases, &[])
}

/// [`spec`] documenting only the RPCs `only` selects.
fn spec_of(aliases: &[AliasConfig], only: &[&str]) -> Value {
    let pool = protox::Compiler::with_file_resolver(TestProtos)
        .open_file("test/v1/api.proto")
        .unwrap()
        .descriptor_pool();
    let selection = RpcSelection::new(&pool, only).unwrap();
    generate(&pool, &config(), aliases, &selection)
}

#[test]
fn custom_head_rule_is_a_head_operation() {
    let spec = spec(&[]);
    let item = &spec["paths"]["/v1/items/{name}"];
    assert_eq!(item["head"]["operationId"], "Api.Head");
    assert_eq!(item["head"]["parameters"][0]["in"], "path");
}

#[test]
fn custom_verb_after_a_variable_keeps_its_path_and_parameter() {
    // OpenAPI path templating allows a template expression inside a segment,
    // so the verb stays in the path and the variable is a path parameter.
    let spec = spec(&[]);
    let archive = &spec["paths"]["/v1/items/{name}:archive"]["post"];
    assert_eq!(archive["operationId"], "Api.Archive");
    let params = archive["parameters"].as_array().unwrap();
    let path_params: Vec<&Value> = params.iter().filter(|p| p["in"] == "path").collect();
    assert_eq!(path_params.len(), 1);
    assert_eq!(path_params[0]["name"], "name");
    // No path item without the verb comes from this binding.
    assert!(spec["paths"]["/v1/items/{name}"].get("post").is_none());
}

#[test]
fn a_route_the_router_refuses_beside_another_is_not_documented() {
    // The escaped literal is a variable to the router, which then refuses the
    // catch-all after it at that position: only the route it serves is in
    // the spec.
    let spec = spec(&[]);
    let paths = spec["paths"].as_object().unwrap();
    assert!(
        paths.contains_key("/v1/shelf/{name=foo%3Abar/*}/books"),
        "{paths:?}"
    );
    assert!(!paths.contains_key("/v1/shelf/{name=**}"), "{paths:?}");
}

#[test]
fn a_template_the_router_cannot_serve_is_not_documented() {
    // The router skips `a{name}b`; the spec must not promise it either.
    let spec = spec(&[]);
    let paths = spec["paths"].as_object().unwrap();
    assert!(!paths.contains_key("/v1/broken/a{name}b"), "{paths:?}");
    assert!(!paths.values().any(|item| item
        .as_object()
        .unwrap()
        .values()
        .any(|op| op["operationId"] == "Api.Broken")));
}

#[test]
fn star_rule_is_listed_under_every_method_with_distinct_ids() {
    let spec = spec(&[]);
    let verify = spec["paths"]["/v1/verify"].as_object().unwrap();
    let mut methods: Vec<&str> = verify.keys().map(String::as_str).collect();
    methods.sort_unstable();
    let mut expected = OPERATIONS.to_vec();
    expected.sort_unstable();
    assert_eq!(methods, expected);
    let ids: HashSet<&str> = verify
        .values()
        .map(|op| op["operationId"].as_str().unwrap())
        .collect();
    assert_eq!(ids.len(), OPERATIONS.len());
    assert!(ids.contains("Api.Verify_head"));
}

#[test]
fn extension_method_has_no_openapi_operation() {
    // OpenAPI 3.0 has no slot for PROPFIND.
    assert!(spec(&[])["paths"].get("/v1/dav").is_none());
}

#[test]
fn additional_bindings_are_listed() {
    let spec = spec(&[]);
    assert_eq!(
        spec["paths"]["/v1/items"]["post"]["operationId"],
        "Api.Create"
    );
    assert_eq!(
        spec["paths"]["/v1/items/{name}"]["put"]["operationId"],
        "Api.Create_2"
    );
    assert_eq!(
        spec["paths"]["/v1/items:touch"]["post"]["operationId"],
        "Api.Create_3"
    );
}

#[test]
fn body_rule_decides_body_and_query_fields() {
    let spec = spec(&[]);
    // `body: "*"`: the whole message is the body, nothing in the query.
    let create = &spec["paths"]["/v1/items"]["post"];
    assert_eq!(
        create["requestBody"]["content"]["application/json"]["schema"]["$ref"],
        "#/components/schemas/Item"
    );
    assert!(create.get("parameters").is_none(), "{create}");

    // `body: "note"`: that field is the body, the rest is path or query.
    let put = &spec["paths"]["/v1/items/{name}"]["put"];
    assert_eq!(
        put["requestBody"]["content"]["application/json"]["schema"]["$ref"],
        "#/components/schemas/Note"
    );
    let params: Vec<(&str, &str)> = put["parameters"]
        .as_array()
        .unwrap()
        .iter()
        .map(|p| (p["name"].as_str().unwrap(), p["in"].as_str().unwrap()))
        .collect();
    assert_eq!(params, [("name", "path"), ("count", "query")]);

    // No body rule on a POST: every field is a query parameter.
    let touch = &spec["paths"]["/v1/items:touch"]["post"];
    assert!(touch.get("requestBody").is_none(), "{touch}");
    assert_eq!(touch["parameters"].as_array().unwrap().len(), 3);
}

#[test]
fn message_typed_query_parameter_schema_is_registered() {
    // `filter` is bound only through the query; its `$ref` must resolve to a
    // schema in `components`.
    let spec = spec(&[]);
    let param = &spec["paths"]["/v1/find"]["get"]["parameters"][0];
    assert_eq!(param["in"], "query");
    assert_eq!(param["schema"]["$ref"], "#/components/schemas/Filter");
    assert!(
        spec["components"]["schemas"].get("Filter").is_some(),
        "{}",
        spec["components"]["schemas"]
    );
}

#[test]
fn self_referencing_message_does_not_recurse_forever() {
    // `Tree.child` is a `Tree`: in the query (GET) and in the body (POST) its
    // schema is registered once and referenced, instead of being expanded
    // until the stack overflows.
    let spec = spec(&[]);
    let tree = &spec["components"]["schemas"]["Tree"];
    assert_eq!(
        tree["properties"]["child"]["$ref"],
        "#/components/schemas/Tree"
    );
    assert_eq!(
        spec["paths"]["/v1/tree"]["get"]["parameters"][1]["schema"]["$ref"],
        "#/components/schemas/Tree"
    );
    assert!(spec["paths"]["/v1/tree"]["post"]["requestBody"].is_object());
}

#[test]
fn head_operations_describe_no_response_content() {
    // A HEAD response has no content (RFC 9110 §9.3.2), whether the binding
    // is a `HEAD` rule or the HEAD operation of a `*` rule.
    let spec = spec(&[]);
    for path in ["/v1/items/{name}", "/v1/verify"] {
        let head = &spec["paths"][path]["head"]["responses"]["200"];
        assert!(head.is_object(), "{path}");
        assert!(head.get("content").is_none(), "{path}: {head}");
    }
    // The other operations of the `*` rule keep their content.
    assert!(spec["paths"]["/v1/verify"]["get"]["responses"]["200"]["content"].is_object());
}

#[test]
fn json_response_body_is_described_by_the_selected_field() {
    // `response_body: "note"` answers with the `note` field only.
    let spec = spec(&[]);
    let content = &spec["paths"]["/v1/items/{name}/note"]["get"]["responses"]["200"]["content"];
    assert_eq!(
        content["application/json"]["schema"]["$ref"],
        "#/components/schemas/Note"
    );
}

#[test]
fn http_body_request_and_response_are_raw_content() {
    let spec = spec(&[]);
    let raw = json!({ "*/*": { "schema": { "type": "string", "format": "binary" } } });
    let put = &spec["paths"]["/v1/uploads/{name}"]["put"];
    assert_eq!(put["requestBody"]["content"], raw);
    assert_eq!(put["responses"]["200"]["content"], raw);
    let root = &spec["paths"]["/v1/raw"]["post"];
    assert_eq!(root["requestBody"]["content"], raw);
    assert_eq!(root["responses"]["200"]["content"], raw);
}

#[test]
fn aliases_get_their_own_operation_ids() {
    let alias: AliasConfig = serde_yaml::from_str("from: /api/{path}\nto: /v1").unwrap();
    let spec = spec(&[alias]);
    assert_eq!(
        spec["paths"]["/v1/items"]["post"]["operationId"],
        "Api.Create"
    );
    let aliased = &spec["paths"]["/api/items"]["post"]["operationId"];
    assert!(aliased.is_string());
    assert_ne!(aliased, "Api.Create");
}

#[test]
fn a_selection_documents_only_the_rpcs_it_transcodes() {
    // The spec describes what is mounted: an RPC left out of `transcode.only`
    // has no route, so it has no operation either.
    let spec = spec_of(&[], &["test.v1.Api/Find"]);
    let paths = spec["paths"].as_object().unwrap();
    assert_eq!(paths.keys().collect::<Vec<_>>(), ["/v1/find"]);
    assert_eq!(spec["tags"][0]["name"], "Api");
    // A whole service selects all of its RPCs.
    let spec = spec_of(&[], &["test.v1.Api"]);
    assert!(spec["paths"]["/v1/items"]["post"].is_object());
}

#[test]
fn a_selection_naming_nothing_in_the_descriptors_is_an_error() {
    let pool = protox::Compiler::with_file_resolver(TestProtos)
        .open_file("test/v1/api.proto")
        .unwrap()
        .descriptor_pool();
    for name in ["test.v1.Nope", "test.v1.Api/Nope", "Api"] {
        let err = RpcSelection::new(&pool, [name]).unwrap_err();
        assert!(err.contains(name), "{err}");
    }
}