Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement] [Rust-Axum] Fix clippy warning #19920

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/configs/manual/rust-axum-header-uuid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ generateAliasAsModel: true
additionalProperties:
hideGenerationTimestamp: "true"
packageName: rust-axum-header-uui
globalProperties:
skipFormModel: "false"
enablePostProcessFile: true
2 changes: 2 additions & 0 deletions bin/configs/manual/rust-axum-multipart-v3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ additionalProperties:
hideGenerationTimestamp: "true"
allowBlockingResponseSerialize: "true"
packageName: multipart-v3
globalProperties:
skipFormModel: false
enablePostProcessFile: true
2 changes: 2 additions & 0 deletions bin/configs/manual/rust-axum-openapi-v3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ additionalProperties:
hideGenerationTimestamp: "true"
allowBlockingValidator: "true"
packageName: openapi-v3
globalProperties:
skipFormModel: false
enablePostProcessFile: true
2 changes: 2 additions & 0 deletions bin/configs/manual/rust-axum-ops-v3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ generateAliasAsModel: true
additionalProperties:
hideGenerationTimestamp: "true"
packageName: ops-v3
globalProperties:
skipFormModel: false
enablePostProcessFile: true
2 changes: 2 additions & 0 deletions bin/configs/manual/rust-axum-ping-bearer-auth-v3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ generateAliasAsModel: true
additionalProperties:
hideGenerationTimestamp: "true"
packageName: ping-bearer-auth
globalProperties:
skipFormModel: false
enablePostProcessFile: true
2 changes: 2 additions & 0 deletions bin/configs/manual/rust-axum-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ generateAliasAsModel: true
additionalProperties:
hideGenerationTimestamp: "true"
packageName: rust-server-test
globalProperties:
skipFormModel: false
enablePostProcessFile: true
2 changes: 2 additions & 0 deletions bin/configs/manual/rust-axum-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ additionalProperties:
hideGenerationTimestamp: "true"
packageName: rust-axum-validation-test
disableValidator: "true"
globalProperties:
skipFormModel: false
enablePostProcessFile: true
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@ impl std::str::FromStr for {{{classname}}} {
{{/arrayModelType}}
{{^arrayModelType}}
{{! general struct}}

{{#anyOf.size}}
/// Any of:
{{#anyOf}}
Expand Down Expand Up @@ -575,7 +574,6 @@ impl PartialEq for {{{classname}}} {
}
}
{{/anyOf.size}}

{{#oneOf.size}}
/// One of:
{{#oneOf}}
Expand Down Expand Up @@ -608,7 +606,6 @@ impl PartialEq for {{{classname}}} {
}
}
{{/oneOf.size}}

{{^anyOf.size}}
{{^oneOf.size}}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, validator::Validate)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ impl<T> Nullable<T> {
}
}

impl<'a, T: Clone> Nullable<&'a T> {
impl<T: Clone> Nullable<&T> {
/// Maps an `Nullable<&T>` to an `Nullable<T>` by cloning the contents of the
/// Nullable.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.9.0-SNAPSHOT
7.10.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ server, you can easily generate a server stub.
To see how to make this your own, look here: [README]((https://openapi-generator.tech))

- API version: 1.0.7
- Generator version: 7.9.0-SNAPSHOT
- Generator version: 7.10.0-SNAPSHOT



Expand Down
189 changes: 189 additions & 0 deletions samples/server/petstore/rust-axum/output/multipart-v3/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,195 @@ impl std::convert::TryFrom<HeaderValue> for header::IntoHeaderValue<MultipartRel
}
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, validator::Validate)]
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
pub struct MultipartRequest {
#[serde(rename = "string_field")]
pub string_field: String,

#[serde(rename = "optional_string_field")]
#[serde(skip_serializing_if = "Option::is_none")]
pub optional_string_field: Option<String>,

#[serde(rename = "object_field")]
#[serde(skip_serializing_if = "Option::is_none")]
pub object_field: Option<models::MultipartRequestObjectField>,

#[serde(rename = "binary_field")]
pub binary_field: ByteArray,
}

impl MultipartRequest {
#[allow(clippy::new_without_default, clippy::too_many_arguments)]
pub fn new(string_field: String, binary_field: ByteArray) -> MultipartRequest {
MultipartRequest {
string_field,
optional_string_field: None,
object_field: None,
binary_field,
}
}
}

/// Converts the MultipartRequest value to the Query Parameters representation (style=form, explode=false)
/// specified in https://swagger.io/docs/specification/serialization/
/// Should be implemented in a serde serializer
impl std::fmt::Display for MultipartRequest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let params: Vec<Option<String>> = vec![
Some("string_field".to_string()),
Some(self.string_field.to_string()),
self.optional_string_field
.as_ref()
.map(|optional_string_field| {
[
"optional_string_field".to_string(),
optional_string_field.to_string(),
]
.join(",")
}),
// Skipping object_field in query parameter serialization

// Skipping binary_field in query parameter serialization
// Skipping binary_field in query parameter serialization
];

write!(
f,
"{}",
params.into_iter().flatten().collect::<Vec<_>>().join(",")
)
}
}

/// Converts Query Parameters representation (style=form, explode=false) to a MultipartRequest value
/// as specified in https://swagger.io/docs/specification/serialization/
/// Should be implemented in a serde deserializer
impl std::str::FromStr for MultipartRequest {
type Err = String;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
/// An intermediate representation of the struct to use for parsing.
#[derive(Default)]
#[allow(dead_code)]
struct IntermediateRep {
pub string_field: Vec<String>,
pub optional_string_field: Vec<String>,
pub object_field: Vec<models::MultipartRequestObjectField>,
pub binary_field: Vec<ByteArray>,
}

let mut intermediate_rep = IntermediateRep::default();

// Parse into intermediate representation
let mut string_iter = s.split(',');
let mut key_result = string_iter.next();

while key_result.is_some() {
let val = match string_iter.next() {
Some(x) => x,
None => {
return std::result::Result::Err(
"Missing value while parsing MultipartRequest".to_string(),
)
}
};

if let Some(key) = key_result {
#[allow(clippy::match_single_binding)]
match key {
#[allow(clippy::redundant_clone)]
"string_field" => intermediate_rep.string_field.push(
<String as std::str::FromStr>::from_str(val).map_err(|x| x.to_string())?,
),
#[allow(clippy::redundant_clone)]
"optional_string_field" => intermediate_rep.optional_string_field.push(
<String as std::str::FromStr>::from_str(val).map_err(|x| x.to_string())?,
),
#[allow(clippy::redundant_clone)]
"object_field" => intermediate_rep.object_field.push(
<models::MultipartRequestObjectField as std::str::FromStr>::from_str(val)
.map_err(|x| x.to_string())?,
),
"binary_field" => return std::result::Result::Err(
"Parsing binary data in this style is not supported in MultipartRequest"
.to_string(),
),
_ => {
return std::result::Result::Err(
"Unexpected key while parsing MultipartRequest".to_string(),
)
}
}
}

// Get the next key
key_result = string_iter.next();
}

// Use the intermediate representation to return the struct
std::result::Result::Ok(MultipartRequest {
string_field: intermediate_rep
.string_field
.into_iter()
.next()
.ok_or_else(|| "string_field missing in MultipartRequest".to_string())?,
optional_string_field: intermediate_rep.optional_string_field.into_iter().next(),
object_field: intermediate_rep.object_field.into_iter().next(),
binary_field: intermediate_rep
.binary_field
.into_iter()
.next()
.ok_or_else(|| "binary_field missing in MultipartRequest".to_string())?,
})
}
}

// Methods for converting between header::IntoHeaderValue<MultipartRequest> and HeaderValue

#[cfg(feature = "server")]
impl std::convert::TryFrom<header::IntoHeaderValue<MultipartRequest>> for HeaderValue {
type Error = String;

fn try_from(
hdr_value: header::IntoHeaderValue<MultipartRequest>,
) -> std::result::Result<Self, Self::Error> {
let hdr_value = hdr_value.to_string();
match HeaderValue::from_str(&hdr_value) {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(e) => std::result::Result::Err(format!(
"Invalid header value for MultipartRequest - value: {} is invalid {}",
hdr_value, e
)),
}
}
}

#[cfg(feature = "server")]
impl std::convert::TryFrom<HeaderValue> for header::IntoHeaderValue<MultipartRequest> {
type Error = String;

fn try_from(hdr_value: HeaderValue) -> std::result::Result<Self, Self::Error> {
match hdr_value.to_str() {
std::result::Result::Ok(value) => {
match <MultipartRequest as std::str::FromStr>::from_str(value) {
std::result::Result::Ok(value) => {
std::result::Result::Ok(header::IntoHeaderValue(value))
}
std::result::Result::Err(err) => std::result::Result::Err(format!(
"Unable to convert header value '{}' into MultipartRequest - {}",
value, err
)),
}
}
std::result::Result::Err(e) => std::result::Result::Err(format!(
"Unable to convert header: {:?} to string: {}",
hdr_value, e
)),
}
}
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, validator::Validate)]
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
pub struct MultipartRequestObjectField {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ impl<T> Nullable<T> {
}
}

impl<'a, T: Clone> Nullable<&'a T> {
impl<T: Clone> Nullable<&T> {
/// Maps an `Nullable<&T>` to an `Nullable<T>` by cloning the contents of the
/// Nullable.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.9.0-SNAPSHOT
7.10.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ server, you can easily generate a server stub.
To see how to make this your own, look here: [README]((https://openapi-generator.tech))

- API version: 1.0.7
- Generator version: 7.9.0-SNAPSHOT
- Generator version: 7.10.0-SNAPSHOT



Expand Down
Loading
Loading