[Rust Server] Handle text/xml correctly (#5660)

* [Rust Server] Handle text/xml correctly

  Treat application/xml the same as text/xml as per RFC 7303

* [Rust Server] Add test for text/xml

* Update samples
This commit is contained in:
Richard Whitehouse
2020-03-26 17:14:13 +00:00
committed by GitHub
parent 7a01062a41
commit cfe3b86d70
8 changed files with 48 additions and 11 deletions

View File

@@ -81,11 +81,15 @@ paths:
post:
requestBody:
content:
application/xml:
text/xml:
schema:
$ref: '#/components/schemas/anotherXmlObject'
responses:
"201":
content:
text/xml:
schema:
$ref: '#/components/schemas/anotherXmlObject'
description: OK
"400":
description: Bad Request

View File

@@ -185,7 +185,7 @@ No authorization required
[[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)
# ****
> (optional)
> models::AnotherXmlObject (optional)
### Required Parameters
@@ -203,7 +203,7 @@ Name | Type | Description | Notes
### Return type
(empty response body)
[**models::AnotherXmlObject**](anotherXmlObject.md)
### Authorization
@@ -211,8 +211,8 @@ No authorization required
### HTTP request headers
- **Content-Type**: application/xml
- **Accept**: Not defined
- **Content-Type**: text/xml
- **Accept**: text/xml,
[[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)

View File

@@ -936,10 +936,22 @@ impl<F, C> Api<C> for Client<F> where
201 => {
let body = response.body();
Box::new(
future::ok(
XmlOtherPostResponse::OK
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|
// ToDo: this will move to swagger-rs and become a standard From conversion trait
// once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream
serde_xml_rs::from_str::<models::AnotherXmlObject>(body)
.map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))
)
)
.map(move |body| {
XmlOtherPostResponse::OK(body)
})
) as Box<dyn Future<Item=_, Error=_>>
},
400 => {

View File

@@ -137,6 +137,7 @@ pub enum XmlExtraPostResponse {
pub enum XmlOtherPostResponse {
/// OK
OK
(models::AnotherXmlObject)
,
/// Bad Request
BadRequest

View File

@@ -50,6 +50,11 @@ pub mod responses {
pub static ref UUID_GET_DUPLICATE_RESPONSE_LONG_TEXT: Mime = "application/json".parse().unwrap();
}
lazy_static! {
/// Create Mime objects for the response content types for XmlOtherPost
pub static ref XML_OTHER_POST_OK: Mime = "text/xml".parse().unwrap();
}
}
pub mod requests {
@@ -67,7 +72,7 @@ pub mod requests {
lazy_static! {
/// Create Mime objects for the request content types for XmlOtherPost
pub static ref XML_OTHER_POST: Mime = "application/xml".parse().unwrap();
pub static ref XML_OTHER_POST: Mime = "text/xml".parse().unwrap();
}
lazy_static! {

View File

@@ -637,10 +637,19 @@ where
Ok(rsp) => match rsp {
XmlOtherPostResponse::OK
(body)
=> {
response.set_status(StatusCode::try_from(201).unwrap());
response.headers_mut().set(ContentType(mimetypes::responses::XML_OTHER_POST_OK.clone()));
let mut namespaces = BTreeMap::new();
// An empty string is used to indicate a global namespace in xmltree.
namespaces.insert("".to_string(), models::AnotherXmlObject::NAMESPACE.to_string());
let body = serde_xml_rs::to_string_with_namespaces(&body, namespaces).expect("impossible to fail to serialize");
response.set_body(body);
},
XmlOtherPostResponse::BadRequest