Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -386,7 +386,7 @@ public String toParamName(String name) {

@Override
public String toEnumValue(String value, String datatype) {
// rust-server templates expect value to be in quotes
// rust-server templates expect value to be in quotes for Display/FromStr
return "\"" + super.toEnumValue(value, datatype) + "\"";
}

Expand Down Expand Up @@ -1565,6 +1565,31 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
public ModelsMap postProcessModels(ModelsMap objs) {
ModelsMap result = super.postProcessModelsEnum(objs);

// Fix for integer enums: add unquoted numeric values for serde serialization.
// Integer enums should serialize as JSON numbers, not strings.
for (ModelMap modelMap : result.getModels()) {
CodegenModel model = modelMap.getModel();

if (Boolean.TRUE.equals(model.isEnum) &&
(model.isInteger || model.isLong || model.isNumber) &&
model.allowableValues != null) {

@SuppressWarnings("unchecked")
List<Map<String, Object>> enumVars =
(List<Map<String, Object>>) model.allowableValues.get("enumVars");

if (enumVars != null) {
for (Map<String, Object> enumVar : enumVars) {
String value = (String) enumVar.get("value");
if (value != null) {
// Strip quotes added by toEnumValue()
enumVar.put("numericValue", value.substring(1, value.length() - 1));
}
}
}
}
}

// Check for model names that conflict with serde_valid macro internals
// Once we find one, set a class-level flag that persists across all model batches
if (!hasConflictingModelNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ use crate::header;
pub enum {{{classname}}} {
{{#allowableValues}}
{{#enumVars}}
{{#numericValue}}
#[serde(rename = {{{numericValue}}})]
{{/numericValue}}
{{^numericValue}}
#[serde(rename = {{{value}}})]
{{/numericValue}}
{{{name}}},
{{/enumVars}}
{{/allowableValues}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3542,9 +3542,9 @@ impl EnumTest {
#[cfg_attr(feature = "validate", derive(Validate))]
#[cfg_attr(feature = "conversion", derive(frunk_enum_derive::LabelledGenericEnum))]
pub enum EnumTestEnumInteger {
#[serde(rename = "1")]
#[serde(rename = 1)]
Variant1,
#[serde(rename = "-1")]
#[serde(rename = -1)]
Variant12,
}

Expand Down Expand Up @@ -8464,9 +8464,9 @@ impl TestEnumParametersEnumQueryDoubleParameter {
#[cfg_attr(feature = "validate", derive(Validate))]
#[cfg_attr(feature = "conversion", derive(frunk_enum_derive::LabelledGenericEnum))]
pub enum TestEnumParametersEnumQueryIntegerParameter {
#[serde(rename = "1")]
#[serde(rename = 1)]
Variant1,
#[serde(rename = "-2")]
#[serde(rename = -2)]
Variant2,
}

Expand Down
Loading