-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rust Server] Refactor Mustache templates for request/response body h…
…andling (#19347) * [Rust Server] Refactor Mustache templates for request/response body handling * Update samples
- Loading branch information
1 parent
8f7354a
commit daf5222
Showing
27 changed files
with
1,644 additions
and
1,143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...es/openapi-generator/src/main/resources/rust-server/client-request-body-instance.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}} |
Oops, something went wrong.