Skip to content

[Rust Server] Support OpenAPI v3 callbacks #5405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/generators/rust-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ sidebar_label: rust-server
|MultiServer|✗|OAS3
|ParameterizedServer|✗|OAS3
|ParameterStyling|✗|OAS3
|Callbacks||OAS3
|Callbacks||OAS3
|LinkObjects|✗|OAS3

### Parameter Feature
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ client = [
{{#usesUrlEncodedForm}}
"serde_urlencoded",
{{/usesUrlEncodedForm}}
{{#hasCallbacks}}
"tokio-tls", "regex", "percent-encoding", "lazy_static",
{{/hasCallbacks}}
"serde_json", "serde_ignored", "hyper", "hyper-tls", "native-tls", "openssl", "tokio", "url"
]
server = [
Expand All @@ -34,6 +37,7 @@ server = [
{{#apiUsesMultipartRelated}}
"hyper_0_10", "mime_multipart",
{{/apiUsesMultipartRelated}}
{{! Anything added to the list below, should probably be added to the callbacks list above }}
"serde_json", "serde_ignored", "hyper", "native-tls", "openssl", "tokio", "tokio-tls", "regex", "percent-encoding", "url", "lazy_static"
]
conversion = ["frunk", "frunk_derives", "frunk_core", "frunk-enum-core", "frunk-enum-derive"]
Expand Down Expand Up @@ -84,7 +88,7 @@ url = {version = "1.5", optional = true}
serde_urlencoded = {version = "0.5.1", optional = true}
{{/usesUrlEncodedForm}}

# Server-specific
# Server, and client callback-specific
lazy_static = { version = "1.4", optional = true }
percent-encoding = {version = "1.0.0", optional = true}
regex = {version = "0.2", optional = true}
Expand All @@ -100,8 +104,9 @@ frunk-enum-core = { version = "0.2.0", optional = true }
[dev-dependencies]
clap = "2.25"
error-chain = "0.12"
env_logger = "0.6"
{{^apiUsesUuid}}
uuid = {version = "0.5", features = ["serde", "v4"]}
uuid = {version = "0.7", features = ["serde", "v4"]}
{{/apiUsesUuid}}

[[example]]
Expand Down
50 changes: 50 additions & 0 deletions modules/openapi-generator/src/main/resources/rust-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Rust Server Generator Templates

The Rust Server Generator templates use Mustache Partials.

The following tree shows which templates include which:

- `api_doc.mustache`
- `cargo-config`
- `Cargo.mustache`
- `context.mustache`
- `client-callbacks-mod.mustache`
- `server-imports.mustache`
- `server-make-service.mustache`
- `server-service-footer.mustache`
- `server-service-header.mustache`
- `server-operation.mustache`
- `client-mod.mustache`
- `client-imports.mustache`
- `client-operation.mustache`
- `example-ca.pem`
- `example-client-main.mustache`
- `example-client-server.mustache`
- `example-server-chain.pem`
- `example-server-common.mustache`
- `example-server-key.pem`
- `example-server-main.mustache`
- `example-server-server.mustache`
- `example-server-operation.mustache`
- `gitignore`
- `header.mustache`
- `lib.mustache`
- `response.mustache`
- `mimetypes.mustache`
- `mimetypes-request.mustache`
- `mimetypes-response.mustache`
- `model_doc.mustache`
- `models.mustache`
- `openapi.mustache`
- `README.mustache`
- `server-callbacks.mustache`
- `client-imports.mustache`
- `client-operation.mustache`
- `server-service-header.mustache`
- `server-mod.mustache`
- `server-imports.mustache`
- `server-make-service.mustache`
- `server-service-footer.mustache`
- `server-service-header.mustache`
- `server-operation.mustache`
- `server-paths.mustache`
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,20 @@ cargo run --example server
### Running the example client
To run a client, follow one of the following simple steps:

```{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}
cargo run --example client {{{operationId}}}{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
```
{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#operation}}
{{#vendorExtensions}}
{{^noClientExample}}
cargo run --example client {{{operationId}}}
{{/noClientExample}}
{{/vendorExtensions}}
{{/operation}}
{{/operations}}
{{/apis}}
{{/apiInfo}}
```

### HTTPS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{{>server-imports}}
use CallbackApi as Api;
{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#operation}}
{{#callbacks}}
{{#urls}}
{{#requests}}
use {{{operationId}}}Response;
{{/requests}}
{{/urls}}
{{/callbacks}}
{{/operation}}
{{/operations}}
{{/apis}}
{{/apiInfo}}

{{#callbacks}}
{{>server-paths}}
{{/callbacks}}

{{>server-make-service}}

{{>server-service-header}}
{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#operation}}
{{#callbacks}}
{{#urls}}
{{#requests}}
{{>server-operation}}
{{/requests}}
{{/urls}}
{{/callbacks}}
{{/operation}}
{{/operations}}
{{/apis}}
{{/apiInfo}}
{{>server-service-footer}}
/// Request parser for `Api`.
pub struct ApiRequestParser;
impl<T> RequestParser<T> for ApiRequestParser {
fn parse_operation_id(request: &Request<T>) -> Result<&'static str, ()> {
let path = paths::GLOBAL_REGEX_SET.matches(request.uri().path());
match request.method() {
{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#operation}}
{{#callbacks}}
{{#urls}}
{{#requests}}
// {{{operationId}}} - {{{httpMethod}}} {{{path}}}
&hyper::Method::{{{vendorExtensions.HttpMethod}}} if path.matched(paths::ID_{{{vendorExtensions.PATH_ID}}}) => Ok("{{{operationId}}}"),
{{/requests}}
{{/urls}}
{{/callbacks}}
{{/operation}}
{{/operations}}
{{/apis}}
{{/apiInfo}}
_ => Err(()),
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use futures;
use futures::{Future, Stream, future, stream};
use hyper;
use hyper::client::HttpConnector;
use hyper::header::{HeaderName, HeaderValue, CONTENT_TYPE};
use hyper::{Body, Uri, Response};
use hyper_tls::HttpsConnector;
use serde_json;
use std::borrow::Cow;
#[allow(unused_imports)]
use std::collections::{HashMap, BTreeMap};
use std::io::{Read, Error, ErrorKind};
use std::error;
use std::fmt;
use std::path::Path;
use std::sync::Arc;
use std::str;
use std::str::FromStr;
use std::string::ToString;
use swagger;
use swagger::client::Service;
use swagger::connector;
use swagger::{ApiError, XSpanIdString, Has, AuthData};
use url::form_urlencoded;
use url::percent_encoding::{utf8_percent_encode, PATH_SEGMENT_ENCODE_SET, QUERY_ENCODE_SET};
{{#apiUsesMultipartFormData}}
use mime::Mime;
use std::io::Cursor;
use multipart::client::lazy::Multipart;
{{/apiUsesMultipartFormData}}
{{#apiUsesMultipartRelated}}
use hyper_0_10::header::{Headers, ContentType};
header! { (ContentId, "Content-ID") => [String] }
use mime_multipart::{Node, Part, generate_boundary, write_multipart};
{{/apiUsesMultipartRelated}}
{{#apiUsesUuid}}
use uuid;
{{/apiUsesUuid}}
{{#usesXml}}
use serde_xml_rs;
{{/usesXml}}

use mimetypes;
use models;
use header;

define_encode_set! {
/// This encode set is used for object IDs
///
/// Aside from the special characters defined in the `PATH_SEGMENT_ENCODE_SET`,
/// the vertical bar (|) is encoded.
pub ID_ENCODE_SET = [PATH_SEGMENT_ENCODE_SET] | {'|'}
}
Loading