Skip to content

Commit 43b0bc6

Browse files
author
daniel.eades
committed
fix(httpmock): handle string types in body parameters
String types (`&str`) don't implement `Deserialize` and are unsized, which causes `json_body_obj` to fail. Convert to owned `String` before passing to httpmock methods.
1 parent 8d6d829 commit 43b0bc6

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

progenitor-impl/src/httpmock.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,21 @@ impl Generator {
244244
},
245245
),
246246
OperationParameterKind::Body(body_content_type) => match typ {
247-
OperationParameterType::Type(_) => (
248-
true,
249-
quote! {
250-
Self(self.0.json_body_obj(value))
251-
},
252-
),
247+
OperationParameterType::Type(type_id) => {
248+
let ty = self.type_space.get_type(type_id).unwrap();
249+
let is_string =
250+
matches!(ty.details(), typify::TypeDetails::String);
251+
// String types need `to_owned()` because `str` doesn't
252+
// implement Deserialize and is unsized
253+
let value_expr =
254+
if is_string { quote! { &value.to_owned() } } else { quote! { value } };
255+
(
256+
true,
257+
quote! {
258+
Self(self.0.json_body_obj(#value_expr))
259+
},
260+
)
261+
}
253262
OperationParameterType::RawBody => match body_content_type {
254263
BodyContentType::OctetStream => (
255264
true,
@@ -335,13 +344,18 @@ impl Generator {
335344
crate::method::OperationResponseKind::Type(arg_type_id) => {
336345
let arg_type = self.type_space.get_type(arg_type_id).unwrap();
337346
let arg_type_ident = arg_type.parameter_ident();
347+
let is_string =
348+
matches!(arg_type.details(), typify::TypeDetails::String);
349+
// String types need `to_owned()` because `str` is unsized
350+
let value_expr =
351+
if is_string { quote! { &value.to_owned() } } else { quote! { value } };
338352
(
339353
quote! {
340354
value: #arg_type_ident,
341355
},
342356
quote! {
343357
.header("content-type", "application/json")
344-
.json_body_obj(value)
358+
.json_body_obj(#value_expr)
345359
},
346360
)
347361
}

0 commit comments

Comments
 (0)