Skip to content

Commit b60fc90

Browse files
authored
[Rust Server] Support OpenAPI v3 callbacks (#5405)
* [Rust Server] Support Callbacks * [Rust Server] Support callbacks in the examples * [Rust Server] Update features documentation * [Rust Server] Mark as supporting callbacks * Update samples * [Rust Server] Add tests for callbacks * [Rust Server] Fix README Don't suggest examples which don't exist
1 parent 8f8443d commit b60fc90

File tree

95 files changed

+8524
-5846
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+8524
-5846
lines changed

docs/generators/rust-server.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ sidebar_label: rust-server
163163
|MultiServer|✗|OAS3
164164
|ParameterizedServer|✗|OAS3
165165
|ParameterStyling|✗|OAS3
166-
|Callbacks||OAS3
166+
|Callbacks||OAS3
167167
|LinkObjects|✗|OAS3
168168

169169
### Parameter Feature

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java

Lines changed: 231 additions & 99 deletions
Large diffs are not rendered by default.

modules/openapi-generator/src/main/resources/rust-server/Cargo.mustache

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ client = [
2222
{{#usesUrlEncodedForm}}
2323
"serde_urlencoded",
2424
{{/usesUrlEncodedForm}}
25+
{{#hasCallbacks}}
26+
"tokio-tls", "regex", "percent-encoding", "lazy_static",
27+
{{/hasCallbacks}}
2528
"serde_json", "serde_ignored", "hyper", "hyper-tls", "native-tls", "openssl", "tokio", "url"
2629
]
2730
server = [
@@ -34,6 +37,7 @@ server = [
3437
{{#apiUsesMultipartRelated}}
3538
"hyper_0_10", "mime_multipart",
3639
{{/apiUsesMultipartRelated}}
40+
{{! Anything added to the list below, should probably be added to the callbacks list above }}
3741
"serde_json", "serde_ignored", "hyper", "native-tls", "openssl", "tokio", "tokio-tls", "regex", "percent-encoding", "url", "lazy_static"
3842
]
3943
conversion = ["frunk", "frunk_derives", "frunk_core", "frunk-enum-core", "frunk-enum-derive"]
@@ -84,7 +88,7 @@ url = {version = "1.5", optional = true}
8488
serde_urlencoded = {version = "0.5.1", optional = true}
8589
{{/usesUrlEncodedForm}}
8690

87-
# Server-specific
91+
# Server, and client callback-specific
8892
lazy_static = { version = "1.4", optional = true }
8993
percent-encoding = {version = "1.0.0", optional = true}
9094
regex = {version = "0.2", optional = true}
@@ -100,8 +104,9 @@ frunk-enum-core = { version = "0.2.0", optional = true }
100104
[dev-dependencies]
101105
clap = "2.25"
102106
error-chain = "0.12"
107+
env_logger = "0.6"
103108
{{^apiUsesUuid}}
104-
uuid = {version = "0.5", features = ["serde", "v4"]}
109+
uuid = {version = "0.7", features = ["serde", "v4"]}
105110
{{/apiUsesUuid}}
106111

107112
[[example]]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Rust Server Generator Templates
2+
3+
The Rust Server Generator templates use Mustache Partials.
4+
5+
The following tree shows which templates include which:
6+
7+
- `api_doc.mustache`
8+
- `cargo-config`
9+
- `Cargo.mustache`
10+
- `context.mustache`
11+
- `client-callbacks-mod.mustache`
12+
- `server-imports.mustache`
13+
- `server-make-service.mustache`
14+
- `server-service-footer.mustache`
15+
- `server-service-header.mustache`
16+
- `server-operation.mustache`
17+
- `client-mod.mustache`
18+
- `client-imports.mustache`
19+
- `client-operation.mustache`
20+
- `example-ca.pem`
21+
- `example-client-main.mustache`
22+
- `example-client-server.mustache`
23+
- `example-server-chain.pem`
24+
- `example-server-common.mustache`
25+
- `example-server-key.pem`
26+
- `example-server-main.mustache`
27+
- `example-server-server.mustache`
28+
- `example-server-operation.mustache`
29+
- `gitignore`
30+
- `header.mustache`
31+
- `lib.mustache`
32+
- `response.mustache`
33+
- `mimetypes.mustache`
34+
- `mimetypes-request.mustache`
35+
- `mimetypes-response.mustache`
36+
- `model_doc.mustache`
37+
- `models.mustache`
38+
- `openapi.mustache`
39+
- `README.mustache`
40+
- `server-callbacks.mustache`
41+
- `client-imports.mustache`
42+
- `client-operation.mustache`
43+
- `server-service-header.mustache`
44+
- `server-mod.mustache`
45+
- `server-imports.mustache`
46+
- `server-make-service.mustache`
47+
- `server-service-footer.mustache`
48+
- `server-service-header.mustache`
49+
- `server-operation.mustache`
50+
- `server-paths.mustache`

modules/openapi-generator/src/main/resources/rust-server/README.mustache

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,20 @@ cargo run --example server
6262
### Running the example client
6363
To run a client, follow one of the following simple steps:
6464

65-
```{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}
66-
cargo run --example client {{{operationId}}}{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
65+
```
66+
{{#apiInfo}}
67+
{{#apis}}
68+
{{#operations}}
69+
{{#operation}}
70+
{{#vendorExtensions}}
71+
{{^noClientExample}}
72+
cargo run --example client {{{operationId}}}
73+
{{/noClientExample}}
74+
{{/vendorExtensions}}
75+
{{/operation}}
76+
{{/operations}}
77+
{{/apis}}
78+
{{/apiInfo}}
6779
```
6880

6981
### HTTPS
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{{>server-imports}}
2+
use CallbackApi as Api;
3+
{{#apiInfo}}
4+
{{#apis}}
5+
{{#operations}}
6+
{{#operation}}
7+
{{#callbacks}}
8+
{{#urls}}
9+
{{#requests}}
10+
use {{{operationId}}}Response;
11+
{{/requests}}
12+
{{/urls}}
13+
{{/callbacks}}
14+
{{/operation}}
15+
{{/operations}}
16+
{{/apis}}
17+
{{/apiInfo}}
18+
19+
{{#callbacks}}
20+
{{>server-paths}}
21+
{{/callbacks}}
22+
23+
{{>server-make-service}}
24+
25+
{{>server-service-header}}
26+
{{#apiInfo}}
27+
{{#apis}}
28+
{{#operations}}
29+
{{#operation}}
30+
{{#callbacks}}
31+
{{#urls}}
32+
{{#requests}}
33+
{{>server-operation}}
34+
{{/requests}}
35+
{{/urls}}
36+
{{/callbacks}}
37+
{{/operation}}
38+
{{/operations}}
39+
{{/apis}}
40+
{{/apiInfo}}
41+
{{>server-service-footer}}
42+
/// Request parser for `Api`.
43+
pub struct ApiRequestParser;
44+
impl<T> RequestParser<T> for ApiRequestParser {
45+
fn parse_operation_id(request: &Request<T>) -> Result<&'static str, ()> {
46+
let path = paths::GLOBAL_REGEX_SET.matches(request.uri().path());
47+
match request.method() {
48+
{{#apiInfo}}
49+
{{#apis}}
50+
{{#operations}}
51+
{{#operation}}
52+
{{#callbacks}}
53+
{{#urls}}
54+
{{#requests}}
55+
// {{{operationId}}} - {{{httpMethod}}} {{{path}}}
56+
&hyper::Method::{{{vendorExtensions.HttpMethod}}} if path.matched(paths::ID_{{{vendorExtensions.PATH_ID}}}) => Ok("{{{operationId}}}"),
57+
{{/requests}}
58+
{{/urls}}
59+
{{/callbacks}}
60+
{{/operation}}
61+
{{/operations}}
62+
{{/apis}}
63+
{{/apiInfo}}
64+
_ => Err(()),
65+
}
66+
}
67+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use futures;
2+
use futures::{Future, Stream, future, stream};
3+
use hyper;
4+
use hyper::client::HttpConnector;
5+
use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE};
6+
use hyper::{Body, Uri, Response};
7+
use hyper_tls::HttpsConnector;
8+
use serde_json;
9+
use std::borrow::Cow;
10+
#[allow(unused_imports)]
11+
use std::collections::{HashMap, BTreeMap};
12+
use std::io::{Read, Error, ErrorKind};
13+
use std::error;
14+
use std::fmt;
15+
use std::path::Path;
16+
use std::sync::Arc;
17+
use std::str;
18+
use std::str::FromStr;
19+
use std::string::ToString;
20+
use swagger;
21+
use swagger::client::Service;
22+
use swagger::connector;
23+
use swagger::{ApiError, XSpanIdString, Has, AuthData};
24+
use url::form_urlencoded;
25+
use url::percent_encoding::{utf8_percent_encode, PATH_SEGMENT_ENCODE_SET, QUERY_ENCODE_SET};
26+
{{#apiUsesMultipartFormData}}
27+
use mime::Mime;
28+
use std::io::Cursor;
29+
use multipart::client::lazy::Multipart;
30+
{{/apiUsesMultipartFormData}}
31+
{{#apiUsesMultipartRelated}}
32+
use hyper_0_10::header::{Headers, ContentType};
33+
header! { (ContentId, "Content-ID") => [String] }
34+
use mime_multipart::{Node, Part, generate_boundary, write_multipart};
35+
{{/apiUsesMultipartRelated}}
36+
{{#apiUsesUuid}}
37+
use uuid;
38+
{{/apiUsesUuid}}
39+
{{#usesXml}}
40+
use serde_xml_rs;
41+
{{/usesXml}}
42+
43+
use mimetypes;
44+
use models;
45+
use header;
46+
47+
define_encode_set! {
48+
/// This encode set is used for object IDs
49+
///
50+
/// Aside from the special characters defined in the `PATH_SEGMENT_ENCODE_SET`,
51+
/// the vertical bar (|) is encoded.
52+
pub ID_ENCODE_SET = [PATH_SEGMENT_ENCODE_SET] | {'|'}
53+
}

0 commit comments

Comments
 (0)