Skip to content

Commit 32f9f4d

Browse files
authored
fix(specs): support synonyms type in camel case [skip-bc] (#4031)
1 parent c16b91d commit 32f9f4d

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

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

+1-2
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

+24
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

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ 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
47+
.replaceAll("-", "_")
48+
.replaceAll("/", "_")
49+
.replaceAll("(?<=.)([A-Z]+|\\d+)", "_$1")
50+
.replaceAll("__", "_")
51+
.toUpperCase(Locale.ROOT);
52+
}
53+
4454
/**
4555
* Will add the boolean `vendorExtensions.x-is-custom-request` to operations if they should not
4656
* escape '/' in the path variable

specs/search/paths/synonyms/common/parameters.yml

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ SynonymType:
3030
- altcorrection1
3131
- altcorrection2
3232
- placeholder
33+
- oneWaySynonym
34+
- altCorrection1
35+
- altCorrection2
3336

3437
ObjectID:
3538
name: objectID

0 commit comments

Comments
 (0)