rust-server.server-request-body-basic.mustache Maven / Gradle / Ivy
{{#vendorExtensions}}
let param_{{{paramName}}}: Option<{{{dataType}}}> = if !body.is_empty() {
{{#x-consumes-xml}}
let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body);
{{/x-consumes-xml}}
{{#x-consumes-json}}
let deserializer = &mut serde_json::Deserializer::from_slice(&*body);
{{/x-consumes-json}}
{{^x-consumes-plain-text}}
match serde_ignored::deserialize(deserializer, |path| {
warn!("Ignoring unknown field in body: {}", path);
unused_elements.push(path.to_string());
}) {
Ok(param_{{{paramName}}}) => param_{{{paramName}}},
{{#required}}
Err(e) => return Ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(format!("Couldn't parse body parameter {{{baseName}}} - doesn't match schema: {}", e)))
.expect("Unable to create Bad Request response for invalid body parameter {{{baseName}}} due to schema")),
{{/required}}
{{^required}}
Err(_) => None,
{{/required}}
}
{{/x-consumes-plain-text}}
{{#x-consumes-plain-text}}
{{#isByteArray}}
Some(swagger::ByteArray(body.to_vec()))
{{/isByteArray}}
{{#isString}}
match String::from_utf8(body.to_vec()) {
Ok(param_{{{paramName}}}) => Some(param_{{{paramName}}}),
Err(e) => return Ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from(format!("Couldn't parse body parameter {{{baseName}}} - not valid UTF-8: {}", e)))
.expect("Unable to create Bad Request response for invalid body parameter {{{baseName}}} due to UTF-8")),
}
{{/isString}}
{{/x-consumes-plain-text}}
{{/vendorExtensions}}
} else {
None
};
{{#required}}
let param_{{{paramName}}} = match param_{{{paramName}}} {
Some(param_{{{paramName}}}) => param_{{{paramName}}},
None => return Ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from("Missing required body parameter {{{baseName}}}"))
.expect("Unable to create Bad Request response for missing body parameter {{{baseName}}}")),
};
{{/required}}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy