diff --git a/bin/configs/rust-hyper-anyof.yaml b/bin/configs/rust-hyper-anyof.yaml new file mode 100644 index 000000000000..008f1e7b06e7 --- /dev/null +++ b/bin/configs/rust-hyper-anyof.yaml @@ -0,0 +1,8 @@ +generatorName: rust +outputDir: samples/client/others/rust/hyper/anyof +library: hyper +inputSpec: modules/openapi-generator/src/test/resources/3_0/rust/rust-anyof-test.yaml +templateDir: modules/openapi-generator/src/main/resources/rust +additionalProperties: + supportAsync: false + packageName: anyof-hyper diff --git a/samples/client/others/rust/hyper/anyof/.gitignore b/samples/client/others/rust/hyper/anyof/.gitignore new file mode 100644 index 000000000000..6aa106405a4b --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/.gitignore @@ -0,0 +1,3 @@ +/target/ +**/*.rs.bk +Cargo.lock diff --git a/samples/client/others/rust/hyper/anyof/.openapi-generator-ignore b/samples/client/others/rust/hyper/anyof/.openapi-generator-ignore new file mode 100644 index 000000000000..7484ee590a38 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/.openapi-generator-ignore @@ -0,0 +1,23 @@ +# OpenAPI Generator Ignore +# Generated by openapi-generator https://github.com/openapitools/openapi-generator + +# Use this file to prevent files from being overwritten by the generator. +# The patterns follow closely to .gitignore or .dockerignore. + +# As an example, the C# client generator defines ApiClient.cs. +# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line: +#ApiClient.cs + +# You can match any string of characters against a directory, file or extension with a single asterisk (*): +#foo/*/qux +# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux + +# You can recursively match patterns against a directory, file or extension with a double asterisk (**): +#foo/**/qux +# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux + +# You can also negate patterns with an exclamation (!). +# For example, you can ignore all files in a docs folder with the file extension .md: +#docs/*.md +# Then explicitly reverse the ignore rule for a single file: +#!docs/README.md diff --git a/samples/client/others/rust/hyper/anyof/.openapi-generator/FILES b/samples/client/others/rust/hyper/anyof/.openapi-generator/FILES new file mode 100644 index 000000000000..5eb7ffb44324 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/.openapi-generator/FILES @@ -0,0 +1,19 @@ +.gitignore +.travis.yml +Cargo.toml +README.md +docs/AnotherAnyOfTest.md +docs/DefaultApi.md +docs/ModelIdentifier.md +docs/TestResponse.md +git_push.sh +src/apis/client.rs +src/apis/configuration.rs +src/apis/default_api.rs +src/apis/mod.rs +src/apis/request.rs +src/lib.rs +src/models/another_any_of_test.rs +src/models/mod.rs +src/models/model_identifier.rs +src/models/test_response.rs diff --git a/samples/client/others/rust/hyper/anyof/.openapi-generator/VERSION b/samples/client/others/rust/hyper/anyof/.openapi-generator/VERSION new file mode 100644 index 000000000000..5e5282953086 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/.openapi-generator/VERSION @@ -0,0 +1 @@ +7.16.0-SNAPSHOT diff --git a/samples/client/others/rust/hyper/anyof/.travis.yml b/samples/client/others/rust/hyper/anyof/.travis.yml new file mode 100644 index 000000000000..22761ba7ee19 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/.travis.yml @@ -0,0 +1 @@ +language: rust diff --git a/samples/client/others/rust/hyper/anyof/Cargo.toml b/samples/client/others/rust/hyper/anyof/Cargo.toml new file mode 100644 index 000000000000..51de8b09747f --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "anyof-hyper" +version = "1.0.0" +authors = ["OpenAPI Generator team and contributors"] +description = "No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)" +# Override this license by providing a License Object in the OpenAPI. +license = "Unlicense" +edition = "2021" + +[dependencies] +serde = { version = "^1.0", features = ["derive"] } +serde_json = "^1.0" +serde_repr = "^0.1" +url = "^2.5" +hyper = { version = "^1.3.1", features = ["full"] } +hyper-util = { version = "0.1.5", features = ["client", "client-legacy", "http1", "http2"] } +http-body-util = { version = "0.1.2" } +http = "~0.2" +base64 = "~0.7.0" +futures = "^0.3" diff --git a/samples/client/others/rust/hyper/anyof/README.md b/samples/client/others/rust/hyper/anyof/README.md new file mode 100644 index 000000000000..24331c3263ad --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/README.md @@ -0,0 +1,48 @@ +# Rust API client for anyof-hyper + +No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + + +## Overview + +This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. By using the [openapi-spec](https://openapis.org) from a remote server, you can easily generate an API client. + +- API version: 1.0.0 +- Package version: 1.0.0 +- Generator version: 7.16.0-SNAPSHOT +- Build package: `org.openapitools.codegen.languages.RustClientCodegen` + +## Installation + +Put the package under your project folder in a directory named `anyof-hyper` and add the following to `Cargo.toml` under `[dependencies]`: + +``` +anyof-hyper = { path = "./anyof-hyper" } +``` + +## Documentation for API Endpoints + +All URIs are relative to *http://localhost* + +Class | Method | HTTP request | Description +------------ | ------------- | ------------- | ------------- +*DefaultApi* | [**model_get**](docs/DefaultApi.md#model_get) | **Get** /model | + + +## Documentation For Models + + - [AnotherAnyOfTest](docs/AnotherAnyOfTest.md) + - [ModelIdentifier](docs/ModelIdentifier.md) + - [TestResponse](docs/TestResponse.md) + + +To get access to the crate's generated documentation, use: + +``` +cargo doc --open +``` + +## Author + + + diff --git a/samples/client/others/rust/hyper/anyof/docs/AnotherAnyOfTest.md b/samples/client/others/rust/hyper/anyof/docs/AnotherAnyOfTest.md new file mode 100644 index 000000000000..9ff99dde6d80 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/docs/AnotherAnyOfTest.md @@ -0,0 +1,10 @@ +# AnotherAnyOfTest + +## Properties + +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/client/others/rust/hyper/anyof/docs/DefaultApi.md b/samples/client/others/rust/hyper/anyof/docs/DefaultApi.md new file mode 100644 index 000000000000..72f06e2b48f2 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/docs/DefaultApi.md @@ -0,0 +1,34 @@ +# \DefaultApi + +All URIs are relative to *http://localhost* + +Method | HTTP request | Description +------------- | ------------- | ------------- +[**model_get**](DefaultApi.md#model_get) | **Get** /model | + + + +## model_get + +> models::TestResponse model_get() + + +### Parameters + +This endpoint does not need any parameter. + +### Return type + +[**models::TestResponse**](TestResponse.md) + +### Authorization + +No authorization required + +### HTTP request headers + +- **Content-Type**: Not defined +- **Accept**: application/json + +[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) + diff --git a/samples/client/others/rust/hyper/anyof/docs/ModelIdentifier.md b/samples/client/others/rust/hyper/anyof/docs/ModelIdentifier.md new file mode 100644 index 000000000000..5c5cb57b311c --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/docs/ModelIdentifier.md @@ -0,0 +1,10 @@ +# ModelIdentifier + +## Properties + +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/client/others/rust/hyper/anyof/docs/TestResponse.md b/samples/client/others/rust/hyper/anyof/docs/TestResponse.md new file mode 100644 index 000000000000..de74be5e72d8 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/docs/TestResponse.md @@ -0,0 +1,12 @@ +# TestResponse + +## Properties + +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**model** | Option<[**models::ModelIdentifier**](ModelIdentifier.md)> | | [optional] +**status** | Option<**String**> | | [optional] + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/client/others/rust/hyper/anyof/git_push.sh b/samples/client/others/rust/hyper/anyof/git_push.sh new file mode 100644 index 000000000000..f53a75d4fabe --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/git_push.sh @@ -0,0 +1,57 @@ +#!/bin/sh +# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/ +# +# Usage example: /bin/sh ./git_push.sh wing328 openapi-petstore-perl "minor update" "gitlab.com" + +git_user_id=$1 +git_repo_id=$2 +release_note=$3 +git_host=$4 + +if [ "$git_host" = "" ]; then + git_host="github.com" + echo "[INFO] No command line input provided. Set \$git_host to $git_host" +fi + +if [ "$git_user_id" = "" ]; then + git_user_id="GIT_USER_ID" + echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id" +fi + +if [ "$git_repo_id" = "" ]; then + git_repo_id="GIT_REPO_ID" + echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id" +fi + +if [ "$release_note" = "" ]; then + release_note="Minor update" + echo "[INFO] No command line input provided. Set \$release_note to $release_note" +fi + +# Initialize the local directory as a Git repository +git init + +# Adds the files in the local repository and stages them for commit. +git add . + +# Commits the tracked changes and prepares them to be pushed to a remote repository. +git commit -m "$release_note" + +# Sets the new remote +git_remote=$(git remote) +if [ "$git_remote" = "" ]; then # git remote not defined + + if [ "$GIT_TOKEN" = "" ]; then + echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment." + git remote add origin https://${git_host}/${git_user_id}/${git_repo_id}.git + else + git remote add origin https://${git_user_id}:"${GIT_TOKEN}"@${git_host}/${git_user_id}/${git_repo_id}.git + fi + +fi + +git pull origin master + +# Pushes (Forces) the changes in the local repository up to the remote repository +echo "Git pushing to https://${git_host}/${git_user_id}/${git_repo_id}.git" +git push origin master 2>&1 | grep -v 'To https' diff --git a/samples/client/others/rust/hyper/anyof/src/apis/client.rs b/samples/client/others/rust/hyper/anyof/src/apis/client.rs new file mode 100644 index 000000000000..5554552ee23e --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/src/apis/client.rs @@ -0,0 +1,25 @@ +use std::sync::Arc; + +use hyper; +use hyper_util::client::legacy::connect::Connect; +use super::configuration::Configuration; + +pub struct APIClient { + default_api: Box, +} + +impl APIClient { + pub fn new(configuration: Configuration) -> APIClient + where C: Clone + std::marker::Send + Sync + 'static { + let rc = Arc::new(configuration); + + APIClient { + default_api: Box::new(crate::apis::DefaultApiClient::new(rc.clone())), + } + } + + pub fn default_api(&self) -> &dyn crate::apis::DefaultApi{ + self.default_api.as_ref() + } + +} diff --git a/samples/client/others/rust/hyper/anyof/src/apis/configuration.rs b/samples/client/others/rust/hyper/anyof/src/apis/configuration.rs new file mode 100644 index 000000000000..ecb121335ce0 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/src/apis/configuration.rs @@ -0,0 +1,92 @@ +/* + * Rust anyOf Test + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * The version of the OpenAPI document: 1.0.0 + * + * Generated by: https://openapi-generator.tech + */ + +use hyper; +use hyper_util::client::legacy::Client; +use hyper_util::client::legacy::connect::Connect; +use hyper_util::client::legacy::connect::HttpConnector; +use hyper_util::rt::TokioExecutor; + +pub struct Configuration + where C: Clone + std::marker::Send + Sync + 'static { + pub base_path: String, + pub user_agent: Option, + pub client: Client, + pub basic_auth: Option, + pub oauth_access_token: Option, + pub api_key: Option, + // TODO: take an oauth2 token source, similar to the go one +} + +pub type BasicAuth = (String, Option); + +pub struct ApiKey { + pub prefix: Option, + pub key: String, +} + +impl Configuration { + /// Construct a default [`Configuration`](Self) with a hyper client using a default + /// [`HttpConnector`](hyper_util::client::legacy::connect::HttpConnector). + /// + /// Use [`with_client`](Configuration::with_client) to construct a Configuration with a + /// custom hyper client. + /// + /// # Example + /// + /// ``` + /// # use anyof_hyper::apis::configuration::Configuration; + /// let api_config = Configuration { + /// basic_auth: Some(("user".into(), None)), + /// ..Configuration::new() + /// }; + /// ``` + pub fn new() -> Configuration { + Configuration::default() + } +} + +impl Configuration + where C: Clone + std::marker::Send + Sync { + + /// Construct a new Configuration with a custom hyper client. + /// + /// # Example + /// + /// ``` + /// # use core::time::Duration; + /// # use anyof_hyper::apis::configuration::Configuration; + /// use hyper_util::client::legacy::Client; + /// use hyper_util::rt::TokioExecutor; + /// + /// let client = Client::builder(TokioExecutor::new()) + /// .pool_idle_timeout(Duration::from_secs(30)) + /// .build_http(); + /// + /// let api_config = Configuration::with_client(client); + /// ``` + pub fn with_client(client: Client) -> Configuration { + Configuration { + base_path: "http://localhost".to_owned(), + user_agent: Some("OpenAPI-Generator/1.0.0/rust".to_owned()), + client, + basic_auth: None, + oauth_access_token: None, + api_key: None, + } + } +} + +impl Default for Configuration { + fn default() -> Self { + let client = Client::builder(TokioExecutor::new()).build_http(); + Configuration::with_client(client) + } +} diff --git a/samples/client/others/rust/hyper/anyof/src/apis/default_api.rs b/samples/client/others/rust/hyper/anyof/src/apis/default_api.rs new file mode 100644 index 000000000000..4bca8f3629b2 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/src/apis/default_api.rs @@ -0,0 +1,53 @@ +/* + * Rust anyOf Test + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * The version of the OpenAPI document: 1.0.0 + * + * Generated by: https://openapi-generator.tech + */ + +use std::sync::Arc; +use std::borrow::Borrow; +use std::pin::Pin; +#[allow(unused_imports)] +use std::option::Option; + +use hyper; +use hyper_util::client::legacy::connect::Connect; +use futures::Future; + +use crate::models; +use super::{Error, configuration}; +use super::request as __internal_request; + +pub struct DefaultApiClient + where C: Clone + std::marker::Send + Sync + 'static { + configuration: Arc>, +} + +impl DefaultApiClient + where C: Clone + std::marker::Send + Sync { + pub fn new(configuration: Arc>) -> DefaultApiClient { + DefaultApiClient { + configuration, + } + } +} + +pub trait DefaultApi: Send + Sync { + fn model_get(&self, ) -> Pin> + Send>>; +} + +implDefaultApi for DefaultApiClient + where C: Clone + std::marker::Send + Sync { + #[allow(unused_mut)] + fn model_get(&self, ) -> Pin> + Send>> { + let mut req = __internal_request::Request::new(hyper::Method::GET, "/model".to_string()) + ; + + req.execute(self.configuration.borrow()) + } + +} diff --git a/samples/client/others/rust/hyper/anyof/src/apis/mod.rs b/samples/client/others/rust/hyper/anyof/src/apis/mod.rs new file mode 100644 index 000000000000..58227e44cb35 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/src/apis/mod.rs @@ -0,0 +1,73 @@ +use std::fmt; +use std::fmt::Debug; + +use hyper; +use hyper::http; +use hyper_util::client::legacy::connect::Connect; +use serde_json; + +#[derive(Debug)] +pub enum Error { + Api(ApiError), + Header(http::header::InvalidHeaderValue), + Http(http::Error), + Hyper(hyper::Error), + HyperClient(hyper_util::client::legacy::Error), + Serde(serde_json::Error), + UriError(http::uri::InvalidUri), +} + +pub struct ApiError { + pub code: hyper::StatusCode, + pub body: hyper::body::Incoming, +} + +impl Debug for ApiError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("ApiError") + .field("code", &self.code) + .field("body", &"hyper::body::Incoming") + .finish() + } +} + +impl From<(hyper::StatusCode, hyper::body::Incoming)> for Error { + fn from(e: (hyper::StatusCode, hyper::body::Incoming)) -> Self { + Error::Api(ApiError { + code: e.0, + body: e.1, + }) + } +} + +impl From for Error { + fn from(e: http::Error) -> Self { + Error::Http(e) + } +} + +impl From for Error { + fn from(e: hyper_util::client::legacy::Error) -> Self { + Error::HyperClient(e) + } +} + +impl From for Error { + fn from(e: hyper::Error) -> Self { + Error::Hyper(e) + } +} + +impl From for Error { + fn from(e: serde_json::Error) -> Self { + Error::Serde(e) + } +} + +mod request; + +mod default_api; +pub use self::default_api::{ DefaultApi, DefaultApiClient }; + +pub mod configuration; +pub mod client; diff --git a/samples/client/others/rust/hyper/anyof/src/apis/request.rs b/samples/client/others/rust/hyper/anyof/src/apis/request.rs new file mode 100644 index 000000000000..a6f7b74cc6ef --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/src/apis/request.rs @@ -0,0 +1,247 @@ +use std::collections::HashMap; +use std::pin::Pin; + +use futures; +use futures::Future; +use futures::future::*; +use http_body_util::BodyExt; +use hyper; +use hyper_util::client::legacy::connect::Connect; +use hyper::header::{AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE, HeaderValue, USER_AGENT}; +use serde; +use serde_json; + +use super::{configuration, Error}; + +pub(crate) struct ApiKey { + pub in_header: bool, + pub in_query: bool, + pub param_name: String, +} + +impl ApiKey { + fn key(&self, prefix: &Option, key: &str) -> String { + match prefix { + None => key.to_owned(), + Some(ref prefix) => format!("{} {}", prefix, key), + } + } +} + +#[allow(dead_code)] +pub(crate) enum Auth { + None, + ApiKey(ApiKey), + Basic, + Oauth, +} + +/// If the authorization type is unspecified then it will be automatically detected based +/// on the configuration. This functionality is useful when the OpenAPI definition does not +/// include an authorization scheme. +pub(crate) struct Request { + auth: Option, + method: hyper::Method, + path: String, + query_params: HashMap, + no_return_type: bool, + path_params: HashMap, + form_params: HashMap, + header_params: HashMap, + // TODO: multiple body params are possible technically, but not supported here. + serialized_body: Option, +} + +#[allow(dead_code)] +impl Request { + pub fn new(method: hyper::Method, path: String) -> Self { + Request { + auth: None, + method, + path, + query_params: HashMap::new(), + path_params: HashMap::new(), + form_params: HashMap::new(), + header_params: HashMap::new(), + serialized_body: None, + no_return_type: false, + } + } + + pub fn with_body_param(mut self, param: T) -> Self { + self.serialized_body = Some(serde_json::to_string(¶m).unwrap()); + self + } + + pub fn with_header_param(mut self, basename: String, param: String) -> Self { + self.header_params.insert(basename, param); + self + } + + #[allow(unused)] + pub fn with_query_param(mut self, basename: String, param: String) -> Self { + self.query_params.insert(basename, param); + self + } + + #[allow(unused)] + pub fn with_path_param(mut self, basename: String, param: String) -> Self { + self.path_params.insert(basename, param); + self + } + + #[allow(unused)] + pub fn with_form_param(mut self, basename: String, param: String) -> Self { + self.form_params.insert(basename, param); + self + } + + pub fn returns_nothing(mut self) -> Self { + self.no_return_type = true; + self + } + + pub fn with_auth(mut self, auth: Auth) -> Self { + self.auth = Some(auth); + self + } + + pub fn execute<'a, C, U>( + self, + conf: &configuration::Configuration, + ) -> Pin> + 'a + Send>> + where + C: Connect + Clone + std::marker::Send + Sync, + U: Sized + std::marker::Send + 'a, + for<'de> U: serde::Deserialize<'de>, + { + let mut query_string = ::url::form_urlencoded::Serializer::new("".to_owned()); + + let mut path = self.path; + for (k, v) in self.path_params { + // replace {id} with the value of the id path param + path = path.replace(&format!("{{{}}}", k), &v); + } + + for (key, val) in self.query_params { + query_string.append_pair(&key, &val); + } + + let mut uri_str = format!("{}{}", conf.base_path, path); + + let query_string_str = query_string.finish(); + if !query_string_str.is_empty() { + uri_str += "?"; + uri_str += &query_string_str; + } + let uri: hyper::Uri = match uri_str.parse() { + Err(e) => return Box::pin(futures::future::err(Error::UriError(e))), + Ok(u) => u, + }; + + let mut req_builder = hyper::Request::builder() + .uri(uri) + .method(self.method); + + // Detect the authorization type if it hasn't been set. + let auth = self.auth.unwrap_or_else(|| + if conf.api_key.is_some() { + panic!("Cannot automatically set the API key from the configuration, it must be specified in the OpenAPI definition") + } else if conf.oauth_access_token.is_some() { + Auth::Oauth + } else if conf.basic_auth.is_some() { + Auth::Basic + } else { + Auth::None + } + ); + match auth { + Auth::ApiKey(apikey) => { + if let Some(ref key) = conf.api_key { + let val = apikey.key(&key.prefix, &key.key); + if apikey.in_query { + query_string.append_pair(&apikey.param_name, &val); + } + if apikey.in_header { + req_builder = req_builder.header(&apikey.param_name, val); + } + } + } + Auth::Basic => { + if let Some(ref auth_conf) = conf.basic_auth { + let mut text = auth_conf.0.clone(); + text.push(':'); + if let Some(ref pass) = auth_conf.1 { + text.push_str(&pass[..]); + } + let encoded = base64::encode(&text); + req_builder = req_builder.header(AUTHORIZATION, encoded); + } + } + Auth::Oauth => { + if let Some(ref token) = conf.oauth_access_token { + let text = "Bearer ".to_owned() + token; + req_builder = req_builder.header(AUTHORIZATION, text); + } + } + Auth::None => {} + } + + if let Some(ref user_agent) = conf.user_agent { + req_builder = req_builder.header(USER_AGENT, match HeaderValue::from_str(user_agent) { + Ok(header_value) => header_value, + Err(e) => return Box::pin(futures::future::err(super::Error::Header(e))) + }); + } + + for (k, v) in self.header_params { + req_builder = req_builder.header(&k, v); + } + + let req_headers = req_builder.headers_mut().unwrap(); + let request_result = if self.form_params.len() > 0 { + req_headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/x-www-form-urlencoded")); + let mut enc = ::url::form_urlencoded::Serializer::new("".to_owned()); + for (k, v) in self.form_params { + enc.append_pair(&k, &v); + } + req_builder.body(enc.finish()) + } else if let Some(body) = self.serialized_body { + req_headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json")); + req_headers.insert(CONTENT_LENGTH, body.len().into()); + req_builder.body(body) + } else { + req_builder.body(String::new()) + }; + let request = match request_result { + Ok(request) => request, + Err(e) => return Box::pin(futures::future::err(Error::from(e))) + }; + + let no_return_type = self.no_return_type; + Box::pin(conf.client + .request(request) + .map_err(|e| Error::from(e)) + .and_then(move |response| { + let status = response.status(); + if !status.is_success() { + futures::future::err::(Error::from((status, response.into_body()))).boxed() + } else if no_return_type { + // This is a hack; if there's no_ret_type, U is (), but serde_json gives an + // error when deserializing "" into (), so deserialize 'null' into it + // instead. + // An alternate option would be to require U: Default, and then return + // U::default() here instead since () implements that, but then we'd + // need to impl default for all models. + futures::future::ok::(serde_json::from_str("null").expect("serde null value")).boxed() + } else { + let collect = response.into_body().collect().map_err(Error::from); + collect.map(|collected| { + collected.and_then(|collected| { + serde_json::from_slice(&collected.to_bytes()).map_err(Error::from) + }) + }).boxed() + } + })) + } +} diff --git a/samples/client/others/rust/hyper/anyof/src/lib.rs b/samples/client/others/rust/hyper/anyof/src/lib.rs new file mode 100644 index 000000000000..f5cfd2315405 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/src/lib.rs @@ -0,0 +1,12 @@ +#![allow(unused_imports)] +#![allow(clippy::too_many_arguments)] + +extern crate serde_repr; +extern crate serde; +extern crate serde_json; +extern crate url; +extern crate hyper; +extern crate futures; + +pub mod apis; +pub mod models; diff --git a/samples/client/others/rust/hyper/anyof/src/models/another_any_of_test.rs b/samples/client/others/rust/hyper/anyof/src/models/another_any_of_test.rs new file mode 100644 index 000000000000..628ae6e5c66f --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/src/models/another_any_of_test.rs @@ -0,0 +1,28 @@ +/* + * Rust anyOf Test + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * The version of the OpenAPI document: 1.0.0 + * + * Generated by: https://openapi-generator.tech + */ + +use crate::models; +use serde::{Deserialize, Serialize}; + +/// AnotherAnyOfTest : Another test case with different types +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(untagged)] +pub enum AnotherAnyOfTest { + String(String), + Integer(i32), + ArrayVecString(Vec), +} + +impl Default for AnotherAnyOfTest { + fn default() -> Self { + Self::String(Default::default()) + } +} + diff --git a/samples/client/others/rust/hyper/anyof/src/models/mod.rs b/samples/client/others/rust/hyper/anyof/src/models/mod.rs new file mode 100644 index 000000000000..edc3aa046ce1 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/src/models/mod.rs @@ -0,0 +1,6 @@ +pub mod another_any_of_test; +pub use self::another_any_of_test::AnotherAnyOfTest; +pub mod model_identifier; +pub use self::model_identifier::ModelIdentifier; +pub mod test_response; +pub use self::test_response::TestResponse; diff --git a/samples/client/others/rust/hyper/anyof/src/models/model_identifier.rs b/samples/client/others/rust/hyper/anyof/src/models/model_identifier.rs new file mode 100644 index 000000000000..a43f0ed38a15 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/src/models/model_identifier.rs @@ -0,0 +1,29 @@ +/* + * Rust anyOf Test + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * The version of the OpenAPI document: 1.0.0 + * + * Generated by: https://openapi-generator.tech + */ + +use crate::models; +use serde::{Deserialize, Serialize}; + +/// ModelIdentifier : Model identifier that can be a string or specific enum value +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(untagged)] +pub enum ModelIdentifier { + /// Any model name as string + String(String), + /// Known model enum values + String(String), +} + +impl Default for ModelIdentifier { + fn default() -> Self { + Self::String(Default::default()) + } +} + diff --git a/samples/client/others/rust/hyper/anyof/src/models/test_response.rs b/samples/client/others/rust/hyper/anyof/src/models/test_response.rs new file mode 100644 index 000000000000..d6cb6e5e1fb4 --- /dev/null +++ b/samples/client/others/rust/hyper/anyof/src/models/test_response.rs @@ -0,0 +1,30 @@ +/* + * Rust anyOf Test + * + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * The version of the OpenAPI document: 1.0.0 + * + * Generated by: https://openapi-generator.tech + */ + +use crate::models; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)] +pub struct TestResponse { + #[serde(rename = "model", skip_serializing_if = "Option::is_none")] + pub model: Option>, + #[serde(rename = "status", skip_serializing_if = "Option::is_none")] + pub status: Option, +} + +impl TestResponse { + pub fn new() -> TestResponse { + TestResponse { + model: None, + status: None, + } + } +} +