diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java index 5b8a6e69118f..75933ea708c6 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java @@ -821,12 +821,15 @@ private void postProcessOperationWithModels(CodegenOperation op, List } else if (isMimetypePlain(mediaType)) { consumesPlainText = true; } else if (isMimetypeWwwFormUrlEncoded(mediaType)) { + op.vendorExtensions.put("x-consumes-form", true); additionalProperties.put("usesUrlEncodedForm", true); } else if (isMimetypeMultipartFormData(mediaType)) { op.vendorExtensions.put("x-consumes-multipart", true); + op.vendorExtensions.put("x-consumes-multipart-form", true); additionalProperties.put("apiUsesMultipartFormData", true); additionalProperties.put("apiUsesMultipart", true); } else if (isMimetypeMultipartRelated(mediaType)) { + op.vendorExtensions.put("x-consumes-multipart", true); op.vendorExtensions.put("x-consumes-multipart-related", true); additionalProperties.put("apiUsesMultipartRelated", true); additionalProperties.put("apiUsesMultipart", true); @@ -835,15 +838,23 @@ private void postProcessOperationWithModels(CodegenOperation op, List } } + if (op.bodyParams.size() > 0 || op.formParams.size() > 0){ + op.vendorExtensions.put("x-has-request-body", true); + } + String underscoredOperationId = underscore(op.operationId).toUpperCase(Locale.ROOT); + if (op.bodyParam != null) { // Default to consuming json op.bodyParam.vendorExtensions.put("x-uppercase-operation-id", underscoredOperationId); if (consumesXml) { + op.vendorExtensions.put("x-consumes-basic", true); op.bodyParam.vendorExtensions.put("x-consumes-xml", true); } else if (consumesPlainText) { + op.vendorExtensions.put("x-consumes-basic", true); op.bodyParam.vendorExtensions.put("x-consumes-plain-text", true); } else { + op.vendorExtensions.put("x-consumes-basic", true); op.bodyParam.vendorExtensions.put("x-consumes-json", true); } } @@ -855,10 +866,13 @@ private void postProcessOperationWithModels(CodegenOperation op, List // Default to producing json if nothing else is specified if (consumesXml) { + op.vendorExtensions.put("x-consumes-basic", true); param.vendorExtensions.put("x-consumes-xml", true); } else if (consumesPlainText) { + op.vendorExtensions.put("x-consumes-basic", true); param.vendorExtensions.put("x-consumes-plain-text", true); } else { + op.vendorExtensions.put("x-consumes-basic", true); param.vendorExtensions.put("x-consumes-json", true); } } diff --git a/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache b/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache index f74833a77a51..fec80b345b8e 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache @@ -85,217 +85,7 @@ Ok(req) => req, Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; - -{{#vendorExtensions}} - {{#x-consumes-multipart}} - let (body_string, multipart_header) = { - let mut multipart = Multipart::new(); - - {{#vendorExtensions}} - {{#formParams}} - {{#-first}} - // For each parameter, encode as appropriate and add to the multipart body as a stream. - {{/-first}} - - {{^isByteArray}} - {{#jsonSchema}} - let {{{paramName}}}_str = match serde_json::to_string(¶m_{{{paramName}}}) { - Ok(str) => str, - Err(e) => return Err(ApiError(format!("Unable to serialize {{{paramName}}} to string: {}", e))), - }; - - let {{{paramName}}}_vec = {{{paramName}}}_str.as_bytes().to_vec(); - let {{{paramName}}}_mime = mime_0_2::Mime::from_str("application/json").expect("impossible to fail to parse"); - let {{{paramName}}}_cursor = Cursor::new({{{paramName}}}_vec); - - multipart.add_stream("{{{paramName}}}", {{{paramName}}}_cursor, None as Option<&str>, Some({{{paramName}}}_mime)); - {{/jsonSchema}} - {{/isByteArray}} - - {{#isByteArray}} - let {{{paramName}}}_vec = param_{{{paramName}}}.to_vec(); - - let {{{paramName}}}_mime = match mime_0_2::Mime::from_str("application/octet-stream") { - Ok(mime) => mime, - Err(err) => return Err(ApiError(format!("Unable to get mime type: {:?}", err))), - }; - - let {{{paramName}}}_cursor = Cursor::new({{{paramName}}}_vec); - - let filename = None as Option<&str> ; - multipart.add_stream("{{{paramName}}}", {{{paramName}}}_cursor, filename, Some({{{paramName}}}_mime)); - {{/isByteArray}} - {{/formParams}} - {{/vendorExtensions}} - - let mut fields = match multipart.prepare() { - Ok(fields) => fields, - Err(err) => return Err(ApiError(format!("Unable to build request: {}", err))), - }; - - let mut body_string = String::new(); - - match fields.read_to_string(&mut body_string) { - Ok(_) => (), - Err(err) => return Err(ApiError(format!("Unable to build body: {}", err))), - } - - let boundary = fields.boundary(); - - let multipart_header = format!("multipart/form-data;boundary={}", boundary); - - (body_string, multipart_header) - }; - - *request.body_mut() = Body::from(body_string); - - request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(&multipart_header) { - Ok(h) => h, - Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", multipart_header, e))) - }); - - {{/x-consumes-multipart}} - {{^x-consumes-multipart}} - {{#vendorExtensions}} - {{^x-consumes-multipart-related}} - {{#formParams}} - {{#-first}} - let params = &[ - {{/-first}} - ("{{{baseName}}}", {{#vendorExtensions}}{{#required}}Some({{#isString}}param_{{{paramName}}}{{/isString}}{{^isString}}format!("{:?}", param_{{{paramName}}}){{/isString}}){{/required}}{{^required}}{{#isString}}param_{{{paramName}}}{{/isString}}{{^isString}}param_{{{paramName}}}.map(|param| format!("{:?}", param)){{/isString}}{{/required}}{{/vendorExtensions}}), - {{#-last}} - ]; - let body = serde_urlencoded::to_string(params).expect("impossible to fail to serialize"); - - let header = "{{#consumes}}{{#-first}}{{{mediaType}}}{{/-first}}{{/consumes}}{{^consumes}}application/json{{/consumes}}"; - request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { - Ok(h) => h, - Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) - }); - *request.body_mut() = Body::from(body.into_bytes()); - {{/-last}} - {{/formParams}} - {{/x-consumes-multipart-related}} - {{#x-consumes-multipart-related}} - {{#formParams}} - {{#-first}} - // Construct the Body for a multipart/related request. The mime 0.2.6 library - // does not parse quoted-string parameters correctly. The boundary doesn't - // need to be a quoted string if it does not contain a '/', hence ensure - // no such boundary is used. - let mut boundary = generate_boundary(); - for b in boundary.iter_mut() { - if b == &(b'/') { - *b = b'='; - } - } - - let mut body_parts = vec![]; - {{/-first}} - -{{#required}} - { -{{/required}} -{{^required}} - if let Some({{{paramName}}}) = param_{{{paramName}}} { -{{/required}} - let part = Node::Part(Part { - headers: { - let mut h = Headers::new(); - h.set(ContentType("{{{contentType}}}".parse().unwrap())); - h.set_raw("Content-ID", vec![b"{{{baseName}}}".to_vec()]); - h - }, - {{#isBinary}} - body: {{#required}}param_{{/required}}{{{paramName}}}.0, - {{/isBinary}} - {{^isBinary}} - body: serde_json::to_string(&{{{paramName}}}) - .expect("Impossible to fail to serialize") - .into_bytes(), - {{/isBinary}} - }); - body_parts.push(part); - } - {{#-last}} - - // Write the body into a vec. - let mut body: Vec = vec![]; - write_multipart(&mut body, &boundary, &body_parts) - .expect("Failed to write multipart body"); - - // Add the message body to the request object. - *request.body_mut() = Body::from(body); - - let header = "{{#consumes}}{{#-first}}{{{mediaType}}}{{/-first}}{{/consumes}}{{^consumes}}application/json{{/consumes}}"; - request.headers_mut().insert(CONTENT_TYPE, - match HeaderValue::from_bytes( - &[header.as_bytes(), "; boundary=".as_bytes(), &boundary, "; type=\"application/json\"".as_bytes()].concat() - ) { - Ok(h) => h, - Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) - }); - - {{/-last}} - {{/formParams}} - {{/x-consumes-multipart-related}} -{{/vendorExtensions}} -{{#bodyParam}} - {{#-first}} - // Body parameter - {{/-first}} - {{#vendorExtensions}} - {{#x-consumes-plain-text}} - {{#isByteArray}} - let body = param_{{{paramName}}}.0; - {{/isByteArray}} - {{^isByteArray}} - let body = param_{{{paramName}}}; - {{/isByteArray}} - {{/x-consumes-plain-text}} - {{#required}} - {{#x-consumes-xml}} - let body = param_{{{paramName}}}.as_xml(); - {{/x-consumes-xml}} - {{#x-consumes-json}} - let body = serde_json::to_string(¶m_{{{paramName}}}).expect("impossible to fail to serialize"); - {{/x-consumes-json}} - {{/required}} - {{^required}} - let body = param_{{{paramName}}}.map(|ref body| { - {{#x-consumes-xml}} - body.as_xml() - {{/x-consumes-xml}} - {{#x-consumes-json}} - serde_json::to_string(body).expect("impossible to fail to serialize") - {{/x-consumes-json}} - }); - {{/required}} - {{/vendorExtensions}} - {{#-last}} - - {{/-last}} -{{/bodyParam}} -{{#bodyParam}} - {{^required}} - if let Some(body) = body { - {{/required}} - *request.body_mut() = Body::from(body); - {{^required}} - } - {{/required}} - - let header = "{{#consumes}}{{#-first}}{{{mediaType}}}{{/-first}}{{/consumes}}{{^consumes}}application/json{{/consumes}}"; - request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { - Ok(h) => h, - Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) - }); - {{#-last}} - - {{/-last}} -{{/bodyParam}} -{{/x-consumes-multipart}} -{{/vendorExtensions}} +{{>client-request-body-instance}} let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -421,29 +211,9 @@ let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; -{{#vendorExtensions}} -{{#x-produces-bytes}} - let body = swagger::ByteArray(body.to_vec()); -{{/x-produces-bytes}} -{{^x-produces-bytes}} - let body = str::from_utf8(&body) - .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - {{#x-produces-xml}} - // ToDo: this will move to swagger-rs and become a standard From conversion trait - // once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream - let body = serde_xml_rs::from_str::<{{{dataType}}}>(body) - .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; - {{/x-produces-xml}} - {{#x-produces-json}} - let body = serde_json::from_str::<{{{dataType}}}>(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; - {{/x-produces-json}} - {{#x-produces-plain-text}} - let body = body.to_string(); - {{/x-produces-plain-text}} -{{/x-produces-bytes}} -{{/vendorExtensions}} + +{{>client-response-body-instance}} + Ok({{{operationId}}}Response::{{#vendorExtensions}}{{x-response-id}}{{/vendorExtensions}} {{^headers}} (body) diff --git a/modules/openapi-generator/src/main/resources/rust-server/client-request-body-instance.mustache b/modules/openapi-generator/src/main/resources/rust-server/client-request-body-instance.mustache new file mode 100644 index 000000000000..d9f734cdf8a6 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/rust-server/client-request-body-instance.mustache @@ -0,0 +1,84 @@ +{{#vendorExtensions}} + {{#x-consumes-multipart-form}} + + // Consumes multipart/form body +{{>client-request-body-multipart-form}} + {{/x-consumes-multipart-form}} + {{#x-consumes-multipart-related}} + + // Consumes multipart/related body + {{#formParams}} +{{>generate-multipart-related}} + {{/formParams}} + + let header = "multipart/related"; + request.headers_mut().insert(CONTENT_TYPE, + match HeaderValue::from_bytes( + &[header.as_bytes(), "; boundary=".as_bytes(), &boundary, "; type=\"application/json\"".as_bytes()].concat() + ) { + Ok(h) => h, + Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) + }); + + // Add the message body to the request object. + *request.body_mut() = Body::from(body); + {{/x-consumes-multipart-related}} + {{#x-consumes-form}} + + // Consumes form body + {{#formParams}} + {{#-first}} + let params = &[ + {{/-first}} + ("{{{baseName}}}", {{#vendorExtensions}}{{#required}}Some({{#isString}}param_{{{paramName}}}{{/isString}}{{^isString}}format!("{:?}", param_{{{paramName}}}){{/isString}}){{/required}}{{^required}}{{#isString}}param_{{{paramName}}}{{/isString}}{{^isString}}param_{{{paramName}}}.map(|param| format!("{:?}", param)){{/isString}}{{/required}}{{/vendorExtensions}}), + {{#-last}} + ]; + let body = serde_urlencoded::to_string(params).expect("impossible to fail to serialize"); + + *request.body_mut() = Body::from(body.into_bytes()); + + let header = "{{#consumes}}{{#-first}}{{{mediaType}}}{{/-first}}{{/consumes}}{{^consumes}}application/json{{/consumes}}"; + request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { + Ok(h) => h, + Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) + }); + {{/-last}} + {{/formParams}} + {{/x-consumes-form}} + {{#x-consumes-basic}} + + // Consumes basic body + {{#bodyParam}} + // Body parameter + {{^required}} + if let Some(param_{{{paramName}}}) = param_{{{paramName}}} { + {{/required}} + {{#vendorExtensions}} + {{#x-consumes-plain-text}} + {{#isByteArray}} + let body = param_{{{paramName}}}.0; + {{/isByteArray}} + {{^isByteArray}} + let body = param_{{{paramName}}}; + {{/isByteArray}} + {{/x-consumes-plain-text}} + {{#x-consumes-xml}} + let body = param_{{{paramName}}}.as_xml(); + {{/x-consumes-xml}} + {{#x-consumes-json}} + let body = serde_json::to_string(¶m_{{{paramName}}}).expect("impossible to fail to serialize"); + {{/x-consumes-json}} + {{/vendorExtensions}} + *request.body_mut() = Body::from(body); + {{^required}} + } + {{/required}} + + let header = "{{#consumes}}{{#-first}}{{{mediaType}}}{{/-first}}{{/consumes}}{{^consumes}}application/json{{/consumes}}"; + request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { + Ok(h) => h, + Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) + }); + {{/bodyParam}} + {{/x-consumes-basic}} +{{/vendorExtensions}} diff --git a/modules/openapi-generator/src/main/resources/rust-server/client-request-body-multipart-form.mustache b/modules/openapi-generator/src/main/resources/rust-server/client-request-body-multipart-form.mustache new file mode 100644 index 000000000000..ce8869195f77 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/rust-server/client-request-body-multipart-form.mustache @@ -0,0 +1,65 @@ + let (body_string, multipart_header) = { + let mut multipart = Multipart::new(); + + {{#vendorExtensions}} + {{#formParams}} + {{#-first}} + // For each parameter, encode as appropriate and add to the multipart body as a stream. + {{/-first}} + + {{^isByteArray}} + {{#jsonSchema}} + let {{{paramName}}}_str = match serde_json::to_string(¶m_{{{paramName}}}) { + Ok(str) => str, + Err(e) => return Err(ApiError(format!("Unable to serialize {{{paramName}}} to string: {}", e))), + }; + + let {{{paramName}}}_vec = {{{paramName}}}_str.as_bytes().to_vec(); + let {{{paramName}}}_mime = mime_0_2::Mime::from_str("application/json").expect("impossible to fail to parse"); + let {{{paramName}}}_cursor = Cursor::new({{{paramName}}}_vec); + + multipart.add_stream("{{{paramName}}}", {{{paramName}}}_cursor, None as Option<&str>, Some({{{paramName}}}_mime)); + {{/jsonSchema}} + {{/isByteArray}} + + {{#isByteArray}} + let {{{paramName}}}_vec = param_{{{paramName}}}.to_vec(); + + let {{{paramName}}}_mime = match mime_0_2::Mime::from_str("application/octet-stream") { + Ok(mime) => mime, + Err(err) => return Err(ApiError(format!("Unable to get mime type: {:?}", err))), + }; + + let {{{paramName}}}_cursor = Cursor::new({{{paramName}}}_vec); + + let filename = None as Option<&str> ; + multipart.add_stream("{{{paramName}}}", {{{paramName}}}_cursor, filename, Some({{{paramName}}}_mime)); + {{/isByteArray}} + {{/formParams}} + {{/vendorExtensions}} + + let mut fields = match multipart.prepare() { + Ok(fields) => fields, + Err(err) => return Err(ApiError(format!("Unable to build request: {}", err))), + }; + + let mut body_string = String::new(); + + match fields.read_to_string(&mut body_string) { + Ok(_) => (), + Err(err) => return Err(ApiError(format!("Unable to build body: {}", err))), + } + + let boundary = fields.boundary(); + + let multipart_header = format!("multipart/form-data;boundary={}", boundary); + + (body_string, multipart_header) + }; + + *request.body_mut() = Body::from(body_string); + + request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(&multipart_header) { + Ok(h) => h, + Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", multipart_header, e))) + }); diff --git a/modules/openapi-generator/src/main/resources/rust-server/client-response-body-instance.mustache b/modules/openapi-generator/src/main/resources/rust-server/client-response-body-instance.mustache new file mode 100644 index 000000000000..3ff6dfb0c7a9 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/rust-server/client-response-body-instance.mustache @@ -0,0 +1,22 @@ +{{#vendorExtensions}} + {{#x-produces-bytes}} + let body = swagger::ByteArray(body.to_vec()); + {{/x-produces-bytes}} + {{^x-produces-bytes}} + let body = str::from_utf8(&body) + .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; + {{#x-produces-xml}} + // ToDo: this will move to swagger-rs and become a standard From conversion trait + // once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream + let body = serde_xml_rs::from_str::<{{{dataType}}}>(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + {{/x-produces-xml}} + {{#x-produces-json}} + let body = serde_json::from_str::<{{{dataType}}}>(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + {{/x-produces-json}} + {{#x-produces-plain-text}} + let body = body.to_string(); + {{/x-produces-plain-text}} + {{/x-produces-bytes}} +{{/vendorExtensions}} diff --git a/modules/openapi-generator/src/main/resources/rust-server/client-response-body-multipart-related.mustache b/modules/openapi-generator/src/main/resources/rust-server/client-response-body-multipart-related.mustache new file mode 100644 index 000000000000..0c6aeaacc490 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/rust-server/client-response-body-multipart-related.mustache @@ -0,0 +1,76 @@ +// Create headers from top-level content type header. +let multipart_headers = match swagger::multipart::related::create_multipart_headers(header.headers.get(CONTENT_TYPE)) { + Ok(headers) => headers, + Err(e) => { + return Err(ApiError(e)); + } +}; + +// &*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 Err(ApiError(format!("Could not read multipart body for {{operationId}}: {}", e))); + } +}; + +{{#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_ref().map(|x| x.as_str()) { +{{#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); + }) { + Ok(json_data) => json_data, + Err(e) => return Err(ApiError(format!("Couldn't parse body parameter {{dataType}} - doesn't match schema: {}", e))) + }; + // 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); + }, + None => { + warn!("Missing content type"); + }, + } + } else { + return Err(ApiError(format!("Unexpected part in multipart body for {{operationId}}: {:?}", node))); + } +} +{{#formParams}} + {{#-first}} + +// Check that the required multipart chunks are present. + {{/-first}} + {{#required}} +let param_{{{paramName}}} = match param_{{{paramName}}} { + Some(x) => x, + None => return Err(ApiError("Missing required multipart/related parameter {{{paramName}}}")) +}; + {{/required}} +{{/formParams}} + {{^vendorExtensions.x-consumes-basic}} + let body = {{{dataType}}} { + {{#formParams}} + {{{paramName}}}: param_{{{paramName}}}, + {{/formParams}} + }; + {{/vendorExtensions.x-consumes-basic}} diff --git a/modules/openapi-generator/src/main/resources/rust-server/generate-multipart-related.mustache b/modules/openapi-generator/src/main/resources/rust-server/generate-multipart-related.mustache new file mode 100644 index 000000000000..ff2330c07d4d --- /dev/null +++ b/modules/openapi-generator/src/main/resources/rust-server/generate-multipart-related.mustache @@ -0,0 +1,45 @@ +{{#-first}} + // Construct the Body for a multipart/related request. The mime 0.2.6 library + // does not parse quoted-string parameters correctly. The boundary doesn't + // need to be a quoted string if it does not contain a '/', hence ensure + // no such boundary is used. + let mut boundary = generate_boundary(); + for b in boundary.iter_mut() { + if b == &(b'/') { + *b = b'='; + } + } + let mut body_parts = vec![]; + +{{/-first}} +{{#required}} + { +{{/required}} +{{^required}} + if let Some({{{paramName}}}) = param_{{{paramName}}} { +{{/required}} + let part = Node::Part(Part { + headers: { + let mut h = Headers::new(); + h.set(ContentType("{{{contentType}}}".parse().unwrap())); + h.set_raw("Content-ID", vec![b"{{{baseName}}}".to_vec()]); + h + }, + {{#isBinary}} + body: {{#required}}param_{{/required}}{{{paramName}}}.0, + {{/isBinary}} + {{^isBinary}} + body: serde_json::to_string(&{{{paramName}}}) + .expect("Impossible to fail to serialize") + .into_bytes(), + {{/isBinary}} + }); + body_parts.push(part); + } +{{#-last}} + + // Write the body into a vec. + let mut body: Vec = vec![]; + write_multipart(&mut body, &boundary, &body_parts) + .expect("Failed to write multipart body"); +{{/-last}} diff --git a/modules/openapi-generator/src/main/resources/rust-server/server-operation.mustache b/modules/openapi-generator/src/main/resources/rust-server/server-operation.mustache index f0ba616a9abc..4957b65c4dc5 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/server-operation.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/server-operation.mustache @@ -38,16 +38,6 @@ {{/hasAuthMethods}} {{#vendorExtensions}} - {{#x-consumes-multipart}} - let boundary = match swagger::multipart::form::boundary(&headers) { - Some(boundary) => boundary.to_string(), - None => return Ok(Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(Body::from("Couldn't find valid multipart body".to_string())) - .expect("Unable to create Bad Request response for incorrect boundary")), - }; - - {{/x-consumes-multipart}} {{#x-has-path-params}} // Path parameters let path: &str = uri.path(); @@ -202,278 +192,22 @@ {{/-last}} {{/queryParams}} -{{#vendorExtensions}} -{{^x-consumes-multipart}} -{{#bodyParams}} -{{#-first}} - // Body parameters (note that non-required body parameters will ignore garbage +{{#vendorExtensions.x-has-request-body}} + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { -{{#vendorExtensions}} -{{^x-consumes-plain-text}} - let mut unused_elements = Vec::new(); -{{/x-consumes-plain-text}} - 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}} -{{/-first}} - {{#-last}} - - {{/-last}} -{{/bodyParams}} -{{/x-consumes-multipart}} -{{#x-consumes-multipart}} - {{^bodyParams}} - {{#vendorExtensions}} - // Form Body parameters (note that non-required body parameters will ignore garbage - // values, rather than causing a 400 response). Produce warning header and logs for - // any unused fields. - let result = body.into_raw(); - match result.await { - Ok(body) => { - use std::io::Read; - - // Read Form Parameters from body - let mut entries = match Multipart::with_body(&body.to_vec()[..], boundary).save().temp() { - SaveResult::Full(entries) => { - entries - }, - _ => { - return Ok(Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(Body::from("Unable to process all message parts".to_string())) - .expect("Unable to create Bad Request response due to failure to process all message")) - }, - }; - {{#formParams}} - let field_{{{paramName}}} = entries.fields.remove("{{{paramName}}}"); - let param_{{{paramName}}} = match field_{{{paramName}}} { - Some(field) => { - let mut reader = field[0].data.readable().expect("Unable to read field for {{{paramName}}}"); - {{^required}} - Some({ - {{/required}} - {{#isByteArray}} - let mut data = vec![]; - reader.read_to_end(&mut data).expect("Reading saved binary data should never fail"); - swagger::ByteArray(data) - {{/isByteArray}} - {{^isByteArray}} - {{#jsonSchema}} - let mut data = String::new(); - reader.read_to_string(&mut data).expect("Reading saved String should never fail"); - let {{{paramName}}}_model: {{{dataType}}} = match serde_json::from_str(&data) { - Ok(model) => model, - Err(e) => { - return Ok( - Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("{{{paramName}}} data does not match API definition : {}", e))) - .expect("Unable to create Bad Request due to missing required form parameter {{{paramName}}}")) - } - }; - {{{paramName}}}_model - {{/jsonSchema}} - {{/isByteArray}} - {{^required}} - }) - {{/required}} - }, - None => { - {{#required}} - return Ok( - Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(Body::from("Missing required form parameter {{{paramName}}}".to_string())) - .expect("Unable to create Bad Request due to missing required form parameter {{{paramName}}}")) - {{/required}} - {{^required}} - None - {{/required}} - } - }; - {{/formParams}} - {{/vendorExtensions}} - {{/bodyParams}} -{{/x-consumes-multipart}} -{{^x-consumes-multipart-related}} -{{^x-consumes-multipart}} -{{^bodyParams}} -{{#vendorExtensions}} -{{#formParams}} -{{#-first}} - // Form parameters -{{/-first}} - let param_{{{paramName}}} = {{^isContainer}}{{#vendorExtensions}}{{{x-example}}};{{/vendorExtensions}}{{/isContainer}}{{#isArray}}{{#required}}Vec::new();{{/required}}{{^required}}None;{{/required}}{{/isArray}}{{#isMap}}None;{{/isMap}} -{{#-last}} - -{{/-last}} -{{/formParams}} -{{/vendorExtensions}} -{{/bodyParams}} -{{/x-consumes-multipart}} -{{/x-consumes-multipart-related}} -{{#x-consumes-multipart-related}} - // Body parameters (note that non-required body parameters will ignore garbage - // values, rather than causing a 400 response). Produce warning header and logs for - // any unused fields. - let result = body.into_raw(); - match result.await { - Ok(body) => { - let mut unused_elements: Vec = vec![]; - - // Get multipart chunks. - - // Extract the top-level content type header. - let content_type_mime = headers - .get(CONTENT_TYPE) - .ok_or_else(|| "Missing content-type header".to_string()) - .and_then(|v| v.to_str().map_err(|e| format!("Couldn't read content-type header value for {{operationId}}: {}", e))) - .and_then(|v| v.parse::().map_err(|_e| "Couldn't parse content-type header value for {{operationId}}".to_string())); - - // Insert top-level content type header into a Headers object. - let mut multi_part_headers = Headers::new(); - match content_type_mime { - Ok(content_type_mime) => { - multi_part_headers.set(ContentType(content_type_mime)); - }, - 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, &multi_part_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_required_binary_field { - 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}} -{{#-last}} - -{{/-last}} -{{/formParams}} -{{/x-consumes-multipart-related}} -{{/vendorExtensions}} + Ok(body) => { + {{^vendorExtensions.x-consumes-multipart-form}} + {{^vendorExtensions.x-consumes-form}} + {{^bodyParam.vendorExtensions.x-consumes-plain-text}} + let mut unused_elements : Vec = vec![]; + {{/bodyParam.vendorExtensions.x-consumes-plain-text}} + {{/vendorExtensions.x-consumes-form}} + {{/vendorExtensions.x-consumes-multipart-form}} +{{>server-request-body-instance}} +{{/vendorExtensions.x-has-request-body}} let result = api_impl.{{#vendorExtensions}}{{{x-operation-id}}}{{/vendorExtensions}}( {{#vendorExtensions}} {{#x-callback-params}} @@ -491,19 +225,20 @@ HeaderValue::from_str((&context as &dyn Has).get().0.clone().as_str()) .expect("Unable to create X-Span-ID header value")); -{{#bodyParams}} -{{#vendorExtensions}} -{{^x-consumes-plain-text}} +{{#vendorExtensions.x-has-request-body}} + {{^vendorExtensions.x-consumes-multipart-form}} + {{^vendorExtensions.x-consumes-form}} + {{^bodyParam.vendorExtensions.x-consumes-plain-text}} if !unused_elements.is_empty() { response.headers_mut().insert( HeaderName::from_static("warning"), HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - -{{/x-consumes-plain-text}} -{{/vendorExtensions}} -{{/bodyParams}} + {{/bodyParam.vendorExtensions.x-consumes-plain-text}} + {{/vendorExtensions.x-consumes-form}} + {{/vendorExtensions.x-consumes-multipart-form}} +{{/vendorExtensions.x-has-request-body}} match result { Ok(rsp) => match rsp { {{#responses}} @@ -535,7 +270,9 @@ {{/headers}} {{/dataType}} => { + *response.status_mut() = StatusCode::from_u16({{{code}}}).expect("Unable to turn {{{code}}} into a StatusCode"); {{#headers}} + {{^required}} if let Some({{{name}}}) = {{{name}}} { {{/required}} @@ -557,45 +294,7 @@ } {{/required}} {{/headers}} - *response.status_mut() = StatusCode::from_u16({{{code}}}).expect("Unable to turn {{{code}}} into a StatusCode"); -{{#produces}} -{{#-first}} -{{#dataType}} -{{#vendorExtensions}} - response.headers_mut().insert( - CONTENT_TYPE, - HeaderValue::from_str("{{{x-mime-type}}}") - .expect("Unable to create Content-Type header for {{{x-uppercase-operation-id}}}_{{x-uppercase-response-id}}")); -{{/vendorExtensions}} -{{/dataType}} -{{/-first}} -{{/produces}} -{{#dataType}} -{{#vendorExtensions}} -{{#x-produces-xml}} -{{^x-has-namespace}} - let body_content = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); -{{/x-has-namespace}} -{{#x-has-namespace}} - let mut namespaces = std::collections::BTreeMap::new(); - - // An empty string is used to indicate a global namespace in xmltree. - namespaces.insert("".to_string(), {{{dataType}}}::NAMESPACE.to_string()); - let body_content = serde_xml_rs::to_string_with_namespaces(&body, namespaces).expect("impossible to fail to serialize"); -{{/x-has-namespace}} -{{/x-produces-xml}} -{{#x-produces-json}} - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); -{{/x-produces-json}} -{{#x-produces-bytes}} - let body_content = body.0; -{{/x-produces-bytes}} -{{#x-produces-plain-text}} - let body_content = body; -{{/x-produces-plain-text}} -{{/vendorExtensions}} - *response.body_mut() = Body::from(body_content); -{{/dataType}} +{{>server-response-body-instance}} }, {{/responses}} }, @@ -608,44 +307,12 @@ } Ok(response) -{{#vendorExtensions}} -{{^x-consumes-multipart}} -{{^bodyParams}} -{{#vendorExtensions}} -{{#x-consumes-multipart-related}} - }, - Err(e) => Ok(Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter {{{baseName}}}: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter {{{baseName}}}")), - } -{{/x-consumes-multipart-related}} -{{/vendorExtensions}} -{{/bodyParams}} -{{/x-consumes-multipart}} -{{/vendorExtensions}} -{{#bodyParams}} -{{#-first}} +{{#vendorExtensions.x-has-request-body}} }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter {{{baseName}}}: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter {{{baseName}}}")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } -{{/-first}} -{{/bodyParams}} -{{#vendorExtensions}} -{{#x-consumes-multipart}} -{{^bodyParams}} -{{#vendorExtensions}} - }, - Err(e) => Ok(Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(Body::from("Couldn't read multipart body".to_string())) - .expect("Unable to create Bad Request response due to unable read multipart body")), - } -{{/vendorExtensions}} -{{/bodyParams}} -{{/x-consumes-multipart}} -{{/vendorExtensions}} +{{/vendorExtensions.x-has-request-body}} }, diff --git a/modules/openapi-generator/src/main/resources/rust-server/server-request-body-basic.mustache b/modules/openapi-generator/src/main/resources/rust-server/server-request-body-basic.mustache new file mode 100644 index 000000000000..97422852ca75 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/rust-server/server-request-body-basic.mustache @@ -0,0 +1,52 @@ + {{#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}} diff --git a/modules/openapi-generator/src/main/resources/rust-server/server-request-body-form.mustache b/modules/openapi-generator/src/main/resources/rust-server/server-request-body-form.mustache new file mode 100644 index 000000000000..24021a86b7f0 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/rust-server/server-request-body-form.mustache @@ -0,0 +1,24 @@ +{{#vendorExtensions}} + {{#formParams}} + {{#-first}} + // Form parameters + {{/-first}} + let param_{{{paramName}}} = + {{^isContainer}} + {{#vendorExtensions}} + {{{x-example}}}; + {{/vendorExtensions}} + {{/isContainer}} + {{#isArray}} + {{#required}} + Vec::new(); + {{/required}} + {{^required}} + None; + {{/required}} + {{/isArray}} + {{#isMap}} + None; + {{/isMap}} + {{/formParams}} +{{/vendorExtensions}} diff --git a/modules/openapi-generator/src/main/resources/rust-server/server-request-body-instance.mustache b/modules/openapi-generator/src/main/resources/rust-server/server-request-body-instance.mustache new file mode 100644 index 000000000000..4fea15ea52c3 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/rust-server/server-request-body-instance.mustache @@ -0,0 +1,20 @@ +{{#vendorExtensions}} + {{#x-consumes-multipart}} + {{#x-consumes-multipart-related}} +{{>server-request-body-multipart-related}} + {{/x-consumes-multipart-related}} + {{^x-consumes-multipart-related}} + {{^bodyParams}} +{{>server-request-body-multipart-form}} + {{/bodyParams}} + {{/x-consumes-multipart-related}} + {{/x-consumes-multipart}} + {{^x-consumes-multipart}} + {{#bodyParams}} +{{>server-request-body-basic}} + {{/bodyParams}} + {{^bodyParams}} +{{>server-request-body-form}} + {{/bodyParams}} + {{/x-consumes-multipart}} +{{/vendorExtensions}} diff --git a/modules/openapi-generator/src/main/resources/rust-server/server-request-body-multipart-form.mustache b/modules/openapi-generator/src/main/resources/rust-server/server-request-body-multipart-form.mustache new file mode 100644 index 000000000000..4c594df5627b --- /dev/null +++ b/modules/openapi-generator/src/main/resources/rust-server/server-request-body-multipart-form.mustache @@ -0,0 +1,70 @@ + let boundary = match swagger::multipart::form::boundary(&headers) { + Some(boundary) => boundary.to_string(), + None => return Ok(Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(Body::from("Couldn't find valid multipart body".to_string())) + .expect("Unable to create Bad Request response for incorrect boundary")), + }; + + use std::io::Read; + + // Read Form Parameters from body + let mut entries = match Multipart::with_body(&body.to_vec()[..], boundary).save().temp() { + SaveResult::Full(entries) => { + entries + }, + _ => { + return Ok(Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(Body::from("Unable to process all message parts".to_string())) + .expect("Unable to create Bad Request response due to failure to process all message")) + }, + }; + {{#formParams}} + let field_{{{paramName}}} = entries.fields.remove("{{{paramName}}}"); + let param_{{{paramName}}} = match field_{{{paramName}}} { + Some(field) => { + let mut reader = field[0].data.readable().expect("Unable to read field for {{{paramName}}}"); + {{^required}} + Some({ + {{/required}} + {{#isByteArray}} + let mut data = vec![]; + reader.read_to_end(&mut data).expect("Reading saved binary data should never fail"); + swagger::ByteArray(data) + {{/isByteArray}} + {{^isByteArray}} + {{#jsonSchema}} + let mut data = String::new(); + reader.read_to_string(&mut data).expect("Reading saved String should never fail"); + let {{{paramName}}}_model: {{{dataType}}} = match serde_json::from_str(&data) { + Ok(model) => model, + Err(e) => { + return Ok( + Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(Body::from(format!("{{{paramName}}} data does not match API definition : {}", e))) + .expect("Unable to create Bad Request due to missing required form parameter {{{paramName}}}")) + } + }; + {{{paramName}}}_model + {{/jsonSchema}} + {{/isByteArray}} + {{^required}} + }) + {{/required}} + }, + None => { + {{#required}} + return Ok( + Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(Body::from("Missing required form parameter {{{paramName}}}".to_string())) + .expect("Unable to create Bad Request due to missing required form parameter {{{paramName}}}")) + {{/required}} + {{^required}} + None + {{/required}} + } + }; + {{/formParams}} diff --git a/modules/openapi-generator/src/main/resources/rust-server/server-request-body-multipart-related.mustache b/modules/openapi-generator/src/main/resources/rust-server/server-request-body-multipart-related.mustache new file mode 100644 index 000000000000..83f18f2df0c1 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/rust-server/server-request-body-multipart-related.mustache @@ -0,0 +1,96 @@ + // Get multipart chunks. + + // Extract the top-level content type header. + let content_type_mime = headers + .get(CONTENT_TYPE) + .ok_or_else(|| "Missing content-type header".to_string()) + .and_then(|v| v.to_str().map_err(|e| format!("Couldn't read content-type header value for {{operationId}}: {}", e))) + .and_then(|v| v.parse::().map_err(|_e| "Couldn't parse content-type header value for {{operationId}}".to_string())); + + // Insert top-level content type header into a Headers object. + let mut multi_part_headers = Headers::new(); + match content_type_mime { + Ok(content_type_mime) => { + multi_part_headers.set(ContentType(content_type_mime)); + }, + 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, &multi_part_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}} diff --git a/modules/openapi-generator/src/main/resources/rust-server/server-response-body-instance.mustache b/modules/openapi-generator/src/main/resources/rust-server/server-response-body-instance.mustache new file mode 100644 index 000000000000..9bb648c525f7 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/rust-server/server-response-body-instance.mustache @@ -0,0 +1,50 @@ +{{#dataType}} + {{#vendorExtensions}} + {{^x-produces-multipart-related}} + response.headers_mut().insert( + CONTENT_TYPE, + HeaderValue::from_str("{{{x-mime-type}}}") + .expect("Unable to create Content-Type header for {{{x-mime-type}}}")); + {{/x-produces-multipart-related}} + {{#x-produces-xml}} + // XML Body + {{^x-has-namespace}} + let body = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); + {{/x-has-namespace}} + {{#x-has-namespace}} + let mut namespaces = std::collections::BTreeMap::new(); + + // An empty string is used to indicate a global namespace in xmltree. + namespaces.insert("".to_string(), {{{dataType}}}::NAMESPACE.to_string()); + let body = serde_xml_rs::to_string_with_namespaces(&body, namespaces).expect("impossible to fail to serialize"); + {{/x-has-namespace}} + {{/x-produces-xml}} + {{#x-produces-json}} + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + {{/x-produces-json}} + {{#x-produces-bytes}} + // Binary Body + let body = body.0; + {{/x-produces-bytes}} + {{#x-produces-plain-text}} + // Plain text Body + let body = body; + {{/x-produces-plain-text}} + {{#x-produces-multipart-related}} + // multipart/related Body + {{#formParams}} + let param_{{{paramName}}} = body.{{{paramName}}}; + {{/formParams}} + {{#formParams}} +{{>generate-multipart-related}} + let header = "multipart/related"; + response.headers_mut().insert(CONTENT_TYPE, + HeaderValue::from_bytes( + &["multipart/related; boundary=".as_bytes(), &boundary].concat()) + .expect("Unable to create Content-Type header for multipart/related")); + {{/formParams}} + {{/x-produces-multipart-related}} + {{/vendorExtensions}} + *response.body_mut() = Body::from(body); +{{/dataType}} diff --git a/samples/server/petstore/rust-server/output/multipart-v3/src/client/mod.rs b/samples/server/petstore/rust-server/output/multipart-v3/src/client/mod.rs index eaea306af10e..2d52eec0a593 100644 --- a/samples/server/petstore/rust-server/output/multipart-v3/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/multipart-v3/src/client/mod.rs @@ -423,6 +423,7 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes multipart/related body // Construct the Body for a multipart/related request. The mime 0.2.6 library // does not parse quoted-string parameters correctly. The boundary doesn't // need to be a quoted string if it does not contain a '/', hence ensure @@ -433,7 +434,6 @@ impl Api for Client where *b = b'='; } } - let mut body_parts = vec![]; if let Some(object_field) = param_object_field { @@ -482,8 +482,6 @@ impl Api for Client where write_multipart(&mut body, &boundary, &body_parts) .expect("Failed to write multipart body"); - // Add the message body to the request object. - *request.body_mut() = Body::from(body); let header = "multipart/related"; request.headers_mut().insert(CONTENT_TYPE, @@ -494,6 +492,9 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + // Add the message body to the request object. + *request.body_mut() = Body::from(body); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -566,6 +567,7 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes multipart/form body let (body_string, multipart_header) = { let mut multipart = Multipart::new(); @@ -646,6 +648,7 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", multipart_header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -716,6 +719,7 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes multipart/related body // Construct the Body for a multipart/related request. The mime 0.2.6 library // does not parse quoted-string parameters correctly. The boundary doesn't // need to be a quoted string if it does not contain a '/', hence ensure @@ -726,7 +730,6 @@ impl Api for Client where *b = b'='; } } - let mut body_parts = vec![]; if let Some(binary1) = param_binary1 { @@ -760,8 +763,6 @@ impl Api for Client where write_multipart(&mut body, &boundary, &body_parts) .expect("Failed to write multipart body"); - // Add the message body to the request object. - *request.body_mut() = Body::from(body); let header = "multipart/related"; request.headers_mut().insert(CONTENT_TYPE, @@ -772,6 +773,9 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + // Add the message body to the request object. + *request.body_mut() = Body::from(body); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, diff --git a/samples/server/petstore/rust-server/output/multipart-v3/src/server/mod.rs b/samples/server/petstore/rust-server/output/multipart-v3/src/server/mod.rs index d8e45aac7115..d4ca550e5ea0 100644 --- a/samples/server/petstore/rust-server/output/multipart-v3/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/multipart-v3/src/server/mod.rs @@ -155,14 +155,13 @@ impl hyper::service::Service<(Request, C)> for Service where // MultipartRelatedRequestPost - POST /multipart_related_request hyper::Method::POST if path.matched(paths::ID_MULTIPART_RELATED_REQUEST) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. - let result = body.into_raw(); - match result.await { - Ok(body) => { - let mut unused_elements: Vec = vec![]; - + let result = body.into_raw().await; + match result { + Ok(body) => { + let mut unused_elements : Vec = vec![]; // Get multipart chunks. // Extract the top-level content type header. @@ -215,9 +214,9 @@ impl hyper::service::Service<(Request, C)> for Service where }) { Ok(json_data) => json_data, Err(e) => return Ok(Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't parse body parameter models::MultipartRequestObjectField - doesn't match schema: {}", e))) - .expect("Unable to create Bad Request response for invalid body parameter models::MultipartRequestObjectField due to schema")) + .status(StatusCode::BAD_REQUEST) + .body(Body::from(format!("Couldn't parse body parameter models::MultipartRequestObjectField - doesn't match schema: {}", e))) + .expect("Unable to create Bad Request response for invalid body parameter models::MultipartRequestObjectField due to schema")) }; // Push JSON part to return object. param_object_field.get_or_insert(json_data); @@ -246,11 +245,12 @@ impl hyper::service::Service<(Request, C)> for Service where let param_required_binary_field = match param_required_binary_field { Some(x) => x, None => return Ok(Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(Body::from("Missing required multipart/related parameter required_binary_field".to_string())) - .expect("Unable to create Bad Request response for missing multipart/related parameter required_binary_field due to schema")) + .status(StatusCode::BAD_REQUEST) + .body(Body::from("Missing required multipart/related parameter required_binary_field".to_string())) + .expect("Unable to create Bad Request response for missing multipart/related parameter required_binary_field due to schema")) }; + let result = api_impl.multipart_related_request_post( param_required_binary_field, param_object_field, @@ -263,11 +263,18 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str((&context as &dyn Has).get().0.clone().as_str()) .expect("Unable to create X-Span-ID header value")); + if !unused_elements.is_empty() { + response.headers_mut().insert( + HeaderName::from_static("warning"), + HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) + .expect("Unable to create Warning header value")); + } match result { Ok(rsp) => match rsp { MultipartRelatedRequestPostResponse::OK => { *response.status_mut() = StatusCode::from_u16(201).expect("Unable to turn 201 into a StatusCode"); + }, }, Err(_) => { @@ -282,27 +289,27 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter Default: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter Default")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // MultipartRequestPost - POST /multipart_request hyper::Method::POST if path.matched(paths::ID_MULTIPART_REQUEST) => { - let boundary = match swagger::multipart::form::boundary(&headers) { - Some(boundary) => boundary.to_string(), - None => return Ok(Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(Body::from("Couldn't find valid multipart body".to_string())) - .expect("Unable to create Bad Request response for incorrect boundary")), - }; - - // Form Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. - let result = body.into_raw(); - match result.await { - Ok(body) => { + let result = body.into_raw().await; + match result { + Ok(body) => { + let boundary = match swagger::multipart::form::boundary(&headers) { + Some(boundary) => boundary.to_string(), + None => return Ok(Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(Body::from("Couldn't find valid multipart body".to_string())) + .expect("Unable to create Bad Request response for incorrect boundary")), + }; + use std::io::Read; // Read Form Parameters from body @@ -407,6 +414,8 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request due to missing required form parameter binary_field")) } }; + + let result = api_impl.multipart_request_post( param_string_field, param_binary_field, @@ -425,6 +434,7 @@ impl hyper::service::Service<(Request, C)> for Service where MultipartRequestPostResponse::OK => { *response.status_mut() = StatusCode::from_u16(201).expect("Unable to turn 201 into a StatusCode"); + }, }, Err(_) => { @@ -439,21 +449,20 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from("Couldn't read multipart body".to_string())) - .expect("Unable to create Bad Request response due to unable read multipart body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // MultipleIdenticalMimeTypesPost - POST /multiple-identical-mime-types hyper::Method::POST if path.matched(paths::ID_MULTIPLE_IDENTICAL_MIME_TYPES) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. - let result = body.into_raw(); - match result.await { - Ok(body) => { - let mut unused_elements: Vec = vec![]; - + let result = body.into_raw().await; + match result { + Ok(body) => { + let mut unused_elements : Vec = vec![]; // Get multipart chunks. // Extract the top-level content type header. @@ -518,6 +527,7 @@ impl hyper::service::Service<(Request, C)> for Service where // Check that the required multipart chunks are present. + let result = api_impl.multiple_identical_mime_types_post( param_binary1, param_binary2, @@ -529,11 +539,18 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str((&context as &dyn Has).get().0.clone().as_str()) .expect("Unable to create X-Span-ID header value")); + if !unused_elements.is_empty() { + response.headers_mut().insert( + HeaderName::from_static("warning"), + HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) + .expect("Unable to create Warning header value")); + } match result { Ok(rsp) => match rsp { MultipleIdenticalMimeTypesPostResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -548,8 +565,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter Default: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter Default")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, diff --git a/samples/server/petstore/rust-server/output/no-example-v3/src/client/mod.rs b/samples/server/petstore/rust-server/output/no-example-v3/src/client/mod.rs index b8bcf7763395..218967ec7a60 100644 --- a/samples/server/petstore/rust-server/output/no-example-v3/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/no-example-v3/src/client/mod.rs @@ -414,10 +414,10 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body // Body parameter let body = serde_json::to_string(¶m_op_get_request).expect("impossible to fail to serialize"); - - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { diff --git a/samples/server/petstore/rust-server/output/no-example-v3/src/server/mod.rs b/samples/server/petstore/rust-server/output/no-example-v3/src/server/mod.rs index d00a957c0531..6146bbb51ad9 100644 --- a/samples/server/petstore/rust-server/output/no-example-v3/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/no-example-v3/src/server/mod.rs @@ -144,15 +144,15 @@ impl hyper::service::Service<(Request, C)> for Service where // OpGet - GET /op hyper::Method::GET if path.matched(paths::ID_OP) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_op_get_request: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -174,6 +174,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter OpGetRequest")), }; + let result = api_impl.op_get( param_op_get_request, &context @@ -190,12 +191,12 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { OpGetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -210,8 +211,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter OpGetRequest: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter OpGetRequest")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, diff --git a/samples/server/petstore/rust-server/output/openapi-v3/src/client/callbacks.rs b/samples/server/petstore/rust-server/output/openapi-v3/src/client/callbacks.rs index 172d284a43c6..71ba30387a53 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/src/client/callbacks.rs +++ b/samples/server/petstore/rust-server/output/openapi-v3/src/client/callbacks.rs @@ -204,6 +204,7 @@ impl hyper::service::Service<(Request, C)> for Service where CallbackCallbackWithHeaderPostResponse::OK => { *response.status_mut() = StatusCode::from_u16(204).expect("Unable to turn 204 into a StatusCode"); + }, }, Err(_) => { @@ -244,6 +245,7 @@ impl hyper::service::Service<(Request, C)> for Service where CallbackCallbackPostResponse::OK => { *response.status_mut() = StatusCode::from_u16(204).expect("Unable to turn 204 into a StatusCode"); + }, }, Err(_) => { diff --git a/samples/server/petstore/rust-server/output/openapi-v3/src/client/mod.rs b/samples/server/petstore/rust-server/output/openapi-v3/src/client/mod.rs index b71408c2422f..8b23bc38e850 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/openapi-v3/src/client/mod.rs @@ -460,11 +460,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(AnyOfGetResponse::Success (body) ) @@ -474,11 +476,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(AnyOfGetResponse::AlternateSuccess (body) ) @@ -488,11 +492,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(AnyOfGetResponse::AnyOfSuccess (body) ) @@ -936,11 +942,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(MergePatchJsonGetResponse::Merge (body) ) @@ -1013,11 +1021,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(MultigetGetResponse::JSONRsp (body) ) @@ -1027,12 +1037,15 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; // ToDo: this will move to swagger-rs and become a standard From conversion trait // once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream let body = serde_xml_rs::from_str::(body) .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(MultigetGetResponse::XMLRsp (body) ) @@ -1042,7 +1055,10 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = swagger::ByteArray(body.to_vec()); + + Ok(MultigetGetResponse::OctetRsp (body) ) @@ -1052,9 +1068,12 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; let body = body.to_string(); + + Ok(MultigetGetResponse::StringRsp (body) ) @@ -1064,11 +1083,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(MultigetGetResponse::DuplicateResponseLongText (body) ) @@ -1078,11 +1099,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(MultigetGetResponse::DuplicateResponseLongText_2 (body) ) @@ -1092,11 +1115,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(MultigetGetResponse::DuplicateResponseLongText_3 (body) ) @@ -1256,11 +1281,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(OneOfGetResponse::Success (body) ) @@ -1416,11 +1443,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(ParamgetGetResponse::JSONRsp (body) ) @@ -1637,14 +1666,17 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body + // Body parameter let body = param_body.0; - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/octet-stream"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -1770,11 +1802,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(ResponsesWithHeadersGetResponse::Success { body, @@ -1889,11 +1923,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(Rfc7807GetResponse::OK (body) ) @@ -1903,11 +1939,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(Rfc7807GetResponse::NotFound (body) ) @@ -1917,12 +1955,15 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; // ToDo: this will move to swagger-rs and become a standard From conversion trait // once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream let body = serde_xml_rs::from_str::(body) .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(Rfc7807GetResponse::NotAcceptable (body) ) @@ -1981,11 +2022,11 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; - let body = param_object_untyped_props.map(|ref body| { - serde_json::to_string(body).expect("impossible to fail to serialize") - }); - if let Some(body) = body { - *request.body_mut() = Body::from(body); + // Consumes basic body + // Body parameter + if let Some(param_object_untyped_props) = param_object_untyped_props { + let body = serde_json::to_string(¶m_object_untyped_props).expect("impossible to fail to serialize"); + *request.body_mut() = Body::from(body); } let header = "application/json"; @@ -1993,6 +2034,7 @@ impl Api for Client where Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -2076,11 +2118,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(UuidGetResponse::DuplicateResponseLongText (body) ) @@ -2139,11 +2183,11 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; - let body = param_duplicate_xml_object.map(|ref body| { - body.as_xml() - }); - if let Some(body) = body { - *request.body_mut() = Body::from(body); + // Consumes basic body + // Body parameter + if let Some(param_duplicate_xml_object) = param_duplicate_xml_object { + let body = param_duplicate_xml_object.as_xml(); + *request.body_mut() = Body::from(body); } let header = "application/xml"; @@ -2151,6 +2195,7 @@ impl Api for Client where Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -2225,11 +2270,11 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; - let body = param_another_xml_object.map(|ref body| { - body.as_xml() - }); - if let Some(body) = body { - *request.body_mut() = Body::from(body); + // Consumes basic body + // Body parameter + if let Some(param_another_xml_object) = param_another_xml_object { + let body = param_another_xml_object.as_xml(); + *request.body_mut() = Body::from(body); } let header = "text/xml"; @@ -2237,6 +2282,7 @@ impl Api for Client where Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -2252,12 +2298,15 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; // ToDo: this will move to swagger-rs and become a standard From conversion trait // once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream let body = serde_xml_rs::from_str::(body) .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(XmlOtherPostResponse::OK (body) ) @@ -2321,11 +2370,11 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; - let body = param_another_xml_array.map(|ref body| { - body.as_xml() - }); - if let Some(body) = body { - *request.body_mut() = Body::from(body); + // Consumes basic body + // Body parameter + if let Some(param_another_xml_array) = param_another_xml_array { + let body = param_another_xml_array.as_xml(); + *request.body_mut() = Body::from(body); } let header = "application/xml"; @@ -2333,6 +2382,7 @@ impl Api for Client where Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -2407,11 +2457,11 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; - let body = param_xml_array.map(|ref body| { - body.as_xml() - }); - if let Some(body) = body { - *request.body_mut() = Body::from(body); + // Consumes basic body + // Body parameter + if let Some(param_xml_array) = param_xml_array { + let body = param_xml_array.as_xml(); + *request.body_mut() = Body::from(body); } let header = "application/xml"; @@ -2419,6 +2469,7 @@ impl Api for Client where Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -2493,12 +2544,11 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; - let body = param_xml_object.map(|ref body| { - body.as_xml() - }); - - if let Some(body) = body { - *request.body_mut() = Body::from(body); + // Consumes basic body + // Body parameter + if let Some(param_xml_object) = param_xml_object { + let body = param_xml_object.as_xml(); + *request.body_mut() = Body::from(body); } let header = "application/xml"; @@ -2581,15 +2631,17 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body // Body parameter let body = serde_json::to_string(¶m_object_param).expect("impossible to fail to serialize"); - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -2675,11 +2727,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(GetRepoInfoResponse::OK (body) ) diff --git a/samples/server/petstore/rust-server/output/openapi-v3/src/server/mod.rs b/samples/server/petstore/rust-server/output/openapi-v3/src/server/mod.rs index 0c25b4b8887a..b0de9a68581a 100644 --- a/samples/server/petstore/rust-server/output/openapi-v3/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/openapi-v3/src/server/mod.rs @@ -259,9 +259,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for ANY_OF_GET_SUCCESS")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, AnyOfGetResponse::AlternateSuccess (body) @@ -270,9 +272,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for ANY_OF_GET_ALTERNATE_SUCCESS")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, AnyOfGetResponse::AnyOfSuccess (body) @@ -281,9 +285,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for ANY_OF_GET_ANY_OF_SUCCESS")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -341,6 +347,7 @@ impl hyper::service::Service<(Request, C)> for Service where CallbackWithHeaderPostResponse::OK => { *response.status_mut() = StatusCode::from_u16(204).expect("Unable to turn 204 into a StatusCode"); + }, }, Err(_) => { @@ -382,6 +389,7 @@ impl hyper::service::Service<(Request, C)> for Service where ComplexQueryParamGetResponse::Success => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -435,6 +443,7 @@ impl hyper::service::Service<(Request, C)> for Service where EnumInPathPathParamGetResponse::Success => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -485,6 +494,7 @@ impl hyper::service::Service<(Request, C)> for Service where JsonComplexQueryParamGetResponse::Success => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -538,6 +548,7 @@ impl hyper::service::Service<(Request, C)> for Service where MandatoryRequestHeaderGetResponse::Success => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -571,9 +582,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/merge-patch+json") - .expect("Unable to create Content-Type header for MERGE_PATCH_JSON_GET_MERGE")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/merge-patch+json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -607,9 +620,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for MULTIGET_GET_JSON_RSP")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, MultigetGetResponse::XMLRsp (body) @@ -618,9 +633,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/xml") - .expect("Unable to create Content-Type header for MULTIGET_GET_XML_RSP")); - let body_content = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/xml")); + // XML Body + let body = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, MultigetGetResponse::OctetRsp (body) @@ -629,9 +646,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/octet-stream") - .expect("Unable to create Content-Type header for MULTIGET_GET_OCTET_RSP")); - let body_content = body.0; - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/octet-stream")); + // Binary Body + let body = body.0; + *response.body_mut() = Body::from(body); + }, MultigetGetResponse::StringRsp (body) @@ -640,9 +659,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("text/plain") - .expect("Unable to create Content-Type header for MULTIGET_GET_STRING_RSP")); - let body_content = body; - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for text/plain")); + // Plain text Body + let body = body; + *response.body_mut() = Body::from(body); + }, MultigetGetResponse::DuplicateResponseLongText (body) @@ -651,9 +672,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for MULTIGET_GET_DUPLICATE_RESPONSE_LONG_TEXT")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, MultigetGetResponse::DuplicateResponseLongText_2 (body) @@ -662,9 +685,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for MULTIGET_GET_DUPLICATE_RESPONSE_LONG_TEXT_2")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, MultigetGetResponse::DuplicateResponseLongText_3 (body) @@ -673,9 +698,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for MULTIGET_GET_DUPLICATE_RESPONSE_LONG_TEXT_3")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -735,6 +762,7 @@ impl hyper::service::Service<(Request, C)> for Service where MultipleAuthSchemeGetResponse::CheckThatLimitingToMultipleRequiredAuthSchemesWorks => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -768,9 +796,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for ONE_OF_GET_SUCCESS")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -800,6 +830,7 @@ impl hyper::service::Service<(Request, C)> for Service where OverrideServerGetResponse::Success => { *response.status_mut() = StatusCode::from_u16(204).expect("Unable to turn 204 into a StatusCode"); + }, }, Err(_) => { @@ -890,9 +921,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for PARAMGET_GET_JSON_RSP")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -951,6 +984,7 @@ impl hyper::service::Service<(Request, C)> for Service where ReadonlyAuthSchemeGetResponse::CheckThatLimitingToASingleRequiredAuthSchemeWorks => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1008,6 +1042,7 @@ impl hyper::service::Service<(Request, C)> for Service where RegisterCallbackPostResponse::OK => { *response.status_mut() = StatusCode::from_u16(204).expect("Unable to turn 204 into a StatusCode"); + }, }, Err(_) => { @@ -1023,12 +1058,12 @@ impl hyper::service::Service<(Request, C)> for Service where // RequiredOctetStreamPut - PUT /required_octet_stream hyper::Method::PUT if path.matched(paths::ID_REQUIRED_OCTET_STREAM) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { + Ok(body) => { let param_body: Option = if !body.is_empty() { Some(swagger::ByteArray(body.to_vec())) } else { @@ -1042,6 +1077,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter body")), }; + let result = api_impl.required_octet_stream_put( param_body, &context @@ -1057,6 +1093,7 @@ impl hyper::service::Service<(Request, C)> for Service where RequiredOctetStreamPutResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1071,8 +1108,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, @@ -1097,6 +1134,8 @@ impl hyper::service::Service<(Request, C)> for Service where object_header } => { + *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + let success_info = match header::IntoHeaderValue(success_info).try_into() { Ok(val) => val, Err(e) => { @@ -1111,6 +1150,7 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderName::from_static("success-info"), success_info ); + if let Some(bool_header) = bool_header { let bool_header = match header::IntoHeaderValue(bool_header).try_into() { Ok(val) => val, @@ -1127,6 +1167,7 @@ impl hyper::service::Service<(Request, C)> for Service where bool_header ); } + if let Some(object_header) = object_header { let object_header = match header::IntoHeaderValue(object_header).try_into() { Ok(val) => val, @@ -1143,13 +1184,14 @@ impl hyper::service::Service<(Request, C)> for Service where object_header ); } - *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for RESPONSES_WITH_HEADERS_GET_SUCCESS")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, ResponsesWithHeadersGetResponse::PreconditionFailed { @@ -1157,6 +1199,8 @@ impl hyper::service::Service<(Request, C)> for Service where failure_info } => { + *response.status_mut() = StatusCode::from_u16(412).expect("Unable to turn 412 into a StatusCode"); + if let Some(further_info) = further_info { let further_info = match header::IntoHeaderValue(further_info).try_into() { Ok(val) => val, @@ -1173,6 +1217,7 @@ impl hyper::service::Service<(Request, C)> for Service where further_info ); } + if let Some(failure_info) = failure_info { let failure_info = match header::IntoHeaderValue(failure_info).try_into() { Ok(val) => val, @@ -1189,7 +1234,7 @@ impl hyper::service::Service<(Request, C)> for Service where failure_info ); } - *response.status_mut() = StatusCode::from_u16(412).expect("Unable to turn 412 into a StatusCode"); + }, }, Err(_) => { @@ -1223,9 +1268,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for RFC7807_GET_OK")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, Rfc7807GetResponse::NotFound (body) @@ -1234,9 +1281,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/problem+json") - .expect("Unable to create Content-Type header for RFC7807_GET_NOT_FOUND")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/problem+json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, Rfc7807GetResponse::NotAcceptable (body) @@ -1245,9 +1294,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/problem+xml") - .expect("Unable to create Content-Type header for RFC7807_GET_NOT_ACCEPTABLE")); - let body_content = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/problem+xml")); + // XML Body + let body = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -1263,15 +1314,15 @@ impl hyper::service::Service<(Request, C)> for Service where // UntypedPropertyGet - GET /untyped_property hyper::Method::GET if path.matched(paths::ID_UNTYPED_PROPERTY) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_object_untyped_props: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -1283,6 +1334,7 @@ impl hyper::service::Service<(Request, C)> for Service where None }; + let result = api_impl.untyped_property_get( param_object_untyped_props, &context @@ -1299,12 +1351,12 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { UntypedPropertyGetResponse::CheckThatUntypedPropertiesWorks => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1319,8 +1371,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter ObjectUntypedProps: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter ObjectUntypedProps")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, @@ -1344,9 +1396,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for UUID_GET_DUPLICATE_RESPONSE_LONG_TEXT")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -1362,13 +1416,13 @@ impl hyper::service::Service<(Request, C)> for Service where // XmlExtraPost - POST /xml_extra hyper::Method::POST if path.matched(paths::ID_XML_EXTRA) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_duplicate_xml_object: Option = if !body.is_empty() { let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body); match serde_ignored::deserialize(deserializer, |path| { @@ -1382,6 +1436,7 @@ impl hyper::service::Service<(Request, C)> for Service where None }; + let result = api_impl.xml_extra_post( param_duplicate_xml_object, &context @@ -1398,16 +1453,17 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { XmlExtraPostResponse::OK => { *response.status_mut() = StatusCode::from_u16(201).expect("Unable to turn 201 into a StatusCode"); + }, XmlExtraPostResponse::BadRequest => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, }, Err(_) => { @@ -1422,20 +1478,20 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter DuplicateXmlObject: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter DuplicateXmlObject")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // XmlOtherPost - POST /xml_other hyper::Method::POST if path.matched(paths::ID_XML_OTHER) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_another_xml_object: Option = if !body.is_empty() { let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body); match serde_ignored::deserialize(deserializer, |path| { @@ -1449,6 +1505,7 @@ impl hyper::service::Service<(Request, C)> for Service where None }; + let result = api_impl.xml_other_post( param_another_xml_object, &context @@ -1465,7 +1522,6 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { XmlOtherPostResponse::OK @@ -1475,17 +1531,20 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("text/xml") - .expect("Unable to create Content-Type header for XML_OTHER_POST_OK")); + .expect("Unable to create Content-Type header for text/xml")); + // XML Body let mut namespaces = std::collections::BTreeMap::new(); // An empty string is used to indicate a global namespace in xmltree. namespaces.insert("".to_string(), models::AnotherXmlObject::NAMESPACE.to_string()); - let body_content = serde_xml_rs::to_string_with_namespaces(&body, namespaces).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + let body = serde_xml_rs::to_string_with_namespaces(&body, namespaces).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, XmlOtherPostResponse::BadRequest => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, }, Err(_) => { @@ -1500,20 +1559,20 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter AnotherXmlObject: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter AnotherXmlObject")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // XmlOtherPut - PUT /xml_other hyper::Method::PUT if path.matched(paths::ID_XML_OTHER) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_another_xml_array: Option = if !body.is_empty() { let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body); match serde_ignored::deserialize(deserializer, |path| { @@ -1527,6 +1586,7 @@ impl hyper::service::Service<(Request, C)> for Service where None }; + let result = api_impl.xml_other_put( param_another_xml_array, &context @@ -1543,16 +1603,17 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { XmlOtherPutResponse::OK => { *response.status_mut() = StatusCode::from_u16(201).expect("Unable to turn 201 into a StatusCode"); + }, XmlOtherPutResponse::BadRequest => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, }, Err(_) => { @@ -1567,20 +1628,20 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter AnotherXmlArray: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter AnotherXmlArray")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // XmlPost - POST /xml hyper::Method::POST if path.matched(paths::ID_XML) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_xml_array: Option = if !body.is_empty() { let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body); match serde_ignored::deserialize(deserializer, |path| { @@ -1594,6 +1655,7 @@ impl hyper::service::Service<(Request, C)> for Service where None }; + let result = api_impl.xml_post( param_xml_array, &context @@ -1610,16 +1672,17 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { XmlPostResponse::OK => { *response.status_mut() = StatusCode::from_u16(201).expect("Unable to turn 201 into a StatusCode"); + }, XmlPostResponse::BadRequest => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, }, Err(_) => { @@ -1634,20 +1697,20 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter XmlArray: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter XmlArray")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // XmlPut - PUT /xml hyper::Method::PUT if path.matched(paths::ID_XML) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_xml_object: Option = if !body.is_empty() { let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body); match serde_ignored::deserialize(deserializer, |path| { @@ -1661,6 +1724,7 @@ impl hyper::service::Service<(Request, C)> for Service where None }; + let result = api_impl.xml_put( param_xml_object, &context @@ -1677,16 +1741,17 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { XmlPutResponse::OK => { *response.status_mut() = StatusCode::from_u16(201).expect("Unable to turn 201 into a StatusCode"); + }, XmlPutResponse::BadRequest => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, }, Err(_) => { @@ -1701,22 +1766,22 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter XmlObject: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter XmlObject")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // CreateRepo - POST /repos hyper::Method::POST if path.matched(paths::ID_REPOS) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_object_param: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -1738,6 +1803,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter ObjectParam")), }; + let result = api_impl.create_repo( param_object_param, &context @@ -1754,12 +1820,12 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { CreateRepoResponse::Success => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1774,8 +1840,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter ObjectParam: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter ObjectParam")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, @@ -1823,9 +1889,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for GET_REPO_INFO_OK")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { diff --git a/samples/server/petstore/rust-server/output/ops-v3/src/server/mod.rs b/samples/server/petstore/rust-server/output/ops-v3/src/server/mod.rs index 80e243436911..c4c97c93de39 100644 --- a/samples/server/petstore/rust-server/output/ops-v3/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/ops-v3/src/server/mod.rs @@ -266,6 +266,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op10GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -295,6 +296,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op11GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -324,6 +326,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op12GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -353,6 +356,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op13GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -382,6 +386,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op14GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -411,6 +416,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op15GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -440,6 +446,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op16GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -469,6 +476,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op17GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -498,6 +506,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op18GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -527,6 +536,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op19GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -556,6 +566,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op1GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -585,6 +596,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op20GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -614,6 +626,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op21GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -643,6 +656,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op22GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -672,6 +686,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op23GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -701,6 +716,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op24GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -730,6 +746,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op25GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -759,6 +776,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op26GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -788,6 +806,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op27GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -817,6 +836,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op28GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -846,6 +866,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op29GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -875,6 +896,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op2GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -904,6 +926,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op30GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -933,6 +956,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op31GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -962,6 +986,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op32GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -991,6 +1016,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op33GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1020,6 +1046,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op34GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1049,6 +1076,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op35GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1078,6 +1106,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op36GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1107,6 +1136,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op37GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1136,6 +1166,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op3GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1165,6 +1196,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op4GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1194,6 +1226,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op5GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1223,6 +1256,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op6GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1252,6 +1286,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op7GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1281,6 +1316,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op8GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1310,6 +1346,7 @@ impl hyper::service::Service<(Request, C)> for Service where Op9GetResponse::OK => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { diff --git a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs index 625e7aea9fa3..7657258a4069 100644 --- a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs @@ -451,10 +451,10 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body // Body parameter let body = serde_json::to_string(¶m_body).expect("impossible to fail to serialize"); - - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { @@ -477,11 +477,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(TestSpecialTagsResponse::SuccessfulOperation (body) ) @@ -608,11 +610,11 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; - let body = param_body.map(|ref body| { - serde_json::to_string(body).expect("impossible to fail to serialize") - }); - if let Some(body) = body { - *request.body_mut() = Body::from(body); + // Consumes basic body + // Body parameter + if let Some(param_body) = param_body { + let body = serde_json::to_string(¶m_body).expect("impossible to fail to serialize"); + *request.body_mut() = Body::from(body); } let header = "application/json"; @@ -620,6 +622,7 @@ impl Api for Client where Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -635,11 +638,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(FakeOuterBooleanSerializeResponse::OutputBoolean (body) ) @@ -698,11 +703,11 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; - let body = param_body.map(|ref body| { - serde_json::to_string(body).expect("impossible to fail to serialize") - }); - if let Some(body) = body { - *request.body_mut() = Body::from(body); + // Consumes basic body + // Body parameter + if let Some(param_body) = param_body { + let body = serde_json::to_string(¶m_body).expect("impossible to fail to serialize"); + *request.body_mut() = Body::from(body); } let header = "application/json"; @@ -710,6 +715,7 @@ impl Api for Client where Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -725,11 +731,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(FakeOuterCompositeSerializeResponse::OutputComposite (body) ) @@ -788,11 +796,11 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; - let body = param_body.map(|ref body| { - serde_json::to_string(body).expect("impossible to fail to serialize") - }); - if let Some(body) = body { - *request.body_mut() = Body::from(body); + // Consumes basic body + // Body parameter + if let Some(param_body) = param_body { + let body = serde_json::to_string(¶m_body).expect("impossible to fail to serialize"); + *request.body_mut() = Body::from(body); } let header = "application/json"; @@ -800,6 +808,7 @@ impl Api for Client where Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -815,11 +824,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(FakeOuterNumberSerializeResponse::OutputNumber (body) ) @@ -878,11 +889,11 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; - let body = param_body.map(|ref body| { - serde_json::to_string(body).expect("impossible to fail to serialize") - }); - if let Some(body) = body { - *request.body_mut() = Body::from(body); + // Consumes basic body + // Body parameter + if let Some(param_body) = param_body { + let body = serde_json::to_string(¶m_body).expect("impossible to fail to serialize"); + *request.body_mut() = Body::from(body); } let header = "application/json"; @@ -890,6 +901,7 @@ impl Api for Client where Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -905,11 +917,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(FakeOuterStringSerializeResponse::OutputString (body) ) @@ -1109,14 +1123,17 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body + // Body parameter let body = serde_json::to_string(¶m_body).expect("impossible to fail to serialize"); - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -1186,14 +1203,17 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body + // Body parameter let body = serde_json::to_string(¶m_body).expect("impossible to fail to serialize"); - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -1209,11 +1229,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(TestClientModelResponse::SuccessfulOperation (body) ) @@ -1285,6 +1307,7 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes form body let params = &[ ("integer", param_integer.map(|param| format!("{:?}", param))), ("int32", param_int32.map(|param| format!("{:?}", param))), @@ -1303,12 +1326,14 @@ impl Api for Client where ]; let body = serde_urlencoded::to_string(params).expect("impossible to fail to serialize"); + *request.body_mut() = Body::from(body.into_bytes()); + let header = "application/x-www-form-urlencoded"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); - *request.body_mut() = Body::from(body.into_bytes()); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -1424,17 +1449,20 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes form body let params = &[ ("enum_form_string", param_enum_form_string), ]; let body = serde_urlencoded::to_string(params).expect("impossible to fail to serialize"); + *request.body_mut() = Body::from(body.into_bytes()); + let header = "application/x-www-form-urlencoded"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); - *request.body_mut() = Body::from(body.into_bytes()); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -1544,14 +1572,17 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body + // Body parameter let body = serde_json::to_string(¶m_param).expect("impossible to fail to serialize"); - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -1622,18 +1653,21 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes form body let params = &[ ("param", Some(param_param)), ("param2", Some(param_param2)), ]; let body = serde_urlencoded::to_string(params).expect("impossible to fail to serialize"); + *request.body_mut() = Body::from(body.into_bytes()); + let header = "application/x-www-form-urlencoded"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); - *request.body_mut() = Body::from(body.into_bytes()); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -1706,10 +1740,10 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body // Body parameter let body = serde_json::to_string(¶m_body).expect("impossible to fail to serialize"); - - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { @@ -1741,11 +1775,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(TestClassnameResponse::SuccessfulOperation (body) ) @@ -1804,15 +1840,17 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body // Body parameter let body = param_body.as_xml(); - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -2045,12 +2083,15 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; // ToDo: this will move to swagger-rs and become a standard From conversion trait // once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream let body = serde_xml_rs::from_str::>(body) .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(FindPetsByStatusResponse::SuccessfulOperation (body) ) @@ -2150,12 +2191,15 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; // ToDo: this will move to swagger-rs and become a standard From conversion trait // once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream let body = serde_xml_rs::from_str::>(body) .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(FindPetsByTagsResponse::SuccessfulOperation (body) ) @@ -2244,12 +2288,15 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; // ToDo: this will move to swagger-rs and become a standard From conversion trait // once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream let body = serde_xml_rs::from_str::(body) .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(GetPetByIdResponse::SuccessfulOperation (body) ) @@ -2318,14 +2365,17 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body + // Body parameter let body = param_body.as_xml(); - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -2427,18 +2477,21 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes form body let params = &[ ("name", param_name), ("status", param_status), ]; let body = serde_urlencoded::to_string(params).expect("impossible to fail to serialize"); + *request.body_mut() = Body::from(body.into_bytes()); + let header = "application/x-www-form-urlencoded"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); - *request.body_mut() = Body::from(body.into_bytes()); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -2530,6 +2583,7 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes multipart/form body let (body_string, multipart_header) = { let mut multipart = Multipart::new(); @@ -2585,6 +2639,7 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", multipart_header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -2619,11 +2674,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(UploadFileResponse::SuccessfulOperation (body) ) @@ -2780,11 +2837,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::>(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::>(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(GetInventoryResponse::SuccessfulOperation (body) ) @@ -2859,12 +2918,15 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; // ToDo: this will move to swagger-rs and become a standard From conversion trait // once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream let body = serde_xml_rs::from_str::(body) .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(GetOrderByIdResponse::SuccessfulOperation (body) ) @@ -2933,9 +2995,10 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body + // Body parameter let body = serde_json::to_string(¶m_body).expect("impossible to fail to serialize"); - - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { @@ -2958,12 +3021,15 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; // ToDo: this will move to swagger-rs and become a standard From conversion trait // once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream let body = serde_xml_rs::from_str::(body) .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(PlaceOrderResponse::SuccessfulOperation (body) ) @@ -3027,15 +3093,17 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body // Body parameter let body = serde_json::to_string(¶m_body).expect("impossible to fail to serialize"); - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -3105,14 +3173,17 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body + // Body parameter let body = serde_json::to_string(¶m_body).expect("impossible to fail to serialize"); - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -3182,14 +3253,17 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body + // Body parameter let body = serde_json::to_string(¶m_body).expect("impossible to fail to serialize"); - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -3350,12 +3424,15 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; // ToDo: this will move to swagger-rs and become a standard From conversion trait // once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream let body = serde_xml_rs::from_str::(body) .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(GetUserByNameResponse::SuccessfulOperation (body) ) @@ -3472,12 +3549,15 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; // ToDo: this will move to swagger-rs and become a standard From conversion trait // once https://github.com/RReverser/serde-xml-rs/pull/45 is accepted upstream let body = serde_xml_rs::from_str::(body) .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(LoginUserResponse::SuccessfulOperation { body, @@ -3615,9 +3695,10 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body + // Body parameter let body = serde_json::to_string(¶m_body).expect("impossible to fail to serialize"); - - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { diff --git a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/server/mod.rs b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/server/mod.rs index 887af3ab9bf5..ca9745ecf373 100644 --- a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/server/mod.rs @@ -262,15 +262,15 @@ impl hyper::service::Service<(Request, C)> for Service where // TestSpecialTags - PATCH /another-fake/dummy hyper::Method::PATCH if path.matched(paths::ID_ANOTHER_FAKE_DUMMY) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -292,6 +292,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter body")), }; + let result = api_impl.test_special_tags( param_body, &context @@ -308,7 +309,6 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { TestSpecialTagsResponse::SuccessfulOperation @@ -318,9 +318,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for TEST_SPECIAL_TAGS_SUCCESSFUL_OPERATION")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -335,8 +337,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, @@ -356,6 +358,7 @@ impl hyper::service::Service<(Request, C)> for Service where Call123exampleResponse::Success => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -371,15 +374,15 @@ impl hyper::service::Service<(Request, C)> for Service where // FakeOuterBooleanSerialize - POST /fake/outer/boolean hyper::Method::POST if path.matched(paths::ID_FAKE_OUTER_BOOLEAN) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -391,6 +394,7 @@ impl hyper::service::Service<(Request, C)> for Service where None }; + let result = api_impl.fake_outer_boolean_serialize( param_body, &context @@ -407,7 +411,6 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { FakeOuterBooleanSerializeResponse::OutputBoolean @@ -417,9 +420,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("*/*") - .expect("Unable to create Content-Type header for FAKE_OUTER_BOOLEAN_SERIALIZE_OUTPUT_BOOLEAN")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for */*")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -434,22 +439,22 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // FakeOuterCompositeSerialize - POST /fake/outer/composite hyper::Method::POST if path.matched(paths::ID_FAKE_OUTER_COMPOSITE) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -461,6 +466,7 @@ impl hyper::service::Service<(Request, C)> for Service where None }; + let result = api_impl.fake_outer_composite_serialize( param_body, &context @@ -477,7 +483,6 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { FakeOuterCompositeSerializeResponse::OutputComposite @@ -487,9 +492,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("*/*") - .expect("Unable to create Content-Type header for FAKE_OUTER_COMPOSITE_SERIALIZE_OUTPUT_COMPOSITE")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for */*")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -504,22 +511,22 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // FakeOuterNumberSerialize - POST /fake/outer/number hyper::Method::POST if path.matched(paths::ID_FAKE_OUTER_NUMBER) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -531,6 +538,7 @@ impl hyper::service::Service<(Request, C)> for Service where None }; + let result = api_impl.fake_outer_number_serialize( param_body, &context @@ -547,7 +555,6 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { FakeOuterNumberSerializeResponse::OutputNumber @@ -557,9 +564,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("*/*") - .expect("Unable to create Content-Type header for FAKE_OUTER_NUMBER_SERIALIZE_OUTPUT_NUMBER")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for */*")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -574,22 +583,22 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // FakeOuterStringSerialize - POST /fake/outer/string hyper::Method::POST if path.matched(paths::ID_FAKE_OUTER_STRING) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -601,6 +610,7 @@ impl hyper::service::Service<(Request, C)> for Service where None }; + let result = api_impl.fake_outer_string_serialize( param_body, &context @@ -617,7 +627,6 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { FakeOuterStringSerializeResponse::OutputString @@ -627,9 +636,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("*/*") - .expect("Unable to create Content-Type header for FAKE_OUTER_STRING_SERIALIZE_OUTPUT_STRING")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for */*")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -644,8 +655,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, @@ -665,6 +676,7 @@ impl hyper::service::Service<(Request, C)> for Service where FakeResponseWithNumericalDescriptionResponse::Status200 => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -718,6 +730,7 @@ impl hyper::service::Service<(Request, C)> for Service where HyphenParamResponse::Success => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -760,15 +773,15 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing query parameter query")), }; - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -790,6 +803,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter body")), }; + let result = api_impl.test_body_with_query_params( param_query, param_body, @@ -807,12 +821,12 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { TestBodyWithQueryParamsResponse::Success => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -827,22 +841,22 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // TestClientModel - PATCH /fake hyper::Method::PATCH if path.matched(paths::ID_FAKE) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -864,6 +878,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter body")), }; + let result = api_impl.test_client_model( param_body, &context @@ -880,7 +895,6 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { TestClientModelResponse::SuccessfulOperation @@ -890,9 +904,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for TEST_CLIENT_MODEL_SUCCESSFUL_OPERATION")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -907,8 +923,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, @@ -924,21 +940,42 @@ impl hyper::service::Service<(Request, C)> for Service where }; } + // Handle body parameters (note that non-required body parameters will ignore garbage + // values, rather than causing a 400 response). Produce warning header and logs for + // any unused fields. + let result = body.into_raw().await; + match result { + Ok(body) => { // Form parameters - let param_integer = Some(56); - let param_int32 = Some(56); - let param_int64 = Some(789); - let param_number = 8.14; - let param_float = Some(3.4); - let param_double = 1.2; - let param_string = Some("string_example".to_string()); - let param_pattern_without_delimiter = "pattern_without_delimiter_example".to_string(); - let param_byte = swagger::ByteArray(Vec::from("BYTE_ARRAY_DATA_HERE")); - let param_binary = Some(swagger::ByteArray(Vec::from("BINARY_DATA_HERE"))); - let param_date = None; - let param_date_time = None; - let param_password = Some("password_example".to_string()); - let param_callback = Some("callback_example".to_string()); + let param_integer = + Some(56); + let param_int32 = + Some(56); + let param_int64 = + Some(789); + let param_number = + 8.14; + let param_float = + Some(3.4); + let param_double = + 1.2; + let param_string = + Some("string_example".to_string()); + let param_pattern_without_delimiter = + "pattern_without_delimiter_example".to_string(); + let param_byte = + swagger::ByteArray(Vec::from("BYTE_ARRAY_DATA_HERE")); + let param_binary = + Some(swagger::ByteArray(Vec::from("BINARY_DATA_HERE"))); + let param_date = + None; + let param_date_time = + None; + let param_password = + Some("password_example".to_string()); + let param_callback = + Some("callback_example".to_string()); + let result = api_impl.test_endpoint_parameters( param_number, @@ -968,10 +1005,12 @@ impl hyper::service::Service<(Request, C)> for Service where TestEndpointParametersResponse::InvalidUsernameSupplied => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, TestEndpointParametersResponse::UserNotFound => { *response.status_mut() = StatusCode::from_u16(404).expect("Unable to turn 404 into a StatusCode"); + }, }, Err(_) => { @@ -983,6 +1022,12 @@ impl hyper::service::Service<(Request, C)> for Service where } Ok(response) + }, + Err(e) => Ok(Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), + } }, // TestEnumParameters - GET /fake @@ -1087,8 +1132,16 @@ impl hyper::service::Service<(Request, C)> for Service where None => None, }; + // Handle body parameters (note that non-required body parameters will ignore garbage + // values, rather than causing a 400 response). Produce warning header and logs for + // any unused fields. + let result = body.into_raw().await; + match result { + Ok(body) => { // Form parameters - let param_enum_form_string = Some("enum_form_string_example".to_string()); + let param_enum_form_string = + Some("enum_form_string_example".to_string()); + let result = api_impl.test_enum_parameters( param_enum_header_string_array.as_ref(), @@ -1111,10 +1164,12 @@ impl hyper::service::Service<(Request, C)> for Service where TestEnumParametersResponse::InvalidRequest => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, TestEnumParametersResponse::NotFound => { *response.status_mut() = StatusCode::from_u16(404).expect("Unable to turn 404 into a StatusCode"); + }, }, Err(_) => { @@ -1126,19 +1181,25 @@ impl hyper::service::Service<(Request, C)> for Service where } Ok(response) + }, + Err(e) => Ok(Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), + } }, // TestInlineAdditionalProperties - POST /fake/inline-additionalProperties hyper::Method::POST if path.matched(paths::ID_FAKE_INLINE_ADDITIONALPROPERTIES) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_param: Option> = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -1160,6 +1221,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter param")), }; + let result = api_impl.test_inline_additional_properties( param_param, &context @@ -1176,12 +1238,12 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { TestInlineAdditionalPropertiesResponse::SuccessfulOperation => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1196,16 +1258,25 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter param: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter param")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // TestJsonFormData - GET /fake/jsonFormData hyper::Method::GET if path.matched(paths::ID_FAKE_JSONFORMDATA) => { + // Handle body parameters (note that non-required body parameters will ignore garbage + // values, rather than causing a 400 response). Produce warning header and logs for + // any unused fields. + let result = body.into_raw().await; + match result { + Ok(body) => { // Form parameters - let param_param = "param_example".to_string(); - let param_param2 = "param2_example".to_string(); + let param_param = + "param_example".to_string(); + let param_param2 = + "param2_example".to_string(); + let result = api_impl.test_json_form_data( param_param, @@ -1223,6 +1294,7 @@ impl hyper::service::Service<(Request, C)> for Service where TestJsonFormDataResponse::SuccessfulOperation => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -1234,6 +1306,12 @@ impl hyper::service::Service<(Request, C)> for Service where } Ok(response) + }, + Err(e) => Ok(Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), + } }, // TestClassname - PATCH /fake_classname_test @@ -1248,15 +1326,15 @@ impl hyper::service::Service<(Request, C)> for Service where }; } - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -1278,6 +1356,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter body")), }; + let result = api_impl.test_classname( param_body, &context @@ -1294,7 +1373,6 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { TestClassnameResponse::SuccessfulOperation @@ -1304,9 +1382,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for TEST_CLASSNAME_SUCCESSFUL_OPERATION")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -1321,8 +1401,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, @@ -1358,13 +1438,13 @@ impl hyper::service::Service<(Request, C)> for Service where } } - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option = if !body.is_empty() { let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body); match serde_ignored::deserialize(deserializer, |path| { @@ -1388,6 +1468,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter body")), }; + let result = api_impl.add_pet( param_body, &context @@ -1404,12 +1485,12 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { AddPetResponse::InvalidInput => { *response.status_mut() = StatusCode::from_u16(405).expect("Unable to turn 405 into a StatusCode"); + }, }, Err(_) => { @@ -1424,8 +1505,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, @@ -1520,6 +1601,7 @@ impl hyper::service::Service<(Request, C)> for Service where DeletePetResponse::InvalidPetValue => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, }, Err(_) => { @@ -1590,13 +1672,16 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/xml") - .expect("Unable to create Content-Type header for FIND_PETS_BY_STATUS_SUCCESSFUL_OPERATION")); - let body_content = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/xml")); + // XML Body + let body = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, FindPetsByStatusResponse::InvalidStatusValue => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, }, Err(_) => { @@ -1667,13 +1752,16 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/xml") - .expect("Unable to create Content-Type header for FIND_PETS_BY_TAGS_SUCCESSFUL_OPERATION")); - let body_content = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/xml")); + // XML Body + let body = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, FindPetsByTagsResponse::InvalidTagValue => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, }, Err(_) => { @@ -1741,17 +1829,21 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/xml") - .expect("Unable to create Content-Type header for GET_PET_BY_ID_SUCCESSFUL_OPERATION")); - let body_content = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/xml")); + // XML Body + let body = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, GetPetByIdResponse::InvalidIDSupplied => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, GetPetByIdResponse::PetNotFound => { *response.status_mut() = StatusCode::from_u16(404).expect("Unable to turn 404 into a StatusCode"); + }, }, Err(_) => { @@ -1797,13 +1889,13 @@ impl hyper::service::Service<(Request, C)> for Service where } } - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option = if !body.is_empty() { let deserializer = &mut serde_xml_rs::de::Deserializer::new_from_reader(&*body); match serde_ignored::deserialize(deserializer, |path| { @@ -1827,6 +1919,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter body")), }; + let result = api_impl.update_pet( param_body, &context @@ -1843,20 +1936,22 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { UpdatePetResponse::InvalidIDSupplied => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, UpdatePetResponse::PetNotFound => { *response.status_mut() = StatusCode::from_u16(404).expect("Unable to turn 404 into a StatusCode"); + }, UpdatePetResponse::ValidationException => { *response.status_mut() = StatusCode::from_u16(405).expect("Unable to turn 405 into a StatusCode"); + }, }, Err(_) => { @@ -1871,8 +1966,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, @@ -1931,9 +2026,18 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for invalid percent decode")) }; + // Handle body parameters (note that non-required body parameters will ignore garbage + // values, rather than causing a 400 response). Produce warning header and logs for + // any unused fields. + let result = body.into_raw().await; + match result { + Ok(body) => { // Form parameters - let param_name = Some("name_example".to_string()); - let param_status = Some("status_example".to_string()); + let param_name = + Some("name_example".to_string()); + let param_status = + Some("status_example".to_string()); + let result = api_impl.update_pet_with_form( param_pet_id, @@ -1952,6 +2056,7 @@ impl hyper::service::Service<(Request, C)> for Service where UpdatePetWithFormResponse::InvalidInput => { *response.status_mut() = StatusCode::from_u16(405).expect("Unable to turn 405 into a StatusCode"); + }, }, Err(_) => { @@ -1963,6 +2068,12 @@ impl hyper::service::Service<(Request, C)> for Service where } Ok(response) + }, + Err(e) => Ok(Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), + } }, // UploadFile - POST /pet/{petId}/uploadImage @@ -1997,14 +2108,6 @@ impl hyper::service::Service<(Request, C)> for Service where } } - let boundary = match swagger::multipart::form::boundary(&headers) { - Some(boundary) => boundary.to_string(), - None => return Ok(Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(Body::from("Couldn't find valid multipart body".to_string())) - .expect("Unable to create Bad Request response for incorrect boundary")), - }; - // Path parameters let path: &str = uri.path(); let path_params = @@ -2028,12 +2131,20 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for invalid percent decode")) }; - // Form Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. - let result = body.into_raw(); - match result.await { - Ok(body) => { + let result = body.into_raw().await; + match result { + Ok(body) => { + let boundary = match swagger::multipart::form::boundary(&headers) { + Some(boundary) => boundary.to_string(), + None => return Ok(Response::builder() + .status(StatusCode::BAD_REQUEST) + .body(Body::from("Couldn't find valid multipart body".to_string())) + .expect("Unable to create Bad Request response for incorrect boundary")), + }; + use std::io::Read; // Read Form Parameters from body @@ -2096,6 +2207,8 @@ impl hyper::service::Service<(Request, C)> for Service where None } }; + + let result = api_impl.upload_file( param_pet_id, param_additional_metadata, @@ -2117,9 +2230,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for UPLOAD_FILE_SUCCESSFUL_OPERATION")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -2134,8 +2249,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from("Couldn't read multipart body".to_string())) - .expect("Unable to create Bad Request response due to unable read multipart body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, @@ -2179,10 +2294,12 @@ impl hyper::service::Service<(Request, C)> for Service where DeleteOrderResponse::InvalidIDSupplied => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, DeleteOrderResponse::OrderNotFound => { *response.status_mut() = StatusCode::from_u16(404).expect("Unable to turn 404 into a StatusCode"); + }, }, Err(_) => { @@ -2226,9 +2343,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for GET_INVENTORY_SUCCESSFUL_OPERATION")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -2286,17 +2405,21 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/xml") - .expect("Unable to create Content-Type header for GET_ORDER_BY_ID_SUCCESSFUL_OPERATION")); - let body_content = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/xml")); + // XML Body + let body = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, GetOrderByIdResponse::InvalidIDSupplied => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, GetOrderByIdResponse::OrderNotFound => { *response.status_mut() = StatusCode::from_u16(404).expect("Unable to turn 404 into a StatusCode"); + }, }, Err(_) => { @@ -2312,15 +2435,15 @@ impl hyper::service::Service<(Request, C)> for Service where // PlaceOrder - POST /store/order hyper::Method::POST if path.matched(paths::ID_STORE_ORDER) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -2342,6 +2465,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter body")), }; + let result = api_impl.place_order( param_body, &context @@ -2358,7 +2482,6 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { PlaceOrderResponse::SuccessfulOperation @@ -2368,13 +2491,16 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/xml") - .expect("Unable to create Content-Type header for PLACE_ORDER_SUCCESSFUL_OPERATION")); - let body_content = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/xml")); + // XML Body + let body = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, PlaceOrderResponse::InvalidOrder => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, }, Err(_) => { @@ -2389,22 +2515,22 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // CreateUser - POST /user hyper::Method::POST if path.matched(paths::ID_USER) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -2426,6 +2552,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter body")), }; + let result = api_impl.create_user( param_body, &context @@ -2442,12 +2569,12 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { CreateUserResponse::SuccessfulOperation => { *response.status_mut() = StatusCode::from_u16(0).expect("Unable to turn 0 into a StatusCode"); + }, }, Err(_) => { @@ -2462,22 +2589,22 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // CreateUsersWithArrayInput - POST /user/createWithArray hyper::Method::POST if path.matched(paths::ID_USER_CREATEWITHARRAY) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option> = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -2499,6 +2626,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter body")), }; + let result = api_impl.create_users_with_array_input( param_body.as_ref(), &context @@ -2515,12 +2643,12 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { CreateUsersWithArrayInputResponse::SuccessfulOperation => { *response.status_mut() = StatusCode::from_u16(0).expect("Unable to turn 0 into a StatusCode"); + }, }, Err(_) => { @@ -2535,22 +2663,22 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // CreateUsersWithListInput - POST /user/createWithList hyper::Method::POST if path.matched(paths::ID_USER_CREATEWITHLIST) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option> = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -2572,6 +2700,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter body")), }; + let result = api_impl.create_users_with_list_input( param_body.as_ref(), &context @@ -2588,12 +2717,12 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { CreateUsersWithListInputResponse::SuccessfulOperation => { *response.status_mut() = StatusCode::from_u16(0).expect("Unable to turn 0 into a StatusCode"); + }, }, Err(_) => { @@ -2608,8 +2737,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, @@ -2653,10 +2782,12 @@ impl hyper::service::Service<(Request, C)> for Service where DeleteUserResponse::InvalidUsernameSupplied => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, DeleteUserResponse::UserNotFound => { *response.status_mut() = StatusCode::from_u16(404).expect("Unable to turn 404 into a StatusCode"); + }, }, Err(_) => { @@ -2714,17 +2845,21 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/xml") - .expect("Unable to create Content-Type header for GET_USER_BY_NAME_SUCCESSFUL_OPERATION")); - let body_content = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/xml")); + // XML Body + let body = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, GetUserByNameResponse::InvalidUsernameSupplied => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, GetUserByNameResponse::UserNotFound => { *response.status_mut() = StatusCode::from_u16(404).expect("Unable to turn 404 into a StatusCode"); + }, }, Err(_) => { @@ -2811,6 +2946,8 @@ impl hyper::service::Service<(Request, C)> for Service where x_expires_after } => { + *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + if let Some(x_rate_limit) = x_rate_limit { let x_rate_limit = match header::IntoHeaderValue(x_rate_limit).try_into() { Ok(val) => val, @@ -2827,6 +2964,7 @@ impl hyper::service::Service<(Request, C)> for Service where x_rate_limit ); } + if let Some(x_expires_after) = x_expires_after { let x_expires_after = match header::IntoHeaderValue(x_expires_after).try_into() { Ok(val) => val, @@ -2843,17 +2981,19 @@ impl hyper::service::Service<(Request, C)> for Service where x_expires_after ); } - *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/xml") - .expect("Unable to create Content-Type header for LOGIN_USER_SUCCESSFUL_OPERATION")); - let body_content = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/xml")); + // XML Body + let body = serde_xml_rs::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, LoginUserResponse::InvalidUsername => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, }, Err(_) => { @@ -2883,6 +3023,7 @@ impl hyper::service::Service<(Request, C)> for Service where LogoutUserResponse::SuccessfulOperation => { *response.status_mut() = StatusCode::from_u16(0).expect("Unable to turn 0 into a StatusCode"); + }, }, Err(_) => { @@ -2921,15 +3062,15 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for invalid percent decode")) }; - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_body: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -2951,6 +3092,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter body")), }; + let result = api_impl.update_user( param_username, param_body, @@ -2968,16 +3110,17 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { UpdateUserResponse::InvalidUserSupplied => { *response.status_mut() = StatusCode::from_u16(400).expect("Unable to turn 400 into a StatusCode"); + }, UpdateUserResponse::UserNotFound => { *response.status_mut() = StatusCode::from_u16(404).expect("Unable to turn 404 into a StatusCode"); + }, }, Err(_) => { @@ -2992,8 +3135,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, diff --git a/samples/server/petstore/rust-server/output/ping-bearer-auth/src/server/mod.rs b/samples/server/petstore/rust-server/output/ping-bearer-auth/src/server/mod.rs index 7807c3de2bed..09cb3242c612 100644 --- a/samples/server/petstore/rust-server/output/ping-bearer-auth/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/ping-bearer-auth/src/server/mod.rs @@ -168,6 +168,7 @@ impl hyper::service::Service<(Request, C)> for Service where PingGetResponse::OK => { *response.status_mut() = StatusCode::from_u16(201).expect("Unable to turn 201 into a StatusCode"); + }, }, Err(_) => { diff --git a/samples/server/petstore/rust-server/output/rust-server-test/src/client/mod.rs b/samples/server/petstore/rust-server/output/rust-server-test/src/client/mod.rs index dcda90cd4a9d..a88ea9dea0ae 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/rust-server-test/src/client/mod.rs @@ -436,11 +436,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(AllOfGetResponse::OK (body) ) @@ -567,14 +569,17 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body + // Body parameter let body = serde_json::to_string(¶m_nested_response).expect("impossible to fail to serialize"); - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -658,11 +663,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(FileResponseGetResponse::Success (body) ) @@ -735,9 +742,12 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; let body = body.to_string(); + + Ok(GetStructuredYamlResponse::OK (body) ) @@ -796,14 +806,17 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body + // Body parameter let body = param_body; - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "text/html"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -819,9 +832,12 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; let body = body.to_string(); + + Ok(HtmlPostResponse::Success (body) ) @@ -880,14 +896,17 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body + // Body parameter let body = param_value; - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/yaml"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { Ok(h) => h, Err(e) => return Err(ApiError(format!("Unable to create header: {} - {}", header, e))) }); + let header = HeaderValue::from_str(Has::::get(context).0.as_str()); request.headers_mut().insert(HeaderName::from_static("x-span-id"), match header { Ok(h) => h, @@ -971,11 +990,13 @@ impl Api for Client where let body = body .into_raw() .map_err(|e| ApiError(format!("Failed to read response: {}", e))).await?; + let body = str::from_utf8(&body) .map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?; - let body = serde_json::from_str::(body).map_err(|e| { - ApiError(format!("Response body did not match the schema: {}", e)) - })?; + let body = serde_json::from_str::(body) + .map_err(|e| ApiError(format!("Response body did not match the schema: {}", e)))?; + + Ok(RawJsonGetResponse::Success (body) ) @@ -1034,9 +1055,10 @@ impl Api for Client where Err(e) => return Err(ApiError(format!("Unable to create request: {}", e))) }; + // Consumes basic body + // Body parameter let body = serde_json::to_string(¶m_value).expect("impossible to fail to serialize"); - - *request.body_mut() = Body::from(body); + *request.body_mut() = Body::from(body); let header = "application/json"; request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) { diff --git a/samples/server/petstore/rust-server/output/rust-server-test/src/server/mod.rs b/samples/server/petstore/rust-server/output/rust-server-test/src/server/mod.rs index 69527ec5cf89..2ff7ad28aeb4 100644 --- a/samples/server/petstore/rust-server/output/rust-server-test/src/server/mod.rs +++ b/samples/server/petstore/rust-server/output/rust-server-test/src/server/mod.rs @@ -184,9 +184,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("*/*") - .expect("Unable to create Content-Type header for ALL_OF_GET_OK")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for */*")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -216,6 +218,7 @@ impl hyper::service::Service<(Request, C)> for Service where DummyGetResponse::Success => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -231,15 +234,15 @@ impl hyper::service::Service<(Request, C)> for Service where // DummyPut - PUT /dummy hyper::Method::PUT if path.matched(paths::ID_DUMMY) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_nested_response: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -261,6 +264,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter nested_response")), }; + let result = api_impl.dummy_put( param_nested_response, &context @@ -277,12 +281,12 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { DummyPutResponse::Success => { *response.status_mut() = StatusCode::from_u16(200).expect("Unable to turn 200 into a StatusCode"); + }, }, Err(_) => { @@ -297,8 +301,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter nested_response: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter nested_response")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, @@ -322,9 +326,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/json") - .expect("Unable to create Content-Type header for FILE_RESPONSE_GET_SUCCESS")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/json")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -358,9 +364,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("application/yaml") - .expect("Unable to create Content-Type header for GET_STRUCTURED_YAML_OK")); - let body_content = body; - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for application/yaml")); + // Plain text Body + let body = body; + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -376,12 +384,12 @@ impl hyper::service::Service<(Request, C)> for Service where // HtmlPost - POST /html hyper::Method::POST if path.matched(paths::ID_HTML) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { + Ok(body) => { let param_body: Option = if !body.is_empty() { match String::from_utf8(body.to_vec()) { Ok(param_body) => Some(param_body), @@ -401,6 +409,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter body")), }; + let result = api_impl.html_post( param_body, &context @@ -420,9 +429,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("text/html") - .expect("Unable to create Content-Type header for HTML_POST_SUCCESS")); - let body_content = body; - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for text/html")); + // Plain text Body + let body = body; + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -437,19 +448,19 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter body: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter body")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, // PostYaml - POST /post-yaml hyper::Method::POST if path.matched(paths::ID_POST_YAML) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { + Ok(body) => { let param_value: Option = if !body.is_empty() { match String::from_utf8(body.to_vec()) { Ok(param_value) => Some(param_value), @@ -469,6 +480,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter value")), }; + let result = api_impl.post_yaml( param_value, &context @@ -484,6 +496,7 @@ impl hyper::service::Service<(Request, C)> for Service where PostYamlResponse::OK => { *response.status_mut() = StatusCode::from_u16(204).expect("Unable to turn 204 into a StatusCode"); + }, }, Err(_) => { @@ -498,8 +511,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter value: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter value")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } }, @@ -523,9 +536,11 @@ impl hyper::service::Service<(Request, C)> for Service where response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_str("*/*") - .expect("Unable to create Content-Type header for RAW_JSON_GET_SUCCESS")); - let body_content = serde_json::to_string(&body).expect("impossible to fail to serialize"); - *response.body_mut() = Body::from(body_content); + .expect("Unable to create Content-Type header for */*")); + // JSON Body + let body = serde_json::to_string(&body).expect("impossible to fail to serialize"); + *response.body_mut() = Body::from(body); + }, }, Err(_) => { @@ -541,15 +556,15 @@ impl hyper::service::Service<(Request, C)> for Service where // SoloObjectPost - POST /solo-object hyper::Method::POST if path.matched(paths::ID_SOLO_OBJECT) => { - // Body parameters (note that non-required body parameters will ignore garbage + // Handle body parameters (note that non-required body parameters will ignore garbage // values, rather than causing a 400 response). Produce warning header and logs for // any unused fields. let result = body.into_raw().await; match result { - Ok(body) => { - let mut unused_elements = Vec::new(); + Ok(body) => { + let mut unused_elements : Vec = vec![]; let param_value: Option = if !body.is_empty() { - let deserializer = &mut serde_json::Deserializer::from_slice(&body); + let deserializer = &mut serde_json::Deserializer::from_slice(&*body); match serde_ignored::deserialize(deserializer, |path| { warn!("Ignoring unknown field in body: {}", path); unused_elements.push(path.to_string()); @@ -571,6 +586,7 @@ impl hyper::service::Service<(Request, C)> for Service where .expect("Unable to create Bad Request response for missing body parameter value")), }; + let result = api_impl.solo_object_post( param_value, &context @@ -587,12 +603,12 @@ impl hyper::service::Service<(Request, C)> for Service where HeaderValue::from_str(format!("Ignoring unknown fields in body: {:?}", unused_elements).as_str()) .expect("Unable to create Warning header value")); } - match result { Ok(rsp) => match rsp { SoloObjectPostResponse::OK => { *response.status_mut() = StatusCode::from_u16(204).expect("Unable to turn 204 into a StatusCode"); + }, }, Err(_) => { @@ -607,8 +623,8 @@ impl hyper::service::Service<(Request, C)> for Service where }, Err(e) => Ok(Response::builder() .status(StatusCode::BAD_REQUEST) - .body(Body::from(format!("Couldn't read body parameter value: {}", e))) - .expect("Unable to create Bad Request response due to unable to read body parameter value")), + .body(Body::from(format!("Unable to read body: {}", e))) + .expect("Unable to create Bad Request response due to unable to read body")), } },