Skip to content

Commit 481c690

Browse files
authored
[Rust] Fix enum variant name generation (#20689)
* Regression test for enum Name generation in oneOf where variants would end up having the same name. * Fix enum variant name generation Datatypes like `Vec<foo::bar::Baz>` would end up as `Baz` instead of `VecOfBaz`
1 parent 57b1c91 commit 481c690

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustAxumServerCodegen.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,9 @@ private void postProcessOneOfModels(List<ModelMap> allModels) {
615615
for (CodegenProperty model : csOneOf) {
616616
// Generate a valid name for the enum variant.
617617
// Mainly needed for primitive types.
618-
String[] modelParts = model.dataType.replace("<", "Of").replace(">", "").split("::");
619-
model.datatypeWithEnum = camelize(modelParts[modelParts.length - 1]);
618+
619+
model.datatypeWithEnum = camelize(model.dataType.replaceAll("(?:\\w+::)+(\\w+)", "$1")
620+
.replace("<", "Of").replace(">", ""));
620621

621622
// Primitive type is not properly set, this overrides it to guarantee adequate model generation.
622623
if (!model.getDataType().matches(String.format(Locale.ROOT, ".*::%s", model.getDatatypeWithEnum()))) {

modules/openapi-generator/src/test/resources/3_0/rust-axum/rust-axum-oneof.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ components:
3030
- "$ref": "#/components/schemas/Hello"
3131
- "$ref": "#/components/schemas/Greeting"
3232
- "$ref": "#/components/schemas/Goodbye"
33+
- "$ref": "#/components/schemas/SomethingCompletelyDifferent"
3334
title: Message
3435
Hello:
3536
type: object
@@ -83,4 +84,10 @@ components:
8384
- goodbye_message
8485
required:
8586
- op
86-
- d
87+
- d
88+
SomethingCompletelyDifferent:
89+
oneOf:
90+
- items:
91+
type: object
92+
type: array
93+
- type: object

samples/server/petstore/rust-axum/output/rust-axum-oneof/src/models.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,7 @@ pub enum Message {
932932
Hello(Box<models::Hello>),
933933
Greeting(Box<models::Greeting>),
934934
Goodbye(Box<models::Goodbye>),
935+
SomethingCompletelyDifferent(Box<models::SomethingCompletelyDifferent>),
935936
}
936937

937938
impl validator::Validate for Message {
@@ -940,6 +941,7 @@ impl validator::Validate for Message {
940941
Self::Hello(x) => x.validate(),
941942
Self::Greeting(x) => x.validate(),
942943
Self::Goodbye(x) => x.validate(),
944+
Self::SomethingCompletelyDifferent(x) => x.validate(),
943945
}
944946
}
945947
}
@@ -953,6 +955,7 @@ impl serde::Serialize for Message {
953955
Self::Hello(x) => x.serialize(serializer),
954956
Self::Greeting(x) => x.serialize(serializer),
955957
Self::Goodbye(x) => x.serialize(serializer),
958+
Self::SomethingCompletelyDifferent(x) => x.serialize(serializer),
956959
}
957960
}
958961
}
@@ -972,6 +975,11 @@ impl From<models::Goodbye> for Message {
972975
Self::Goodbye(Box::new(value))
973976
}
974977
}
978+
impl From<models::SomethingCompletelyDifferent> for Message {
979+
fn from(value: models::SomethingCompletelyDifferent) -> Self {
980+
Self::SomethingCompletelyDifferent(Box::new(value))
981+
}
982+
}
975983

976984
/// Converts Query Parameters representation (style=form, explode=false) to a Message value
977985
/// as specified in https://swagger.io/docs/specification/serialization/
@@ -983,3 +991,42 @@ impl std::str::FromStr for Message {
983991
serde_json::from_str(s)
984992
}
985993
}
994+
995+
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
996+
#[serde(untagged)]
997+
#[allow(non_camel_case_types)]
998+
pub enum SomethingCompletelyDifferent {
999+
VecOfObject(Box<Vec<crate::types::Object>>),
1000+
Object(Box<crate::types::Object>),
1001+
}
1002+
1003+
impl validator::Validate for SomethingCompletelyDifferent {
1004+
fn validate(&self) -> std::result::Result<(), validator::ValidationErrors> {
1005+
match self {
1006+
Self::VecOfObject(_) => std::result::Result::Ok(()),
1007+
Self::Object(x) => x.validate(),
1008+
}
1009+
}
1010+
}
1011+
1012+
impl From<Vec<crate::types::Object>> for SomethingCompletelyDifferent {
1013+
fn from(value: Vec<crate::types::Object>) -> Self {
1014+
Self::VecOfObject(Box::new(value))
1015+
}
1016+
}
1017+
impl From<crate::types::Object> for SomethingCompletelyDifferent {
1018+
fn from(value: crate::types::Object) -> Self {
1019+
Self::Object(Box::new(value))
1020+
}
1021+
}
1022+
1023+
/// Converts Query Parameters representation (style=form, explode=false) to a SomethingCompletelyDifferent value
1024+
/// as specified in https://swagger.io/docs/specification/serialization/
1025+
/// Should be implemented in a serde deserializer
1026+
impl std::str::FromStr for SomethingCompletelyDifferent {
1027+
type Err = serde_json::Error;
1028+
1029+
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
1030+
serde_json::from_str(s)
1031+
}
1032+
}

0 commit comments

Comments
 (0)