Skip to content
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: 1 addition & 1 deletion bin/utils/test_file_list.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@
- filename: "samples/server/petstore/rust-axum/output/rust-axum-oneof/tests/oneof_with_discriminator.rs"
sha256: 2d4f5a069fdcb3057bb078d5e75b3de63cd477b97725e457079df24bd2c30600
- filename: "samples/server/petstore/rust-axum/output/openapi-v3/tests/oneof_untagged.rs"
sha256: e72fbf81a9849dc7abb7e2169f2fc355c8b1cf991c0e2ffc083126abd9e966e7
sha256: 1d3fb01f65e98290b1d3eece28014c7d3e3f2fdf18e7110249d3c591cc4642ab
5 changes: 3 additions & 2 deletions docs/generators/rust-axum.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>dyn</li>
<li>else</li>
<li>enum</li>
<li>errors</li>
<li>extern</li>
<li>false</li>
<li>final</li>
Expand Down Expand Up @@ -207,8 +208,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|Composite|✓|OAS2,OAS3
|Polymorphism|✗|OAS2,OAS3
|Union|✗|OAS3
|allOf||OAS2,OAS3
|anyOf||OAS3
|allOf||OAS2,OAS3
|anyOf||OAS3
|oneOf|✓|OAS3
|not|✗|OAS3

Expand Down

Large diffs are not rendered by default.

