Skip to content

Commit 838f181

Browse files
authored
fix handling application/xml for body (#1504)
This includes two changes: 1. Align `to_xml` to `to_json` by returning Bytes instead of String 2. Updates generator to use `to_xml` when the content-type is set to `application/xml`
1 parent 03618cc commit 838f181

File tree

15 files changed

+79
-67
lines changed

15 files changed

+79
-67
lines changed

sdk/core/src/xml.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::error::{ErrorKind, ResultExt};
2+
use bytes::Bytes;
23
pub use quick_xml::serde_helpers::text_content;
34
use quick_xml::{
45
de::{from_reader, from_str},
@@ -32,24 +33,27 @@ where
3233
})
3334
}
3435

35-
pub fn to_xml<T>(value: &T) -> crate::Result<String>
36+
pub fn to_xml<T>(value: &T) -> crate::Result<Bytes>
3637
where
3738
T: serde::Serialize,
3839
{
39-
to_string(value).with_context(ErrorKind::DataConversion, || {
40+
let value = to_string(value).with_context(ErrorKind::DataConversion, || {
4041
let t = core::any::type_name::<T>();
4142
format!("failed to serialize {t} into xml")
42-
})
43+
})?;
44+
Ok(Bytes::from(value))
4345
}
4446

45-
pub fn to_xml_with_root<T>(root_tag: &str, value: &T) -> crate::Result<String>
47+
pub fn to_xml_with_root<T>(root_tag: &str, value: &T) -> crate::Result<Bytes>
4648
where
4749
T: serde::Serialize,
4850
{
49-
to_string_with_root(root_tag, value).with_context(ErrorKind::DataConversion, || {
50-
let t = core::any::type_name::<T>();
51-
format!("failed to serialize {t} into xml")
52-
})
51+
let value =
52+
to_string_with_root(root_tag, value).with_context(ErrorKind::DataConversion, || {
53+
let t = core::any::type_name::<T>();
54+
format!("failed to serialize {t} into xml")
55+
})?;
56+
Ok(Bytes::from(value))
5357
}
5458

5559
/// Returns bytes without the UTF-8 BOM.

sdk/storage_blobs/src/options/tags.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use azure_core::{
33
headers::{Header, HeaderName, HeaderValue, TAGS},
44
xml::to_xml,
55
};
6+
use bytes::{Bytes, BytesMut};
67
use std::{
78
collections::HashMap,
89
iter::{Extend, IntoIterator},
@@ -62,11 +63,10 @@ impl Tags {
6263
});
6364
}
6465

