Skip to content

Commit fc988e1

Browse files
committed
fix: weird enum names
1 parent 14b209d commit fc988e1

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ public static String toEnum(String value) {
131131
}
132132

133133
if (!value.matches("[A-Z0-9_]+")) {
134-
// convert camelCase77String to CAMEL_CASE_77_STRING
135-
return value.replaceAll("-", "_").replaceAll("(.+?)([A-Z]|[0-9])", "$1_$2").toUpperCase(Locale.ROOT);
134+
return Helpers.toScreamingSnakeCase(value);
136135
}
137136

138137
return value;

generators/src/main/java/com/algolia/codegen/AlgoliaPythonGenerator.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,28 @@ public String toEnumDefaultValue(CodegenProperty property, String value) {
161161
// always default to None in the client, to let the server handle the default value.
162162
return "None";
163163
}
164+
165+
public static String toEnum(String value) {
166+
// we only convert the full lowercase enums as they have a fallback to camelCase enums with the
167+
// same name so no BC is introduced
168+
if (!value.toLowerCase().equals(value)) {
169+
// special edge case for onewaysynonym because there's no way to distinguish the two at the
170+
// generation level
171+
if (value.equals("'oneWaySynonym'")) {
172+
return Helpers.toSnakeCase(value);
173+
}
174+
return value;
175+
}
176+
return Helpers.toScreamingSnakeCase(value);
177+
}
178+
179+
@Override
180+
public String toEnumVariableName(String value, String datatype) {
181+
return super.toEnumVariableName(toEnum(value), datatype);
182+
}
183+
184+
@Override
185+
public String toEnumVarName(String value, String datatype) {
186+
return super.toEnumVarName(toEnum(value), datatype);
187+
}
164188
}

generators/src/main/java/com/algolia/codegen/utils/Helpers.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public static String camelize(String kebabStr) {
4141
return camel;
4242
}
4343

44+
// convert camelCase77String to CAMEL_CASE_77_STRING
45+
public static String toScreamingSnakeCase(String camelCase) {
46+
return camelCase.replaceAll("-", "_").replaceAll("(.+?)([A-Z]|[0-9])", "$1_$2").toUpperCase(Locale.ROOT);
47+
}
48+
4449
/**
4550
* Will add the boolean `vendorExtensions.x-is-custom-request` to operations if they should not
4651
* escape '/' in the path variable

0 commit comments

Comments
 (0)