[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 48 additions and 11 deletions

View File

@ -69,6 +69,7 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
private static final String bytesType = "swagger::ByteArray"; private static final String bytesType = "swagger::ByteArray";
private static final String xmlMimeType = "application/xml"; private static final String xmlMimeType = "application/xml";
private static final String textXmlMimeType = "text/xml";
private static final String octetMimeType = "application/octet-stream"; private static final String octetMimeType = "application/octet-stream";
private static final String plainMimeType = "text/plain"; private static final String plainMimeType = "text/plain";
private static final String jsonMimeType = "application/json"; private static final String jsonMimeType = "application/json";
@ -521,7 +522,8 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
} }
private boolean isMimetypeXml(String mimetype) { private boolean isMimetypeXml(String mimetype) {
return mimetype.toLowerCase(Locale.ROOT).startsWith(xmlMimeType); return mimetype.toLowerCase(Locale.ROOT).startsWith(xmlMimeType) ||
mimetype.toLowerCase(Locale.ROOT).startsWith(textXmlMimeType);
} }
private boolean isMimetypePlainText(String mimetype) { private boolean isMimetypePlainText(String mimetype) {

View File

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

View File

@ -81,11 +81,15 @@ paths:
post: post:
requestBody: requestBody:
content: content:
application/xml: text/xml:
schema: schema:
$ref: '#/components/schemas/anotherXmlObject' $ref: '#/components/schemas/anotherXmlObject'
responses: responses:
"201": "201":
content:
text/xml:
schema:
$ref: '#/components/schemas/anotherXmlObject'
description: OK description: OK
"400": "400":
description: Bad Request 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) [[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 ### Required Parameters
@ -203,7 +203,7 @@ Name | Type | Description | Notes
### Return type ### Return type
(empty response body) [**models::AnotherXmlObject**](anotherXmlObject.md)
### Authorization ### Authorization
@ -211,8 +211,8 @@ No authorization required
### HTTP request headers ### HTTP request headers
- **Content-Type**: application/xml - **Content-Type**: text/xml
- **Accept**: Not defined - **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) [[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 => { 201 => {
let body = response.body(); let body = response.body();
Box::new( Box::new(
body
future::ok( .concat2()
XmlOtherPostResponse::OK .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=_>> ) as Box<dyn Future<Item=_, Error=_>>
}, },
400 => { 400 => {

View File

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

View File

@ -50,6 +50,11 @@ pub mod responses {
pub static ref UUID_GET_DUPLICATE_RESPONSE_LONG_TEXT: Mime = "application/json".parse().unwrap(); 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 { pub mod requests {
@ -67,7 +72,7 @@ pub mod requests {
lazy_static! { lazy_static! {
/// Create Mime objects for the request content types for XmlOtherPost /// 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! { lazy_static! {

View File

@ -637,10 +637,19 @@ where
Ok(rsp) => match rsp { Ok(rsp) => match rsp {
XmlOtherPostResponse::OK XmlOtherPostResponse::OK
(body)
=> { => {
response.set_status(StatusCode::try_from(201).unwrap()); 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 XmlOtherPostResponse::BadRequest