forked from loafle/openapi-generator-original
[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:
parent
7a01062a41
commit
cfe3b86d70
@ -69,6 +69,7 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
private static final String bytesType = "swagger::ByteArray";
|
||||
|
||||
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 plainMimeType = "text/plain";
|
||||
private static final String jsonMimeType = "application/json";
|
||||
@ -521,7 +522,8 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -99,12 +99,16 @@ paths:
|
||||
post:
|
||||
requestBody:
|
||||
content:
|
||||
application/xml:
|
||||
text/xml:
|
||||
schema:
|
||||
$ref: '#/components/schemas/anotherXmlObject'
|
||||
responses:
|
||||
'201':
|
||||
description: 'OK'
|
||||
content:
|
||||
text/xml:
|
||||
schema:
|
||||
$ref: "#/components/schemas/anotherXmlObject"
|
||||
'400':
|
||||
description: Bad Request
|
||||
put:
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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 => {
|
||||
|
@ -137,6 +137,7 @@ pub enum XmlExtraPostResponse {
|
||||
pub enum XmlOtherPostResponse {
|
||||
/// OK
|
||||
OK
|
||||
(models::AnotherXmlObject)
|
||||
,
|
||||
/// Bad Request
|
||||
BadRequest
|
||||
|
@ -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! {
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user