[Rust Server] Support operation overriding server path on client (#5554)

* [Rust Server] Support operation overriding server on the client

* [Rust Server] Test operation overriding sever

* Update samples
This commit is contained in:
Richard Whitehouse 2020-03-24 16:41:11 +00:00 committed by GitHub
parent 05ace4f43f
commit cff1f1ce80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 203 additions and 12 deletions

View File

@ -15,7 +15,7 @@
"{{vendorExtensions.x-path-format-string}}" "{{vendorExtensions.x-path-format-string}}"
{{/isCallbackRequest}} {{/isCallbackRequest}}
{{^isCallbackRequest}} {{^isCallbackRequest}}
"{}{{{basePathWithoutHost}}}{{vendorExtensions.x-path-format-string}}", "{}{{^servers}}{{{basePathWithoutHost}}}{{/servers}}{{#servers.0}}{{{url}}}{{/servers.0}}{{vendorExtensions.x-path-format-string}}",
self.base_path self.base_path
{{/isCallbackRequest}} {{/isCallbackRequest}}
{{#pathParams}} {{#pathParams}}

View File

@ -330,6 +330,13 @@ paths:
responses: responses:
'200': '200':
description: Success description: Success
/override-server:
get:
servers:
- "url": "/override"
responses:
"204":
description: Success.
components: components:
securitySchemes: securitySchemes:

View File

@ -66,6 +66,7 @@ cargo run --example client MandatoryRequestHeaderGet
cargo run --example client MergePatchJsonGet cargo run --example client MergePatchJsonGet
cargo run --example client MultigetGet cargo run --example client MultigetGet
cargo run --example client MultipleAuthSchemeGet cargo run --example client MultipleAuthSchemeGet
cargo run --example client OverrideServerGet
cargo run --example client ParamgetGet cargo run --example client ParamgetGet
cargo run --example client ReadonlyAuthSchemeGet cargo run --example client ReadonlyAuthSchemeGet
cargo run --example client RegisterCallbackPost cargo run --example client RegisterCallbackPost
@ -118,6 +119,7 @@ Method | HTTP request | Description
[****](docs/default_api.md#) | **GET** /merge-patch-json | [****](docs/default_api.md#) | **GET** /merge-patch-json |
[****](docs/default_api.md#) | **GET** /multiget | Get some stuff. [****](docs/default_api.md#) | **GET** /multiget | Get some stuff.
[****](docs/default_api.md#) | **GET** /multiple_auth_scheme | [****](docs/default_api.md#) | **GET** /multiple_auth_scheme |
[****](docs/default_api.md#) | **GET** /override-server |
[****](docs/default_api.md#) | **GET** /paramget | Get some stuff with parameters. [****](docs/default_api.md#) | **GET** /paramget | Get some stuff with parameters.
[****](docs/default_api.md#) | **GET** /readonly_auth_scheme | [****](docs/default_api.md#) | **GET** /readonly_auth_scheme |
[****](docs/default_api.md#) | **POST** /register-callback | [****](docs/default_api.md#) | **POST** /register-callback |

View File

@ -338,6 +338,13 @@ paths:
responses: responses:
"200": "200":
description: Success description: Success
/override-server:
get:
responses:
"204":
description: Success.
servers:
- url: /override
components: components:
schemas: schemas:
EnumWithStarObject: EnumWithStarObject:

View File

@ -10,6 +10,7 @@ Method | HTTP request | Description
****](default_api.md#) | **GET** /merge-patch-json | ****](default_api.md#) | **GET** /merge-patch-json |
****](default_api.md#) | **GET** /multiget | Get some stuff. ****](default_api.md#) | **GET** /multiget | Get some stuff.
****](default_api.md#) | **GET** /multiple_auth_scheme | ****](default_api.md#) | **GET** /multiple_auth_scheme |
****](default_api.md#) | **GET** /override-server |
****](default_api.md#) | **GET** /paramget | Get some stuff with parameters. ****](default_api.md#) | **GET** /paramget | Get some stuff with parameters.
****](default_api.md#) | **GET** /readonly_auth_scheme | ****](default_api.md#) | **GET** /readonly_auth_scheme |
****](default_api.md#) | **POST** /register-callback | ****](default_api.md#) | **POST** /register-callback |
@ -166,6 +167,28 @@ This endpoint does not need any parameter.
[[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) [[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)
# ****
> ()
### Required Parameters
This endpoint does not need any parameter.
### Return type
(empty response body)
### Authorization
No authorization required
### HTTP request headers
- **Content-Type**: Not defined
- **Accept**: Not defined
[[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)
# **** # ****
> models::AnotherXmlObject (optional) > models::AnotherXmlObject (optional)
Get some stuff with parameters. Get some stuff with parameters.

View File

@ -35,6 +35,7 @@ use openapi_v3::{Api, ApiNoContext, Client, ContextWrapperExt,
MergePatchJsonGetResponse, MergePatchJsonGetResponse,
MultigetGetResponse, MultigetGetResponse,
MultipleAuthSchemeGetResponse, MultipleAuthSchemeGetResponse,
OverrideServerGetResponse,
ParamgetGetResponse, ParamgetGetResponse,
ReadonlyAuthSchemeGetResponse, ReadonlyAuthSchemeGetResponse,
RegisterCallbackPostResponse, RegisterCallbackPostResponse,
@ -69,6 +70,7 @@ fn main() {
"MergePatchJsonGet", "MergePatchJsonGet",
"MultigetGet", "MultigetGet",
"MultipleAuthSchemeGet", "MultipleAuthSchemeGet",
"OverrideServerGet",
"ParamgetGet", "ParamgetGet",
"ReadonlyAuthSchemeGet", "ReadonlyAuthSchemeGet",
"RegisterCallbackPost", "RegisterCallbackPost",
@ -163,6 +165,11 @@ fn main() {
)); ));
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone()); info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
}, },
Some("OverrideServerGet") => {
let result = rt.block_on(client.override_server_get(
));
info!("{:?} (X-Span-ID: {:?})", result, (client.context() as &Has<XSpanIdString>).get().clone());
},
Some("ParamgetGet") => { Some("ParamgetGet") => {
let result = rt.block_on(client.paramget_get( let result = rt.block_on(client.paramget_get(
Some(serde_json::from_str::<uuid::Uuid>("38400000-8cf0-11bd-b23e-10b96e4ef00d").expect("Failed to parse JSON example")), Some(serde_json::from_str::<uuid::Uuid>("38400000-8cf0-11bd-b23e-10b96e4ef00d").expect("Failed to parse JSON example")),

View File

@ -112,6 +112,7 @@ use openapi_v3::{
MergePatchJsonGetResponse, MergePatchJsonGetResponse,
MultigetGetResponse, MultigetGetResponse,
MultipleAuthSchemeGetResponse, MultipleAuthSchemeGetResponse,
OverrideServerGetResponse,
ParamgetGetResponse, ParamgetGetResponse,
ReadonlyAuthSchemeGetResponse, ReadonlyAuthSchemeGetResponse,
RegisterCallbackPostResponse, RegisterCallbackPostResponse,
@ -187,6 +188,15 @@ impl<C> Api<C> for Server<C> where C: Has<XSpanIdString>{
Box::new(future::err("Generic failure".into())) Box::new(future::err("Generic failure".into()))
} }
fn override_server_get(
&self,
context: &C) -> Box<Future<Item=OverrideServerGetResponse, Error=ApiError> + Send>
{
let context = context.clone();
info!("override_server_get() - X-Span-ID: {:?}", context.get().0.clone());
Box::new(future::err("Generic failure".into()))
}
/// Get some stuff with parameters. /// Get some stuff with parameters.
fn paramget_get( fn paramget_get(
&self, &self,

View File

@ -43,6 +43,7 @@ use {Api,
MergePatchJsonGetResponse, MergePatchJsonGetResponse,
MultigetGetResponse, MultigetGetResponse,
MultipleAuthSchemeGetResponse, MultipleAuthSchemeGetResponse,
OverrideServerGetResponse,
ParamgetGetResponse, ParamgetGetResponse,
ReadonlyAuthSchemeGetResponse, ReadonlyAuthSchemeGetResponse,
RegisterCallbackPostResponse, RegisterCallbackPostResponse,
@ -867,6 +868,77 @@ impl<C, F> Api<C> for Client<F> where
})) }))
} }
fn override_server_get(
&self,
context: &C) -> Box<dyn Future<Item=OverrideServerGetResponse, Error=ApiError> + Send>
{
let mut uri = format!(
"{}/override/override-server",
self.base_path
);
// Query parameters
let mut query_string = url::form_urlencoded::Serializer::new("".to_owned());
let query_string_str = query_string.finish();
if !query_string_str.is_empty() {
uri += "?";
uri += &query_string_str;
}
let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Box::new(future::err(ApiError(format!("Unable to build URI: {}", err)))),
};
let mut request = match hyper::Request::builder()
.method("GET")
.uri(uri)
.body(Body::empty()) {
Ok(req) => req,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create request: {}", e))))
};
let header = HeaderValue::from_str((context as &dyn Has<XSpanIdString>).get().0.clone().to_string().as_str());
request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header {
Ok(h) => h,
Err(e) => return Box::new(future::err(ApiError(format!("Unable to create X-Span ID header value: {}", e))))
});
Box::new(self.client_service.request(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
match response.status().as_u16() {
204 => {
let body = response.into_body();
Box::new(
future::ok(
OverrideServerGetResponse::Success
)
) as Box<dyn Future<Item=_, Error=_> + Send>
},
code => {
let headers = response.headers().clone();
Box::new(response.into_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!("<Body was not UTF8: {:?}>", e)),
},
Err(e) => Cow::from(format!("<Failed to read body: {}>", e)),
})))
)
) as Box<dyn Future<Item=_, Error=_> + Send>
}
}
}))
}
fn paramget_get( fn paramget_get(
&self, &self,
param_uuid: Option<uuid::Uuid>, param_uuid: Option<uuid::Uuid>,

View File

@ -130,6 +130,12 @@ pub enum MultipleAuthSchemeGetResponse {
CheckThatLimitingToMultipleRequiredAuthSchemesWorks CheckThatLimitingToMultipleRequiredAuthSchemesWorks
} }
#[derive(Debug, PartialEq)]
pub enum OverrideServerGetResponse {
/// Success.
Success
}
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum ParamgetGetResponse { pub enum ParamgetGetResponse {
/// JSON rsp /// JSON rsp
@ -283,6 +289,10 @@ pub trait Api<C> {
&self, &self,
context: &C) -> Box<dyn Future<Item=MultipleAuthSchemeGetResponse, Error=ApiError> + Send>; context: &C) -> Box<dyn Future<Item=MultipleAuthSchemeGetResponse, Error=ApiError> + Send>;
fn override_server_get(
&self,
context: &C) -> Box<dyn Future<Item=OverrideServerGetResponse, Error=ApiError> + Send>;
/// Get some stuff with parameters. /// Get some stuff with parameters.
fn paramget_get( fn paramget_get(
&self, &self,
@ -380,6 +390,10 @@ pub trait ApiNoContext {
&self, &self,
) -> Box<dyn Future<Item=MultipleAuthSchemeGetResponse, Error=ApiError> + Send>; ) -> Box<dyn Future<Item=MultipleAuthSchemeGetResponse, Error=ApiError> + Send>;
fn override_server_get(
&self,
) -> Box<dyn Future<Item=OverrideServerGetResponse, Error=ApiError> + Send>;
/// Get some stuff with parameters. /// Get some stuff with parameters.
fn paramget_get( fn paramget_get(
&self, &self,
@ -506,6 +520,13 @@ impl<'a, T: Api<C>, C> ApiNoContext for ContextWrapper<'a, T, C> {
self.api().multiple_auth_scheme_get(&self.context()) self.api().multiple_auth_scheme_get(&self.context())
} }
fn override_server_get(
&self,
) -> Box<dyn Future<Item=OverrideServerGetResponse, Error=ApiError> + Send>
{
self.api().override_server_get(&self.context())
}
/// Get some stuff with parameters. /// Get some stuff with parameters.
fn paramget_get( fn paramget_get(
&self, &self,

View File

@ -30,6 +30,7 @@ use {Api,
MergePatchJsonGetResponse, MergePatchJsonGetResponse,
MultigetGetResponse, MultigetGetResponse,
MultipleAuthSchemeGetResponse, MultipleAuthSchemeGetResponse,
OverrideServerGetResponse,
ParamgetGetResponse, ParamgetGetResponse,
ReadonlyAuthSchemeGetResponse, ReadonlyAuthSchemeGetResponse,
RegisterCallbackPostResponse, RegisterCallbackPostResponse,
@ -58,6 +59,7 @@ mod paths {
r"^/merge-patch-json$", r"^/merge-patch-json$",
r"^/multiget$", r"^/multiget$",
r"^/multiple_auth_scheme$", r"^/multiple_auth_scheme$",
r"^/override-server$",
r"^/paramget$", r"^/paramget$",
r"^/readonly_auth_scheme$", r"^/readonly_auth_scheme$",
r"^/register-callback$", r"^/register-callback$",
@ -83,17 +85,18 @@ mod paths {
pub static ID_MERGE_PATCH_JSON: usize = 3; pub static ID_MERGE_PATCH_JSON: usize = 3;
pub static ID_MULTIGET: usize = 4; pub static ID_MULTIGET: usize = 4;
pub static ID_MULTIPLE_AUTH_SCHEME: usize = 5; pub static ID_MULTIPLE_AUTH_SCHEME: usize = 5;
pub static ID_PARAMGET: usize = 6; pub static ID_OVERRIDE_SERVER: usize = 6;
pub static ID_READONLY_AUTH_SCHEME: usize = 7; pub static ID_PARAMGET: usize = 7;
pub static ID_REGISTER_CALLBACK: usize = 8; pub static ID_READONLY_AUTH_SCHEME: usize = 8;
pub static ID_REQUIRED_OCTET_STREAM: usize = 9; pub static ID_REGISTER_CALLBACK: usize = 9;
pub static ID_RESPONSES_WITH_HEADERS: usize = 10; pub static ID_REQUIRED_OCTET_STREAM: usize = 10;
pub static ID_RFC7807: usize = 11; pub static ID_RESPONSES_WITH_HEADERS: usize = 11;
pub static ID_UNTYPED_PROPERTY: usize = 12; pub static ID_RFC7807: usize = 12;
pub static ID_UUID: usize = 13; pub static ID_UNTYPED_PROPERTY: usize = 13;
pub static ID_XML: usize = 14; pub static ID_UUID: usize = 14;
pub static ID_XML_EXTRA: usize = 15; pub static ID_XML: usize = 15;
pub static ID_XML_OTHER: usize = 16; pub static ID_XML_EXTRA: usize = 16;
pub static ID_XML_OTHER: usize = 17;
} }
pub struct MakeService<T, RC> { pub struct MakeService<T, RC> {
@ -561,6 +564,42 @@ where
}) as Self::Future }) as Self::Future
}, },
// OverrideServerGet - GET /override-server
&hyper::Method::GET if path.matched(paths::ID_OVERRIDE_SERVER) => {
Box::new({
{{
Box::new(
api_impl.override_server_get(
&context
).then(move |result| {
let mut response = Response::new(Body::empty());
response.headers_mut().insert(
HeaderName::from_static("x-span-id"),
HeaderValue::from_str((&context as &dyn Has<XSpanIdString>).get().0.clone().to_string().as_str())
.expect("Unable to create X-Span-ID header value"));
match result {
Ok(rsp) => match rsp {
OverrideServerGetResponse::Success
=> {
*response.status_mut() = StatusCode::from_u16(204).expect("Unable to turn 204 into a StatusCode");
},
},
Err(_) => {
// Application code returned an error. This should not happen, as the implementation should
// return a valid response.
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
*response.body_mut() = Body::from("An internal error occurred");
},
}
future::ok(response)
}
))
}}
}) as Self::Future
},
// ParamgetGet - GET /paramget // ParamgetGet - GET /paramget
&hyper::Method::GET if path.matched(paths::ID_PARAMGET) => { &hyper::Method::GET if path.matched(paths::ID_PARAMGET) => {
// Query parameters (note that non-required or collection query parameters will ignore garbage values, rather than causing a 400 response) // Query parameters (note that non-required or collection query parameters will ignore garbage values, rather than causing a 400 response)
@ -1419,6 +1458,7 @@ where
_ if path.matched(paths::ID_MERGE_PATCH_JSON) => method_not_allowed(), _ if path.matched(paths::ID_MERGE_PATCH_JSON) => method_not_allowed(),
_ if path.matched(paths::ID_MULTIGET) => method_not_allowed(), _ if path.matched(paths::ID_MULTIGET) => method_not_allowed(),
_ if path.matched(paths::ID_MULTIPLE_AUTH_SCHEME) => method_not_allowed(), _ if path.matched(paths::ID_MULTIPLE_AUTH_SCHEME) => method_not_allowed(),
_ if path.matched(paths::ID_OVERRIDE_SERVER) => method_not_allowed(),
_ if path.matched(paths::ID_PARAMGET) => method_not_allowed(), _ if path.matched(paths::ID_PARAMGET) => method_not_allowed(),
_ if path.matched(paths::ID_READONLY_AUTH_SCHEME) => method_not_allowed(), _ if path.matched(paths::ID_READONLY_AUTH_SCHEME) => method_not_allowed(),
_ if path.matched(paths::ID_REGISTER_CALLBACK) => method_not_allowed(), _ if path.matched(paths::ID_REGISTER_CALLBACK) => method_not_allowed(),
@ -1467,6 +1507,8 @@ impl<T> RequestParser<T> for ApiRequestParser {
&hyper::Method::GET if path.matched(paths::ID_MULTIGET) => Ok("MultigetGet"), &hyper::Method::GET if path.matched(paths::ID_MULTIGET) => Ok("MultigetGet"),
// MultipleAuthSchemeGet - GET /multiple_auth_scheme // MultipleAuthSchemeGet - GET /multiple_auth_scheme
&hyper::Method::GET if path.matched(paths::ID_MULTIPLE_AUTH_SCHEME) => Ok("MultipleAuthSchemeGet"), &hyper::Method::GET if path.matched(paths::ID_MULTIPLE_AUTH_SCHEME) => Ok("MultipleAuthSchemeGet"),
// OverrideServerGet - GET /override-server
&hyper::Method::GET if path.matched(paths::ID_OVERRIDE_SERVER) => Ok("OverrideServerGet"),
// ParamgetGet - GET /paramget // ParamgetGet - GET /paramget
&hyper::Method::GET if path.matched(paths::ID_PARAMGET) => Ok("ParamgetGet"), &hyper::Method::GET if path.matched(paths::ID_PARAMGET) => Ok("ParamgetGet"),
// ReadonlyAuthSchemeGet - GET /readonly_auth_scheme // ReadonlyAuthSchemeGet - GET /readonly_auth_scheme