65-
pub fn to_xml(&self) -> azure_core::Result<String> {
66-
Ok(format!(
67-
"<?xml version=\"1.0\" encoding=\"utf-8\"?>{}",
68-
to_xml(&self)?
69-
))
66+
pub fn to_xml(&self) -> azure_core::Result<Bytes> {
67+
let mut value = BytesMut::from("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
68+
value.extend(to_xml(&self)?);
69+
Ok(value.freeze())
7070
}
7171
}
7272

sdk/storage_blobs/src/service/operations/get_user_delegation_key.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use azure_core::{
88
use azure_storage::{
99
headers::CommonStorageResponseHeaders, shared_access_signature::service_sas::UserDeligationKey,
1010
};
11+
use bytes::{Bytes, BytesMut};
1112
use time::OffsetDateTime;
1213

1314
operation! {
@@ -29,7 +30,7 @@ impl GetUserDelegationKeyBuilder {
2930
start: self.start_time,
3031
expiry: self.expiry_time,
3132
}
32-
.as_string()?;
33+
.encode()?;
3334

3435
let mut request = BlobServiceClient::finalize_request(
3536
url,
@@ -57,11 +58,10 @@ struct GetUserDelegationKeyRequest {
5758
}
5859

5960
impl GetUserDelegationKeyRequest {
60-
pub fn as_string(&self) -> azure_core::Result<String> {
61-
Ok(format!(
62-
"<?xml version=\"1.0\" encoding=\"utf-8\"?>{}",
63-
to_xml(self)?
64-
))
61+
pub fn encode(&self) -> azure_core::Result<Bytes> {
62+
let mut body = BytesMut::from("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
63+
body.extend(to_xml(self)?);
64+
Ok(body.freeze())
6565
}
6666
}
6767

@@ -89,7 +89,7 @@ mod test {
8989
use azure_core::auth::Secret;
9090
use uuid::Uuid;
9191

92-
const BASIC_REQUEST: &str = "<?xml version=\"1.0\" encoding=\"utf-8\"?><KeyInfo><Start>1970-01-01T00:00:00Z</Start><Expiry>1970-01-01T00:00:01Z</Expiry></KeyInfo>";
92+
const BASIC_REQUEST: &[u8] = b"<?xml version=\"1.0\" encoding=\"utf-8\"?><KeyInfo><Start>1970-01-01T00:00:00Z</Start><Expiry>1970-01-01T00:00:01Z</Expiry></KeyInfo>";
9393
const BASIC_RESPONSE: &str = "
9494
<UserDeligationKey>
9595
<SignedOid>00000000-0000-0000-0000-000000000000</SignedOid>
@@ -108,7 +108,7 @@ mod test {
108108
start: OffsetDateTime::from_unix_timestamp(0).unwrap(),
109109
expiry: OffsetDateTime::from_unix_timestamp(1).unwrap(),
110110
}
111-
.as_string()?;
111+
.encode()?;
112112
assert_eq!(BASIC_REQUEST, request);
113113
Ok(())
114114
}

services/autorust/codegen/src/codegen_operations/set_request_param_code.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,25 @@ impl ToTokens for SetRequestParamsCode {
109109
quote! {}
110110
};
111111

112+
// TODO: more work needs to be done to ensure we're using
113+
// the right encoder.
114+
let encoder = if !self.params.has_content_type_header() && self.content_type.starts_with("application/xml") {
115+
quote! {azure_core::xml::to_xml}
116+
} else {
117+
quote! { azure_core::to_json }
118+
};
119+
112120
if !param.is_optional() || is_vec {
113121
tokens.extend(quote! {
114122
#set_content_type
115-
let req_body = azure_core::to_json(&this.#param_name_var)?;
123+
let req_body = #encoder(&this.#param_name_var)?;
116124
});
117125
} else {
118126
tokens.extend(quote! {
119127
let req_body =
120128
if let Some(#param_name_var) = &this.#param_name_var {
121129
#set_content_type
122-
azure_core::to_json(#param_name_var)?
130+
#encoder(#param_name_var)?
123131
} else {
124132
azure_core::EMPTY_BODY
125133
};

services/svc/blobstorage/src/package_2020_12/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ pub mod service {
453453
req.insert_header(azure_core::headers::AUTHORIZATION, format!("Bearer {}", bearer_token.secret()));
454454
req.insert_header(azure_core::headers::VERSION, "2020-12-06");
455455
req.insert_header("content-type", "application/xml");
456-
let req_body = azure_core::to_json(&this.storage_service_properties)?;
456+
let req_body = azure_core::xml::to_xml(&this.storage_service_properties)?;
457457
if let Some(timeout) = &this.timeout {
458458
req.url_mut().query_pairs_mut().append_pair("timeout", &timeout.to_string());
459459
}
@@ -864,7 +864,7 @@ pub mod service {
864864
req.insert_header(azure_core::headers::AUTHORIZATION, format!("Bearer {}", bearer_token.secret()));
865865
req.insert_header(azure_core::headers::VERSION, "2020-12-06");
866866
req.insert_header("content-type", "application/xml");
867-
let req_body = azure_core::to_json(&this.key_info)?;
867+
let req_body = azure_core::xml::to_xml(&this.key_info)?;
868868
if let Some(timeout) = &this.timeout {
869869
req.url_mut().query_pairs_mut().append_pair("timeout", &timeout.to_string());
870870
}
@@ -2541,7 +2541,7 @@ pub mod container {
25412541
req.insert_header(azure_core::headers::VERSION, "2020-12-06");
25422542
let req_body = if let Some(container_acl) = &this.container_acl {
25432543
req.insert_header("content-type", "application/xml");
2544-
azure_core::to_json(container_acl)?
2544+
azure_core::xml::to_xml(container_acl)?
25452545
} else {
25462546
azure_core::EMPTY_BODY
25472547
};
@@ -9613,7 +9613,7 @@ pub mod blob {
96139613
req.insert_header(azure_core::headers::VERSION, "2020-12-06");
96149614
let req_body = if let Some(query_request) = &this.query_request {
96159615
req.insert_header("content-type", "application/xml");
9616-
azure_core::to_json(query_request)?
9616+
azure_core::xml::to_xml(query_request)?
96179617
} else {
96189618
azure_core::EMPTY_BODY
96199619
};
@@ -10014,7 +10014,7 @@ pub mod blob {
1001410014
}
1001510015
let req_body = if let Some(tags) = &this.tags {
1001610016
req.insert_header("content-type", "application/xml");
10017-
azure_core::to_json(tags)?
10017+
azure_core::xml::to_xml(tags)?
1001810018
} else {
1001910019
azure_core::EMPTY_BODY
1002010020
};
@@ -16168,7 +16168,7 @@ pub mod block_blob {
1616816168
req.insert_header("x-ms-if-tags", x_ms_if_tags);
1616916169
}
1617016170
req.insert_header("content-type", "application/xml");
16171-
let req_body = azure_core::to_json(&this.blocks)?;
16171+
let req_body = azure_core::xml::to_xml(&this.blocks)?;
1617216172
if let Some(x_ms_client_request_id) = &this.x_ms_client_request_id {
1617316173
req.insert_header("x-ms-client-request-id", x_ms_client_request_id);
1617416174
}

services/svc/blobstorage/src/package_2021_02/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ pub mod service {
453453
req.insert_header(azure_core::headers::AUTHORIZATION, format!("Bearer {}", bearer_token.secret()));
454454
req.insert_header(azure_core::headers::VERSION, "2021-02-12");
455455
req.insert_header("content-type", "application/xml");
456-
let req_body = azure_core::to_json(&this.storage_service_properties)?;
456+
let req_body = azure_core::xml::to_xml(&this.storage_service_properties)?;
457457
if let Some(timeout) = &this.timeout {
458458
req.url_mut().query_pairs_mut().append_pair("timeout", &timeout.to_string());
459459
}
@@ -864,7 +864,7 @@ pub mod service {
864864
req.insert_header(azure_core::headers::AUTHORIZATION, format!("Bearer {}", bearer_token.secret()));
865865
req.insert_header(azure_core::headers::VERSION, "2021-02-12");
866866
req.insert_header("content-type", "application/xml");
867-
let req_body = azure_core::to_json(&this.key_info)?;
867+
let req_body = azure_core::xml::to_xml(&this.key_info)?;
868868
if let Some(timeout) = &this.timeout {
869869
req.url_mut().query_pairs_mut().append_pair("timeout", &timeout.to_string());
870870
}
@@ -2541,7 +2541,7 @@ pub mod container {
25412541
req.insert_header(azure_core::headers::VERSION, "2021-02-12");
25422542
let req_body = if let Some(container_acl) = &this.container_acl {
25432543
req.insert_header("content-type", "application/xml");
2544-
azure_core::to_json(container_acl)?
2544+
azure_core::xml::to_xml(container_acl)?
25452545
} else {
25462546
azure_core::EMPTY_BODY
25472547
};
@@ -9613,7 +9613,7 @@ pub mod blob {
96139613
req.insert_header(azure_core::headers::VERSION, "2021-02-12");
96149614
let req_body = if let Some(query_request) = &this.query_request {
96159615
req.insert_header("content-type", "application/xml");
9616-
azure_core::to_json(query_request)?
9616+
azure_core::xml::to_xml(query_request)?
96179617
} else {
96189618
azure_core::EMPTY_BODY
96199619
};
@@ -10014,7 +10014,7 @@ pub mod blob {
1001410014
}
1001510015
let req_body = if let Some(tags) = &this.tags {
1001610016
req.insert_header("content-type", "application/xml");
10017-
azure_core::to_json(tags)?
10017+
azure_core::xml::to_xml(tags)?
1001810018
} else {
1001910019
azure_core::EMPTY_BODY
1002010020
};
@@ -16168,7 +16168,7 @@ pub mod block_blob {
1616816168
req.insert_header("x-ms-if-tags", x_ms_if_tags);
1616916169
}
1617016170
req.insert_header("content-type", "application/xml");
16171-
let req_body = azure_core::to_json(&this.blocks)?;
16171+
let req_body = azure_core::xml::to_xml(&this.blocks)?;
1617216172
if let Some(x_ms_client_request_id) = &this.x_ms_client_request_id {
1617316173
req.insert_header("x-ms-client-request-id", x_ms_client_request_id);
1617416174
}

services/svc/blobstorage/src/package_2021_04/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ pub mod service {
453453
req.insert_header(azure_core::headers::AUTHORIZATION, format!("Bearer {}", bearer_token.secret()));
454454
req.insert_header(azure_core::headers::VERSION, "2021-04-10");
455455
req.insert_header("content-type", "application/xml");
456-
let req_body = azure_core::to_json(&this.storage_service_properties)?;
456+
let req_body = azure_core::xml::to_xml(&this.storage_service_properties)?;
457457
if let Some(timeout) = &this.timeout {
458458
req.url_mut().query_pairs_mut().append_pair("timeout", &timeout.to_string());
459459
}
@@ -864,7 +864,7 @@ pub mod service {
864864
req.insert_header(azure_core::headers::AUTHORIZATION, format!("Bearer {}", bearer_token.secret()));
865865
req.insert_header(azure_core::headers::VERSION, "2021-04-10");
866866
req.insert_header("content-type", "application/xml");
867-
let req_body = azure_core::to_json(&this.key_info)?;
867+
let req_body = azure_core::xml::to_xml(&this.key_info)?;
868868
if let Some(timeout) = &this.timeout {
869869
req.url_mut().query_pairs_mut().append_pair("timeout", &timeout.to_string());
870870
}
@@ -2556,7 +2556,7 @@ pub mod container {
25562556
req.insert_header(azure_core::headers::VERSION, "2021-04-10");
25572557
let req_body = if let Some(container_acl) = &this.container_acl {
25582558
req.insert_header("content-type", "application/xml");
2559-
azure_core::to_json(container_acl)?
2559+
azure_core::xml::to_xml(container_acl)?
25602560
} else {
25612561
azure_core::EMPTY_BODY
25622562
};
@@ -9802,7 +9802,7 @@ pub mod blob {
98029802
req.insert_header(azure_core::headers::VERSION, "2021-04-10");
98039803
let req_body = if let Some(query_request) = &this.query_request {
98049804
req.insert_header("content-type", "application/xml");
9805-
azure_core::to_json(query_request)?
9805+
azure_core::xml::to_xml(query_request)?
98069806
} else {
98079807
azure_core::EMPTY_BODY
98089808
};
@@ -10203,7 +10203,7 @@ pub mod blob {
1020310203
}
1020410204
let req_body = if let Some(tags) = &this.tags {
1020510205
req.insert_header("content-type", "application/xml");
10206-
azure_core::to_json(tags)?
10206+
azure_core::xml::to_xml(tags)?
1020710207
} else {
1020810208
azure_core::EMPTY_BODY
1020910209
};
@@ -16367,7 +16367,7 @@ pub mod block_blob {
1636716367
req.insert_header("x-ms-if-tags", x_ms_if_tags);
1636816368
}
1636916369
req.insert_header("content-type", "application/xml");
16370-
let req_body = azure_core::to_json(&this.blocks)?;
16370+
let req_body = azure_core::xml::to_xml(&this.blocks)?;
1637116371
if let Some(x_ms_client_request_id) = &this.x_ms_client_request_id {
1637216372
req.insert_header("x-ms-client-request-id", x_ms_client_request_id);
1637316373
}

services/svc/blobstorage/src/package_2021_08/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ pub mod service {
454454
req.insert_header(azure_core::headers::AUTHORIZATION, format!("Bearer {}", bearer_token.secret()));
455455
req.insert_header(azure_core::headers::VERSION, "2021-08-06");
456456
req.insert_header("content-type", "application/xml");
457-
let req_body = azure_core::to_json(&this.storage_service_properties)?;
457+
let req_body = azure_core::xml::to_xml(&this.storage_service_properties)?;
458458
if let Some(timeout) = &this.timeout {
459459
req.url_mut().query_pairs_mut().append_pair("timeout", &timeout.to_string());
460460
}
@@ -865,7 +865,7 @@ pub mod service {
865865
req.insert_header(azure_core::headers::AUTHORIZATION, format!("Bearer {}", bearer_token.secret()));
866866
req.insert_header(azure_core::headers::VERSION, "2021-08-06");
867867
req.insert_header("content-type", "application/xml");
868-
let req_body = azure_core::to_json(&this.key_info)?;
868+
let req_body = azure_core::xml::to_xml(&this.key_info)?;
869869
if let Some(timeout) = &this.timeout {
870870
req.url_mut().query_pairs_mut().append_pair("timeout", &timeout.to_string());
871871
}
@@ -2564,7 +2564,7 @@ pub mod container {
25642564
req.insert_header(azure_core::headers::VERSION, "2021-08-06");
25652565
let req_body = if let Some(container_acl) = &this.container_acl {
25662566
req.insert_header("content-type", "application/xml");
2567-
azure_core::to_json(container_acl)?
2567+
azure_core::xml::to_xml(container_acl)?
25682568
} else {
25692569
azure_core::EMPTY_BODY
25702570
};
@@ -9816,7 +9816,7 @@ pub mod blob {
98169816
req.insert_header(azure_core::headers::VERSION, "2021-08-06");
98179817
let req_body = if let Some(query_request) = &this.query_request {
98189818
req.insert_header("content-type", "application/xml");
9819-
azure_core::to_json(query_request)?
9819+
azure_core::xml::to_xml(query_request)?
98209820
} else {
98219821
azure_core::EMPTY_BODY
98229822
};
@@ -10217,7 +10217,7 @@ pub mod blob {
1021710217
}
1021810218
let req_body = if let Some(tags) = &this.tags {
1021910219
req.insert_header("content-type", "application/xml");
10220-
azure_core::to_json(tags)?
10220+
azure_core::xml::to_xml(tags)?
1022110221
} else {
1022210222
azure_core::EMPTY_BODY
1022310223
};
@@ -16391,7 +16391,7 @@ pub mod block_blob {
1639116391
req.insert_header("x-ms-if-tags", x_ms_if_tags);
1639216392
}
1639316393
req.insert_header("content-type", "application/xml");
16394-
let req_body = azure_core::to_json(&this.blocks)?;
16394+
let req_body = azure_core::xml::to_xml(&this.blocks)?;
1639516395
if let Some(x_ms_client_request_id) = &this.x_ms_client_request_id {
1639616396
req.insert_header("x-ms-client-request-id", x_ms_client_request_id);
1639716397
}

0 commit comments

Comments
 (0)