Skip to content

Commit cfe3b86

Browse files
authored
[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
1 parent 7a01062 commit cfe3b86

File tree

8 files changed

+48
-11
lines changed

8 files changed

+48
-11
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
6969
private static final String bytesType = "swagger::ByteArray";
7070

7171
private static final String xmlMimeType = "application/xml";
72+
private static final String textXmlMimeType = "text/xml";
7273
private static final String octetMimeType = "application/octet-stream";
7374
private static final String plainMimeType = "text/plain";
7475
private static final String jsonMimeType = "application/json";
@@ -521,7 +522,8 @@ public String escapeUnsafeCharacters(String input) {
521522
}
522523

523524
private boolean isMimetypeXml(String mimetype) {
524-
return mimetype.toLowerCase(Locale.ROOT).startsWith(xmlMimeType);
525+
return mimetype.toLowerCase(Locale.ROOT).startsWith(xmlMimeType) ||
526+
mimetype.toLowerCase(Locale.ROOT).startsWith(textXmlMimeType);
525527
}
526528

527529
private boolean isMimetypePlainText(String mimetype) {

modules/openapi-generator/src/test/resources/3_0/rust-server/openapi-v3.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,16 @@ paths:
9999
post:
100100
requestBody:
101101
content:
102-
application/xml:
102+
text/xml:
103103
schema:
104104
$ref: '#/components/schemas/anotherXmlObject'
105105
responses:
106106
'201':
107107
description: 'OK'
108+
content:
109+
text/xml:
110+
schema:
111+
$ref: "#/components/schemas/anotherXmlObject"
108112
'400':
109113
description: Bad Request
110114
put:

samples/server/petstore/rust-server/output/openapi-v3/api/openapi.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,15 @@ paths:
8181
post:
8282
requestBody:
8383
content:
84-
application/xml:
84+
text/xml:
8585
schema:
8686
$ref: '#/components/schemas/anotherXmlObject'
8787
responses:
8888
"201":
89+
content:
90+
text/xml:
91+
schema:
92+
$ref: '#/components/schemas/anotherXmlObject'
8993
description: OK
9094
"400":
9195
description: Bad Request

samples/server/petstore/rust-server/output/openapi-v3/docs/default_api.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ No authorization required
185185
[[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)
186186

187187
# ****
188-
> (optional)
188+
> models::AnotherXmlObject (optional)
189189
190190

191191
### Required Parameters
@@ -203,16 +203,16 @@ Name | Type | Description | Notes
203203

204204
### Return type
205205

206-
(empty response body)
206+
[**models::AnotherXmlObject**](anotherXmlObject.md)
207207

208208
### Authorization
209209

210210
No authorization required
211211

212212
### HTTP request headers
213213

214-
- **Content-Type**: application/xml
215-
- **Accept**: Not defined
214+
- **Content-Type**: text/xml
215+
- **Accept**: text/xml,
216216

217217
[[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)
218218

samples/server/petstore/rust-server/output/openapi-v3/src/client/mod.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -936,10 +936,22 @@ impl<F, C> Api<C> for Client<F> where
936936
201 => {
937937
let body = response.body();
938938
Box::new(
939-
940-
future::ok(
941-
XmlOtherPostResponse::OK
939+
body
940+
.concat2()
941+
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
942+
.and_then(|body|
943+
str::from_utf8(&body)
944+
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))
945+
.and_then(|body|
946+
// ToDo: this will move to swagger-rs and become a standard From conversion trait
947+
// once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream
948+
serde_xml_rs::from_str::<models::AnotherXmlObject>(body)
949+
.map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))
950+
)
942951
)
952+
.map(move |body| {
953+
XmlOtherPostResponse::OK(body)
954+
})
943955
) as Box<dyn Future<Item=_, Error=_>>
944956
},
945957
400 => {

samples/server/petstore/rust-server/output/openapi-v3/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ pub enum XmlExtraPostResponse {
137137
pub enum XmlOtherPostResponse {
138138
/// OK
139139
OK
140+
(models::AnotherXmlObject)
140141
,
141142
/// Bad Request
142143
BadRequest

samples/server/petstore/rust-server/output/openapi-v3/src/mimetypes.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ pub mod responses {
5050
pub static ref UUID_GET_DUPLICATE_RESPONSE_LONG_TEXT: Mime = "application/json".parse().unwrap();
5151
}
5252

53+
lazy_static! {
54+
/// Create Mime objects for the response content types for XmlOtherPost
55+
pub static ref XML_OTHER_POST_OK: Mime = "text/xml".parse().unwrap();
56+
}
57+
5358
}
5459

5560
pub mod requests {
@@ -67,7 +72,7 @@ pub mod requests {
6772

6873
lazy_static! {
6974
/// Create Mime objects for the request content types for XmlOtherPost
70-
pub static ref XML_OTHER_POST: Mime = "application/xml".parse().unwrap();
75+
pub static ref XML_OTHER_POST: Mime = "text/xml".parse().unwrap();
7176
}
7277

7378
lazy_static! {

samples/server/petstore/rust-server/output/openapi-v3/src/server/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,19 @@ where
637637
Ok(rsp) => match rsp {
638638
XmlOtherPostResponse::OK
639639

640+
(body)
641+
640642

641643
=> {
642644
response.set_status(StatusCode::try_from(201).unwrap());
643645

646+
response.headers_mut().set(ContentType(mimetypes::responses::XML_OTHER_POST_OK.clone()));
647+
648+
let mut namespaces = BTreeMap::new();
649+
// An empty string is used to indicate a global namespace in xmltree.
650+
namespaces.insert("".to_string(), models::AnotherXmlObject::NAMESPACE.to_string());
651+
let body = serde_xml_rs::to_string_with_namespaces(&body, namespaces).expect("impossible to fail to serialize");
652+
response.set_body(body);
644653
},
645654
XmlOtherPostResponse::BadRequest
646655

0 commit comments

Comments
 (0)