119 changes: 81 additions & 38 deletions modules/openapi-generator/src/main/resources/rust-axum/models.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ pub fn check_xss_map<T>(v: &std::collections::HashMap<String, T>) -> std::result
/// Enumeration of values.
/// Since this enum's variants do not hold data, we can easily define them as `#[repr(C)]`
/// which helps with FFI.
#[allow(non_camel_case_types)]
#[allow(non_camel_case_types, clippy::large_enum_variant)]
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "conversion", derive(frunk_enum_derive::LabelledGenericEnum))]
Expand Down Expand Up @@ -751,17 +751,38 @@ impl std::str::FromStr for {{{classname}}} {
{{^arrayModelType}}
{{! general struct}}
{{#anyOf.size}}
/// Any of:
{{#anyOf}}
/// - {{{.}}}
{{/anyOf}}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct {{{classname}}}(Box<serde_json::value::RawValue>);
{{#discriminator}}
#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
#[serde(tag = "{{{propertyBaseName}}}")]
{{/discriminator}}
{{^discriminator}}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
{{/discriminator}}
#[allow(non_camel_case_types, clippy::large_enum_variant)]
pub enum {{{classname}}} {
{{#composedSchemas}}
{{#anyOf}}
{{{datatypeWithEnum}}}({{{dataType}}}),
{{/anyOf}}
{{/composedSchemas}}
}

impl validator::Validate for {{{classname}}}
{
fn validate(&self) -> std::result::Result<(), validator::ValidationErrors> {
std::result::Result::Ok(())
match self {
{{#composedSchemas}}
{{#anyOf}}
{{^isModel}}
Self::{{{datatypeWithEnum}}}(_) => std::result::Result::Ok(()),
{{/isModel}}
{{#isModel}}
Self::{{{datatypeWithEnum}}}(v) => v.validate(),
{{/isModel}}
{{/anyOf}}
{{/composedSchemas}}
}
}
}

Expand All @@ -776,11 +797,32 @@ impl std::str::FromStr for {{{classname}}} {
}
}

impl PartialEq for {{{classname}}} {
fn eq(&self, other: &Self) -> bool {
self.0.get() == other.0.get()
{{#discriminator}}
impl serde::Serialize for {{{classname}}} {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::Serializer {
match self {
{{#composedSchemas}}
{{#anyOf}}
Self::{{{datatypeWithEnum}}}(x) => x.serialize(serializer),
{{/anyOf}}
{{/composedSchemas}}
}
}
}
{{/discriminator}}

{{#composedSchemas}}
{{#anyOf}}
{{#vendorExtensions.x-from-trait}}
impl From<{{{dataType}}}> for {{{classname}}} {
fn from(value: {{{dataType}}}) -> Self {
Self::{{{datatypeWithEnum}}}(value)
}
}
{{/vendorExtensions.x-from-trait}}
{{/anyOf}}
{{/composedSchemas}}

{{/anyOf.size}}
{{#oneOf.size}}
Expand All @@ -792,11 +834,11 @@ impl PartialEq for {{{classname}}} {
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
{{/discriminator}}
#[allow(non_camel_case_types)]
#[allow(non_camel_case_types, clippy::large_enum_variant)]
pub enum {{{classname}}} {
{{#composedSchemas}}
{{#oneOf}}
{{{datatypeWithEnum}}}(Box<{{{dataType}}}>),
{{{datatypeWithEnum}}}({{{dataType}}}),
{{/oneOf}}
{{/composedSchemas}}
}
Expand All @@ -807,18 +849,29 @@ impl validator::Validate for {{{classname}}}
match self {
{{#composedSchemas}}
{{#oneOf}}
{{#isPrimitiveType}}
{{^isModel}}
Self::{{{datatypeWithEnum}}}(_) => std::result::Result::Ok(()),
{{/isPrimitiveType}}
{{^isPrimitiveType}}
Self::{{{datatypeWithEnum}}}(x) => x.validate(),
{{/isPrimitiveType}}
{{/isModel}}
{{#isModel}}
Self::{{{datatypeWithEnum}}}(v) => v.validate(),
{{/isModel}}
{{/oneOf}}
{{/composedSchemas}}
}
}
}

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

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
serde_json::from_str(s)
}
}

{{#discriminator}}
impl serde::Serialize for {{{classname}}} {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
Expand All @@ -834,29 +887,18 @@ impl serde::Serialize for {{{classname}}} {
}
{{/discriminator}}



{{#composedSchemas}}
{{#oneOf}}
{{#vendorExtensions.x-from-trait}}
impl From<{{{dataType}}}> for {{{classname}}} {
fn from(value: {{{dataType}}}) -> Self {
Self::{{{datatypeWithEnum}}}(Box::new(value))
Self::{{{datatypeWithEnum}}}(value)
}
}
{{/vendorExtensions.x-from-trait}}
{{/oneOf}}
{{/composedSchemas}}

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

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
serde_json::from_str(s)
}
}

{{/oneOf.size}}
{{^anyOf.size}}
{{^oneOf.size}}
Expand All @@ -871,8 +913,10 @@ pub struct {{{classname}}} {
/// Note: inline enums are not fully supported by openapi-generator
{{/isEnum}}
{{#isDiscriminator}}
{{#isString}}
#[serde(default = "{{{classname}}}::_name_for_{{{name}}}")]
#[serde(serialize_with = "{{{classname}}}::_serialize_{{{name}}}")]
{{/isString}}
{{/isDiscriminator}}
#[serde(rename = "{{{baseName}}}")]
{{#hasValidation}}
Expand Down Expand Up @@ -989,9 +1033,9 @@ pub struct {{{classname}}} {
{{/vars}}
}


{{#vars}}
{{#isDiscriminator}}
{{#isString}}
impl {{{classname}}} {
fn _name_for_{{{name}}}() -> String {
String::from("{{{classname}}}")
Expand All @@ -1004,10 +1048,10 @@ impl {{{classname}}} {
s.serialize_str(&Self::_name_for_{{{name}}}())
}
}
{{/isString}}
{{/isDiscriminator}}
{{/vars}}


{{#vars}}
{{#hasValidation}}
{{#pattern}}
Expand Down Expand Up @@ -1035,9 +1079,9 @@ fn validate_byte_{{#lambda.lowercase}}{{{classname}}}_{{{name}}}{{/lambda.lowerc

impl {{{classname}}} {
#[allow(clippy::new_without_default, clippy::too_many_arguments)]
pub fn new({{#vars}}{{^defaultValue}}{{{name}}}: {{#isNullable}}Nullable<{{/isNullable}}{{{dataType}}}{{#isNullable}}>{{/isNullable}}, {{/defaultValue}}{{/vars}}) -> {{{classname}}} {
pub fn new({{#vars}}{{^isDiscriminator}}{{^defaultValue}}{{{name}}}: {{#isNullable}}Nullable<{{/isNullable}}{{{dataType}}}{{#isNullable}}>{{/isNullable}}, {{/defaultValue}}{{/isDiscriminator}}{{#isDiscriminator}}{{^isString}}{{^defaultValue}}{{{name}}}: {{#isNullable}}Nullable<{{/isNullable}}{{{dataType}}}{{#isNullable}}>{{/isNullable}}, {{/defaultValue}}{{/isString}}{{/isDiscriminator}}{{/vars}}) -> {{{classname}}} {
{{{classname}}} {
{{#vars}} {{#defaultValue}}{{{name}}}: {{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}{{{name}}}{{/defaultValue}},
{{#vars}} {{^isDiscriminator}}{{#defaultValue}}{{{name}}}: {{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}{{{name}}}{{/defaultValue}}{{/isDiscriminator}}{{#isDiscriminator}}{{#isString}}{{{name}}}: Self::_name_for_{{{name}}}(){{/isString}}{{^isString}}{{#defaultValue}}{{{name}}}: {{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}{{{name}}}{{/defaultValue}}{{/isString}}{{/isDiscriminator}},
{{/vars}}
}
}
Expand Down Expand Up @@ -1075,7 +1119,7 @@ impl std::fmt::Display for {{{classname}}} {
{{/isArray}}
{{#isArray}}
{{#isNullable}}
Some(self.{{{name}}}.as_ref().map_or(vec!["null".to_string()], |x| x.iter().map(|x| x.to_string()).collect::<Vec<_>>().join(","))),
Some(self.{{{name}}}.as_ref().map_or("null".to_string(), |x| x.iter().map(|x| x.to_string()).collect::<Vec<_>>().join(","))),
{{/isNullable}}
{{^isNullable}}
Some(self.{{{name}}}.iter().map(|x| x.to_string()).collect::<Vec<_>>().join(",")),
Expand Down Expand Up @@ -1226,7 +1270,6 @@ impl std::convert::TryFrom<HeaderValue> for header::IntoHeaderValue<{{{classname
}
}
}

{{/oneOf.size}}
{{/anyOf.size}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,15 +594,15 @@ pub struct PaymentMethod {
#[serde(rename = "type")]
#[validate(custom(function = "check_xss_string"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub r#type: Option<String>,
pub r_type: Option<String>,
}

impl PaymentMethod {
#[allow(clippy::new_without_default, clippy::too_many_arguments)]
pub fn new() -> PaymentMethod {
PaymentMethod {
name: None,
r#type: None,
r_type: None,
}
}
}
Expand All @@ -616,9 +616,9 @@ impl std::fmt::Display for PaymentMethod {
self.name
.as_ref()
.map(|name| ["name".to_string(), name.to_string()].join(",")),
self.r#type
self.r_type
.as_ref()
.map(|r#type| ["type".to_string(), r#type.to_string()].join(",")),
.map(|r_type| ["type".to_string(), r_type.to_string()].join(",")),
];

write!(
Expand All @@ -641,7 +641,7 @@ impl std::str::FromStr for PaymentMethod {
#[allow(dead_code)]
struct IntermediateRep {
pub name: Vec<String>,
pub r#type: Vec<String>,
pub r_type: Vec<String>,
}

let mut intermediate_rep = IntermediateRep::default();
Expand All @@ -668,7 +668,7 @@ impl std::str::FromStr for PaymentMethod {
<String as std::str::FromStr>::from_str(val).map_err(|x| x.to_string())?,
),
#[allow(clippy::redundant_clone)]
"type" => intermediate_rep.r#type.push(
"type" => intermediate_rep.r_type.push(
<String as std::str::FromStr>::from_str(val).map_err(|x| x.to_string())?,
),
_ => {
Expand All @@ -686,7 +686,7 @@ impl std::str::FromStr for PaymentMethod {
// Use the intermediate representation to return the struct
std::result::Result::Ok(PaymentMethod {
name: intermediate_rep.name.into_iter().next(),
r#type: intermediate_rep.r#type.into_iter().next(),
r_type: intermediate_rep.r_type.into_iter().next(),
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,15 +594,15 @@ pub struct PaymentMethod {
#[serde(rename = "type")]
#[validate(custom(function = "check_xss_string"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub r#type: Option<String>,
pub r_type: Option<String>,
}

impl PaymentMethod {
#[allow(clippy::new_without_default, clippy::too_many_arguments)]
pub fn new() -> PaymentMethod {
PaymentMethod {
name: None,
r#type: None,
r_type: None,
}
}
}
Expand All @@ -616,9 +616,9 @@ impl std::fmt::Display for PaymentMethod {
self.name
.as_ref()
.map(|name| ["name".to_string(), name.to_string()].join(",")),
self.r#type
self.r_type
.as_ref()
.map(|r#type| ["type".to_string(), r#type.to_string()].join(",")),
.map(|r_type| ["type".to_string(), r_type.to_string()].join(",")),
];

write!(
Expand All @@ -641,7 +641,7 @@ impl std::str::FromStr for PaymentMethod {
#[allow(dead_code)]
struct IntermediateRep {
pub name: Vec<String>,
pub r#type: Vec<String>,
pub r_type: Vec<String>,
}

let mut intermediate_rep = IntermediateRep::default();
Expand All @@ -668,7 +668,7 @@ impl std::str::FromStr for PaymentMethod {
<String as std::str::FromStr>::from_str(val).map_err(|x| x.to_string())?,
),
#[allow(clippy::redundant_clone)]
"type" => intermediate_rep.r#type.push(
"type" => intermediate_rep.r_type.push(
<String as std::str::FromStr>::from_str(val).map_err(|x| x.to_string())?,
),
_ => {
Expand All @@ -686,7 +686,7 @@ impl std::str::FromStr for PaymentMethod {
// Use the intermediate representation to return the struct
std::result::Result::Ok(PaymentMethod {
name: intermediate_rep.name.into_iter().next(),
r#type: intermediate_rep.r#type.into_iter().next(),
r_type: intermediate_rep.r_type.into_iter().next(),
})
}
}
Expand Down
Loading
Loading