[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`
This commit is contained in:
Elric Milon 2025-02-26 09:57:43 +01:00 committed by GitHub
parent 57b1c91d17
commit 481c69063d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 58 additions and 3 deletions

View File

@ -615,8 +615,9 @@ public class RustAxumServerCodegen extends AbstractRustCodegen implements Codege
for (CodegenProperty model : csOneOf) {
// Generate a valid name for the enum variant.
// Mainly needed for primitive types.
String[] modelParts = model.dataType.replace("<", "Of").replace(">", "").split("::");
model.datatypeWithEnum = camelize(modelParts[modelParts.length - 1]);
model.datatypeWithEnum = camelize(model.dataType.replaceAll("(?:\\w+::)+(\\w+)", "$1")
.replace("<", "Of").replace(">", ""));
// Primitive type is not properly set, this overrides it to guarantee adequate model generation.
if (!model.getDataType().matches(String.format(Locale.ROOT, ".*::%s", model.getDatatypeWithEnum()))) {

View File

@ -30,6 +30,7 @@ components:
- "$ref": "#/components/schemas/Hello"
- "$ref": "#/components/schemas/Greeting"
- "$ref": "#/components/schemas/Goodbye"
- "$ref": "#/components/schemas/SomethingCompletelyDifferent"
title: Message
Hello:
type: object
@ -84,3 +85,9 @@ components:
required:
- op
- d
SomethingCompletelyDifferent:
oneOf:
- items:
type: object
type: array
- type: object

View File

@ -932,6 +932,7 @@ pub enum Message {
Hello(Box<models::Hello>),
Greeting(Box<models::Greeting>),
Goodbye(Box<models::Goodbye>),
SomethingCompletelyDifferent(Box<models::SomethingCompletelyDifferent>),
}
impl validator::Validate for Message {
@ -940,6 +941,7 @@ impl validator::Validate for Message {
Self::Hello(x) => x.validate(),
Self::Greeting(x) => x.validate(),
Self::Goodbye(x) => x.validate(),
Self::SomethingCompletelyDifferent(x) => x.validate(),
}
}
}
@ -953,6 +955,7 @@ impl serde::Serialize for Message {
Self::Hello(x) => x.serialize(serializer),
Self::Greeting(x) => x.serialize(serializer),
Self::Goodbye(x) => x.serialize(serializer),
Self::SomethingCompletelyDifferent(x) => x.serialize(serializer),
}
}
}
@ -972,6 +975,11 @@ impl From<models::Goodbye> for Message {
Self::Goodbye(Box::new(value))
}
}
impl From<models::SomethingCompletelyDifferent> for Message {
fn from(value: models::SomethingCompletelyDifferent) -> Self {
Self::SomethingCompletelyDifferent(Box::new(value))
}
}
/// Converts Query Parameters representation (style=form, explode=false) to a Message value
/// as specified in https://swagger.io/docs/specification/serialization/
@ -983,3 +991,42 @@ impl std::str::FromStr for Message {
serde_json::from_str(s)
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
#[allow(non_camel_case_types)]
pub enum SomethingCompletelyDifferent {
VecOfObject(Box<Vec<crate::types::Object>>),
Object(Box<crate::types::Object>),
}
impl validator::Validate for SomethingCompletelyDifferent {
fn validate(&self) -> std::result::Result<(), validator::ValidationErrors> {
match self {
Self::VecOfObject(_) => std::result::Result::Ok(()),
Self::Object(x) => x.validate(),
}
}
}
impl From<Vec<crate::types::Object>> for SomethingCompletelyDifferent {
fn from(value: Vec<crate::types::Object>) -> Self {
Self::VecOfObject(Box::new(value))
}
}
impl From<crate::types::Object> for SomethingCompletelyDifferent {
fn from(value: crate::types::Object) -> Self {
Self::Object(Box::new(value))
}
}
/// Converts Query Parameters representation (style=form, explode=false) to a SomethingCompletelyDifferent value
/// as specified in https://swagger.io/docs/specification/serialization/
/// Should be implemented in a serde deserializer
impl std::str::FromStr for SomethingCompletelyDifferent {
type Err = serde_json::Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
serde_json::from_str(s)
}
}