@@ -334,35 +334,39 @@ fn request_body_params(
334334 ReferenceOr :: Reference { reference } => Err ( Error :: unimplemented (
335335 format ! ( "Unexpected ref multipart schema: '{reference}'." ) ,
336336 ) ) ,
337- ReferenceOr :: Item ( schema) => {
338- match & schema. schema_kind {
339- SchemaKind :: Type ( Type :: Object ( obj) ) => {
340- fn multipart_param (
341- name : & str ,
342- schema : & ReferenceOr < Box < Schema > > ,
343- ref_cache : & mut RefCache ,
344- ) -> Result < Param > {
345- Ok ( Param {
346- original_name : name. to_string ( ) ,
347- name : name. to_case ( Case :: Snake ) ,
348- tpe : ref_or_box_schema_type ( schema, ref_cache) ?,
349- required : true , // TODO
350- kind : ParamKind :: Multipart ,
351- } )
352- }
353-
354- obj. properties
355- . iter ( )
356- . map ( |( name, schema) | {
357- multipart_param ( name, schema, ref_cache)
358- } )
359- . collect ( )
337+ ReferenceOr :: Item ( schema) => match & schema. schema_kind {
338+ SchemaKind :: Type ( Type :: Object ( obj) ) => {
339+ fn multipart_param (
340+ name : & str ,
341+ required : bool ,
342+ schema : & ReferenceOr < Box < Schema > > ,
343+ ref_cache : & mut RefCache ,
344+ ) -> Result < Param > {
345+ Ok ( Param {
346+ original_name : name. to_string ( ) ,
347+ name : name. to_case ( Case :: Snake ) ,
348+ tpe : ref_or_box_schema_type ( schema, ref_cache) ?,
349+ required,
350+ kind : ParamKind :: Multipart ,
351+ } )
360352 }
361- _ => Err ( Error :: unimplemented (
362- "Object schema expected for multipart request body." ,
363- ) ) ,
353+
354+ obj. properties
355+ . iter ( )
356+ . map ( |( name, schema) | {
357+ multipart_param (
358+ name,
359+ body. required && obj. required . contains ( name) ,
360+ schema,
361+ ref_cache,
362+ )
363+ } )
364+ . collect ( )
364365 }
365- }
366+ _ => Err ( Error :: unimplemented (
367+ "Object schema expected for multipart request body." ,
368+ ) ) ,
369+ } ,
366370 } ,
367371 }
368372 } else {
@@ -657,14 +661,26 @@ fn header_setter(param: &Param) -> RustResult {
657661fn make_part ( param : & Param ) -> RustResult {
658662 let part_type = rust_name ( "reqwest::multipart" , "Part" ) ;
659663
660- if param. tpe == DataType :: Binary {
661- Ok ( indent ( ) + r#".part(""# + & param. original_name + r#"", "# + part_type + "::stream(" + & param. name + r#").mime_str("application/octet-stream")?)"# )
662- } else if param. tpe == DataType :: String {
663- Ok ( indent ( ) + r#".part(""# + & param. original_name + r#"", "# + part_type + "::text(" + & param. name + r#".to_string()).mime_str("text/plain; charset=utf-8")?)"# )
664- } else if let DataType :: Model ( _) = param. tpe {
665- Ok ( indent ( ) + r#".part(""# + & param. original_name + r#"", "# + part_type + "::text(serde_json::to_string(" + & param. name + r#")?).mime_str("application/json")?)"# )
664+ let inner =
665+ if param. tpe == DataType :: Binary {
666+ Ok ( indent ( ) + r#"form = form.part(""# + & param. original_name + r#"", "# + part_type + "::stream(" + & param. name + r#").mime_str("application/octet-stream")?);"# )
667+ } else if param. tpe == DataType :: String {
668+ Ok ( indent ( ) + r#"form = form.part(""# + & param. original_name + r#"", "# + part_type + "::text(" + & param. name + r#".to_string()).mime_str("text/plain; charset=utf-8")?);"# )
669+ }
670+ else if let DataType :: Model ( _) = param. tpe {
671+ Ok ( indent ( ) + r#"form = form.part(""# + & param. original_name + r#"", "# + part_type + "::text(crate::model::MultipartField::to_multipart_field(" + & param. name + r#")).mime_str(crate::model::MultipartField::mime_type("# + & param. name + r#"))?);"# )
672+ } else {
673+ Err ( Error :: unimplemented ( format ! ( "Unsupported multipart part type {:?}" , param. tpe) ) )
674+ } ;
675+
676+ if param. required {
677+ inner
666678 } else {
667- Err ( Error :: unimplemented ( format ! ( "Unsupported multipart part type {:?}" , param. tpe) ) )
679+ Ok (
680+ indent ( ) + line ( unit ( ) + r#"if let Some("# + & param. name + r#") = "# + & param. name + " {" ) +
681+ indented ( inner?) +
682+ line ( "}" )
683+ )
668684 }
669685}
670686
@@ -837,10 +853,8 @@ fn render_method_implementation(method: &Method, error_kind: &ErrorKind) -> Rust
837853 let multipart_setter = if is_multipart {
838854 #[ rustfmt:: skip]
839855 let code = unit ( ) +
840- indent ( ) + "let form = " + rust_name ( "reqwest::multipart" , "Form" ) + "::new()" +
841- indented (
842- multipart_parts?. into_iter ( ) . map ( |p| unit ( ) + NewLine + p) . reduce ( |acc, e| acc + e) . unwrap_or_else ( unit) + ";" + NewLine
843- ) +
856+ indent ( ) + "let mut form = " + rust_name ( "reqwest::multipart" , "Form" ) + "::new();" +
857+ ( multipart_parts?. into_iter ( ) . map ( |p| unit ( ) + NewLine + p) . reduce ( |acc, e| acc + e) . unwrap_or_else ( unit) + NewLine ) +
844858 NewLine +
845859 line ( "request = request.multipart(form);" ) ;
846860
0 commit comments