diff --git a/modules/openapi-generator/src/main/resources/rust-server/lib.mustache b/modules/openapi-generator/src/main/resources/rust-server/lib.mustache index bca0608ecf9..c163280799e 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/lib.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/lib.mustache @@ -39,9 +39,7 @@ pub const BASE_PATH: &'static str = "{{{basePathWithoutHost}}}"; pub const API_VERSION: &'static str = "{{{appVersion}}}"; {{#apiInfo}}{{#apis}}{{#operations}}{{#operation}} -{{^isResponseFile}} #[derive(Debug, PartialEq)] -{{/isResponseFile}} pub enum {{{operationId}}}Response { {{#responses}} {{#message}} /// {{{message}}}{{/message}} diff --git a/modules/openapi-generator/src/test/resources/2_0/rust-server/rust-server-test.yaml b/modules/openapi-generator/src/test/resources/2_0/rust-server/rust-server-test.yaml index 884c55e1113..5642c8bc113 100644 --- a/modules/openapi-generator/src/test/resources/2_0/rust-server/rust-server-test.yaml +++ b/modules/openapi-generator/src/test/resources/2_0/rust-server/rust-server-test.yaml @@ -34,6 +34,14 @@ paths: description: Success schema: type: string + /file_response: + get: + summary: Get a file + responses: + 200: + description: Success + schema: + type: file parameters: nested_response: name: nested_response diff --git a/samples/server/petstore/rust-server/output/rust-server-test/README.md b/samples/server/petstore/rust-server/output/rust-server-test/README.md index e73332bb06f..3d3469d47e0 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/README.md +++ b/samples/server/petstore/rust-server/output/rust-server-test/README.md @@ -57,6 +57,7 @@ To run a client, follow one of the following simple steps: ``` cargo run --example client DummyGet cargo run --example client DummyPut +cargo run --example client FileResponseGet cargo run --example client HtmlPost ``` diff --git a/samples/server/petstore/rust-server/output/rust-server-test/api/openapi.yaml b/samples/server/petstore/rust-server/output/rust-server-test/api/openapi.yaml index a52f6a1d23a..9e97e1f3d2a 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/api/openapi.yaml +++ b/samples/server/petstore/rust-server/output/rust-server-test/api/openapi.yaml @@ -48,6 +48,17 @@ paths: type: string description: Success summary: Test HTML handling + /file_response: + get: + responses: + 200: + content: + '*/*': + schema: + format: binary + type: string + description: Success + summary: Get a file components: requestBodies: nested_response: diff --git a/samples/server/petstore/rust-server/output/rust-server-test/examples/client.rs b/samples/server/petstore/rust-server/output/rust-server-test/examples/client.rs index f3b871666d5..3f877af3e09 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/examples/client.rs +++ b/samples/server/petstore/rust-server/output/rust-server-test/examples/client.rs @@ -21,6 +21,7 @@ use rust_server_test::{ApiNoContext, ContextWrapperExt, ApiError, DummyGetResponse, DummyPutResponse, + FileResponseGetResponse, HtmlPostResponse }; use clap::{App, Arg}; @@ -32,6 +33,7 @@ fn main() { .possible_values(&[ "DummyGet", "DummyPut", + "FileResponseGet", "HtmlPost", ]) .required(true) @@ -83,6 +85,11 @@ fn main() { println!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has).get().clone()); }, + Some("FileResponseGet") => { + let result = core.run(client.file_response_get()); + println!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has).get().clone()); + }, + Some("HtmlPost") => { let result = core.run(client.html_post("body_example".to_string())); println!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has).get().clone()); diff --git a/samples/server/petstore/rust-server/output/rust-server-test/examples/server_lib/server.rs b/samples/server/petstore/rust-server/output/rust-server-test/examples/server_lib/server.rs index a5355d4862c..ca0d0ff2ac5 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/examples/server_lib/server.rs +++ b/samples/server/petstore/rust-server/output/rust-server-test/examples/server_lib/server.rs @@ -13,6 +13,7 @@ use swagger::{Has, XSpanIdString}; use rust_server_test::{Api, ApiError, DummyGetResponse, DummyPutResponse, + FileResponseGetResponse, HtmlPostResponse }; use rust_server_test::models; @@ -44,6 +45,13 @@ impl Api for Server where C: Has{ Box::new(futures::failed("Generic failure".into())) } + /// Get a file + fn file_response_get(&self, context: &C) -> Box> { + let context = context.clone(); + println!("file_response_get() - X-Span-ID: {:?}", context.get().0.clone()); + Box::new(futures::failed("Generic failure".into())) + } + /// Test HTML handling fn html_post(&self, body: String, context: &C) -> Box> { let context = context.clone(); diff --git a/samples/server/petstore/rust-server/output/rust-server-test/src/client/mod.rs b/samples/server/petstore/rust-server/output/rust-server-test/src/client/mod.rs index a3b62184eed..113aa2b01c9 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/rust-server-test/src/client/mod.rs @@ -41,6 +41,7 @@ use swagger::{ApiError, XSpanId, XSpanIdString, Has, AuthData}; use {Api, DummyGetResponse, DummyPutResponse, + FileResponseGetResponse, HtmlPostResponse }; use models; @@ -369,6 +370,73 @@ if let Some(body) = body { } + fn file_response_get(&self, context: &C) -> Box> { + + + let uri = format!( + "{}/file_response", + self.base_path + ); + + let uri = match Uri::from_str(&uri) { + Ok(uri) => uri, + Err(err) => return Box::new(futures::done(Err(ApiError(format!("Unable to build URI: {}", err))))), + }; + + let mut request = hyper::Request::new(hyper::Method::Get, uri); + + + + request.headers_mut().set(XSpanId((context as &Has).get().0.clone())); + + + Box::new(self.client_service.call(request) + .map_err(|e| ApiError(format!("No response received: {}", e))) + .and_then(|mut response| { + match response.status().as_u16() { + 200 => { + let body = response.body(); + Box::new( + body + .concat2() + .map_err(|e| ApiError(format!("Failed to read response: {}", e))) + .and_then(|body| str::from_utf8(&body) + .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e))) + .and_then(|body| + + serde_json::from_str::(body) + .map_err(|e| e.into()) + + )) + .map(move |body| + FileResponseGetResponse::Success(body) + ) + ) as Box> + }, + code => { + let headers = response.headers().clone(); + Box::new(response.body() + .take(100) + .concat2() + .then(move |body| + future::err(ApiError(format!("Unexpected response code {}:\n{:?}\n\n{}", + code, + headers, + match body { + Ok(ref body) => match str::from_utf8(body) { + Ok(body) => Cow::from(body), + Err(e) => Cow::from(format!("", e)), + }, + Err(e) => Cow::from(format!("", e)), + }))) + ) + ) as Box> + } + } + })) + + } + fn html_post(&self, param_body: String, context: &C) -> Box> { diff --git a/samples/server/petstore/rust-server/output/rust-server-test/src/lib.rs b/samples/server/petstore/rust-server/output/rust-server-test/src/lib.rs index d10d7aa2e43..277bf41c40c 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/src/lib.rs +++ b/samples/server/petstore/rust-server/output/rust-server-test/src/lib.rs @@ -51,6 +51,12 @@ pub enum DummyPutResponse { Success , } +#[derive(Debug, PartialEq)] +pub enum FileResponseGetResponse { + /// Success + Success ( swagger::ByteArray ) , +} + #[derive(Debug, PartialEq)] pub enum HtmlPostResponse { /// Success @@ -67,6 +73,9 @@ pub trait Api { fn dummy_put(&self, inline_object: Option, context: &C) -> Box>; + /// Get a file + fn file_response_get(&self, context: &C) -> Box>; + /// Test HTML handling fn html_post(&self, body: String, context: &C) -> Box>; @@ -81,6 +90,9 @@ pub trait ApiNoContext { fn dummy_put(&self, inline_object: Option) -> Box>; + /// Get a file + fn file_response_get(&self) -> Box>; + /// Test HTML handling fn html_post(&self, body: String) -> Box>; @@ -110,6 +122,11 @@ impl<'a, T: Api, C> ApiNoContext for ContextWrapper<'a, T, C> { self.api().dummy_put(inline_object, &self.context()) } + /// Get a file + fn file_response_get(&self) -> Box> { + self.api().file_response_get(&self.context()) + } + /// Test HTML handling fn html_post(&self, body: String) -> Box> { self.api().html_post(body, &self.context()) diff --git a/samples/server/petstore/rust-server/output/rust-server-test/src/mimetypes.rs b/samples/server/petstore/rust-server/output/rust-server-test/src/mimetypes.rs index e7dd9bc1076..679f683a0bf 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/src/mimetypes.rs +++ b/samples/server/petstore/rust-server/output/rust-server-test/src/mimetypes.rs @@ -4,6 +4,10 @@ pub mod responses { use hyper::mime::*; // The macro is called per-operation to beat the recursion limit + /// Create Mime objects for the response content types for FileResponseGet + lazy_static! { + pub static ref FILE_RESPONSE_GET_SUCCESS: Mime = "*/*".parse().unwrap(); + } /// Create Mime objects for the response content types for HtmlPost lazy_static! { pub static ref HTML_POST_SUCCESS: Mime = "text/html".parse().unwrap(); diff --git a/samples/server/petstore/rust-server/output/rust-server-test/src/server/mod.rs b/samples/server/petstore/rust-server/output/rust-server-test/src/server/mod.rs index c436af8021b..a02a82d6c27 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/rust-server-test/src/server/mod.rs @@ -39,6 +39,7 @@ use swagger::auth::Scopes; use {Api, DummyGetResponse, DummyPutResponse, + FileResponseGetResponse, HtmlPostResponse }; #[allow(unused_imports)] @@ -54,11 +55,13 @@ mod paths { lazy_static! { pub static ref GLOBAL_REGEX_SET: regex::RegexSet = regex::RegexSet::new(&[ r"^/dummy$", + r"^/file_response$", r"^/html$" ]).unwrap(); } pub static ID_DUMMY: usize = 0; - pub static ID_HTML: usize = 1; + pub static ID_FILE_RESPONSE: usize = 1; + pub static ID_HTML: usize = 2; } pub struct NewService { @@ -246,6 +249,60 @@ where }, + // FileResponseGet - GET /file_response + &hyper::Method::Get if path.matched(paths::ID_FILE_RESPONSE) => { + + + + + + + + Box::new({ + {{ + + Box::new(api_impl.file_response_get(&context) + .then(move |result| { + let mut response = Response::new(); + response.headers_mut().set(XSpanId((&context as &Has).get().0.to_string())); + + match result { + Ok(rsp) => match rsp { + FileResponseGetResponse::Success + + (body) + + + => { + response.set_status(StatusCode::try_from(200).unwrap()); + + response.headers_mut().set(ContentType(mimetypes::responses::FILE_RESPONSE_GET_SUCCESS.clone())); + + + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + + response.set_body(body); + }, + }, + Err(_) => { + // Application code returned an error. This should not happen, as the implementation should + // return a valid response. + response.set_status(StatusCode::InternalServerError); + response.set_body("An internal error occurred"); + }, + } + + future::ok(response) + } + )) + + }} + }) as Box> + + + }, + + // HtmlPost - POST /html &hyper::Method::Post if path.matched(paths::ID_HTML) => {