Skip to content

[Rust] Fix enum variant name generation #20689

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

Merged
merged 2 commits into from
Feb 26, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,9 @@ private void postProcessOneOfModels(List<ModelMap> allModels) {
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()))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -83,4 +84,10 @@ components:
- goodbye_message
required:
- op
- d
- d
SomethingCompletelyDifferent:
oneOf:
- items:
type: object
type: array
- type: object
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
}
}
}
Expand All @@ -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),
}
}
}
Expand All @@ -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/
Expand All @@ -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)
}
}
Loading