All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rust-server.server-request-body-multipart-related.mustache Maven / Gradle / Ivy

The newest version!
                                // Get multipart chunks.

                                // Create headers from top-level content type header.
                                let multipart_headers = match swagger::multipart::related::create_multipart_headers(headers.get(CONTENT_TYPE)) {
                                    Ok(headers) => headers,
                                    Err(e) => {
                                        return Ok(Response::builder()
                                                .status(StatusCode::BAD_REQUEST)
                                                .body(Body::from(e))
                                                .expect("Unable to create Bad Request response due to unable to read content-type header for {{operationId}}"));
                                    }
                                };

                                // &*body expresses the body as a byteslice, &mut provides a
                                // mutable reference to that byteslice.
                                let nodes = match read_multipart_body(&mut&*body, &multipart_headers, false) {
                                    Ok(nodes) => nodes,
                                    Err(e) => {
                                        return Ok(Response::builder()
                                                .status(StatusCode::BAD_REQUEST)
                                                .body(Body::from(format!("Could not read multipart body for {{operationId}}: {}", e)))
                                                .expect("Unable to create Bad Request response due to unable to read multipart body for {{operationId}}"));
                                    }
                                };

{{#formParams}}
                                let mut param_{{{paramName}}} = None;
{{/formParams}}

                                for node in nodes {
                                    if let Node::Part(part) = node {
                                        let content_type = part.content_type().map(|x| format!("{}",x));
                                        match content_type.as_deref() {
{{#formParams}}
  {{^isBinary}}
                                            Some("{{{contentType}}}") if param_{{{paramName}}}.is_none() => {
                                                // Extract JSON part.
                                                let deserializer = &mut serde_json::Deserializer::from_slice(part.body.as_slice());
                                                let json_data: {{dataType}} = match serde_ignored::deserialize(deserializer, |path| {
                                                    warn!("Ignoring unknown field in JSON part: {}", path);
                                                    unused_elements.push(path.to_string());
                                                }) {
                                                    Ok(json_data) => json_data,
                                                    Err(e) => return Ok(Response::builder()
                                                        .status(StatusCode::BAD_REQUEST)
                                                        .body(Body::from(format!("Couldn't parse body parameter {{dataType}} - doesn't match schema: {}", e)))
                                                        .expect("Unable to create Bad Request response for invalid body parameter {{dataType}} due to schema"))
                                                };
                                                // Push JSON part to return object.
                                                param_{{{paramName}}}.get_or_insert(json_data);
                                            },
  {{/isBinary}}
  {{#isBinary}}
                                            Some("{{{contentType}}}") if param_{{{paramName}}}.is_none() => {
                                                param_{{{paramName}}}.get_or_insert(swagger::ByteArray(part.body));
                                            },
  {{/isBinary}}
{{/formParams}}
                                            Some(content_type) => {
                                                warn!("Ignoring unexpected content type: {}", content_type);
                                                unused_elements.push(content_type.to_string());
                                            },
                                            None => {
                                                warn!("Missing content type");
                                            },
                                        }
                                    } else {
                                        unimplemented!("No support for handling unexpected parts");
                                        // unused_elements.push();
                                    }
                                }
{{#formParams}}
  {{#-first}}

                                // Check that the required multipart chunks are present.
  {{/-first}}
  {{#required}}
                                let param_{{{paramName}}} = match param_{{{paramName}}} {
                                    Some(x) => x,
                                    None =>  return Ok(Response::builder()
                                        .status(StatusCode::BAD_REQUEST)
                                        .body(Body::from("Missing required multipart/related parameter {{{paramName}}}".to_string()))
                                        .expect("Unable to create Bad Request response for missing multipart/related parameter {{{paramName}}} due to schema"))
                                };
  {{/required}}
{{/formParams}}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy