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

feat(rust): implement from trait on enum variants #17866

Closed
Closed
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
17 changes: 16 additions & 1 deletion modules/openapi-generator/src/main/resources/rust/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ impl Default for {{classname}} {
}{{/-first}}{{/mappedModels}}{{/oneOf}}{{#oneOf}}{{#-first}}Self::{{{.}}}(Box::default()){{/-first}}{{/oneOf}}
}
}
{{#oneOf}}

impl From<{{{.}}}> for {{classname}} {
fn from(model: {{{.}}}) -> Self {
Self::{{{.}}}(Box::new(model))
}
}
{{/oneOf}}
Comment on lines +80 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that {{#oneOf}} {{{.}}} {{/oneOf}} isn't enough here.
Even against current code - there is a case with mappedModels, but actual issue is that current implementation in master have several issues, including wrong imports.

You can take a look at #17915
I think this feature should be introduced only after the fixes


{{/discriminator}}

Expand Down Expand Up @@ -109,7 +117,6 @@ impl {{{classname}}} {
}
{{/oneOf.isEmpty}}
{{^oneOf.isEmpty}}
{{! TODO: add other vars that are not part of the oneOf}}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum {{classname}} {
Expand All @@ -126,6 +133,14 @@ impl Default for {{classname}} {
{{#oneOf}}{{#-first}}Self::{{{.}}}(Box::default()){{/-first}}{{/oneOf}}
}
}
{{#oneOf}}

impl From<{{{.}}}> for {{classname}} {
fn from(model: {{{.}}}) -> Self {
Self::{{{.}}}(Box::new(model))
}
}
{{/oneOf}}
{{/oneOf.isEmpty}}
{{/discriminator}}
{{/isEnum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ impl Default for CreateStateRequest {
}
}

impl From<ObjA> for CreateStateRequest {
fn from(model: ObjA) -> Self {
Self::ObjA(Box::new(model))
}
}

impl From<ObjB> for CreateStateRequest {
fn from(model: ObjB) -> Self {
Self::ObjB(Box::new(model))
}
}




Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ impl Default for CustomOneOfArraySchemaInner {
}
}

impl From<ObjA> for CustomOneOfArraySchemaInner {
fn from(model: ObjA) -> Self {
Self::ObjA(Box::new(model))
}
}

impl From<ObjB> for CustomOneOfArraySchemaInner {
fn from(model: ObjB) -> Self {
Self::ObjB(Box::new(model))
}
}

impl From<ObjC> for CustomOneOfArraySchemaInner {
fn from(model: ObjC) -> Self {
Self::ObjC(Box::new(model))
}
}




Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ impl Default for CustomOneOfSchema {
}
}

impl From<ObjA> for CustomOneOfSchema {
fn from(model: ObjA) -> Self {
Self::ObjA(Box::new(model))
}
}

impl From<ObjB> for CustomOneOfSchema {
fn from(model: ObjB) -> Self {
Self::ObjB(Box::new(model))
}
}




Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ impl Default for GetState200Response {
}
}

impl From<ObjA> for GetState200Response {
fn from(model: ObjA) -> Self {
Self::ObjA(Box::new(model))
}
}

impl From<ObjB> for GetState200Response {
fn from(model: ObjB) -> Self {
Self::ObjB(Box::new(model))
}
}

impl From<ObjD> for GetState200Response {
fn from(model: ObjD) -> Self {
Self::ObjD(Box::new(model))
}
}




Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ impl Default for BarRefOrValue {
}
}

impl From<Bar> for BarRefOrValue {
fn from(model: Bar) -> Self {
Self::Bar(Box::new(model))
}
}

impl From<BarRef> for BarRefOrValue {
fn from(model: BarRef) -> Self {
Self::BarRef(Box::new(model))
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ impl Default for FooRefOrValue {
}
}

impl From<Foo> for FooRefOrValue {
fn from(model: Foo) -> Self {
Self::Foo(Box::new(model))
}
}

impl From<FooRef> for FooRefOrValue {
fn from(model: FooRef) -> Self {
Self::FooRef(Box::new(model))
}
}




12 changes: 12 additions & 0 deletions samples/client/others/rust/hyper/oneOf/src/models/fruit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ impl Default for Fruit {
}
}

impl From<Apple> for Fruit {
fn from(model: Apple) -> Self {
Self::Apple(Box::new(model))
}
}

impl From<Banana> for Fruit {
fn from(model: Banana) -> Self {
Self::Banana(Box::new(model))
}
}




Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ impl Default for CreateStateRequest {
}
}

impl From<ObjA> for CreateStateRequest {
fn from(model: ObjA) -> Self {
Self::ObjA(Box::new(model))
}
}

impl From<ObjB> for CreateStateRequest {
fn from(model: ObjB) -> Self {
Self::ObjB(Box::new(model))
}
}




Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ impl Default for CustomOneOfArraySchemaInner {
}
}

impl From<ObjA> for CustomOneOfArraySchemaInner {
fn from(model: ObjA) -> Self {
Self::ObjA(Box::new(model))
}
}

impl From<ObjB> for CustomOneOfArraySchemaInner {
fn from(model: ObjB) -> Self {
Self::ObjB(Box::new(model))
}
}

impl From<ObjC> for CustomOneOfArraySchemaInner {
fn from(model: ObjC) -> Self {
Self::ObjC(Box::new(model))
}
}




Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ impl Default for CustomOneOfSchema {
}
}

impl From<ObjA> for CustomOneOfSchema {
fn from(model: ObjA) -> Self {
Self::ObjA(Box::new(model))
}
}

impl From<ObjB> for CustomOneOfSchema {
fn from(model: ObjB) -> Self {
Self::ObjB(Box::new(model))
}
}




Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ impl Default for GetState200Response {
}
}

impl From<ObjA> for GetState200Response {
fn from(model: ObjA) -> Self {
Self::ObjA(Box::new(model))
}
}

impl From<ObjB> for GetState200Response {
fn from(model: ObjB) -> Self {
Self::ObjB(Box::new(model))
}
}

impl From<ObjD> for GetState200Response {
fn from(model: ObjD) -> Self {
Self::ObjD(Box::new(model))
}
}




Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ impl Default for BarRefOrValue {
}
}

impl From<Bar> for BarRefOrValue {
fn from(model: Bar) -> Self {
Self::Bar(Box::new(model))
}
}

impl From<BarRef> for BarRefOrValue {
fn from(model: BarRef) -> Self {
Self::BarRef(Box::new(model))
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ impl Default for FooRefOrValue {
}
}

impl From<Foo> for FooRefOrValue {
fn from(model: Foo) -> Self {
Self::Foo(Box::new(model))
}
}

impl From<FooRef> for FooRefOrValue {
fn from(model: FooRef) -> Self {
Self::FooRef(Box::new(model))
}
}




12 changes: 12 additions & 0 deletions samples/client/others/rust/reqwest/oneOf/src/models/fruit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ impl Default for Fruit {
}
}

impl From<Apple> for Fruit {
fn from(model: Apple) -> Self {
Self::Apple(Box::new(model))
}
}

impl From<Banana> for Fruit {
fn from(model: Banana) -> Self {
Self::Banana(Box::new(model))
}
}




Loading