Skip to content

Commit

Permalink
fix(specs): support synonyms type in camel case [skip-bc] (#4031)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Oct 28, 2024
1 parent c16b91d commit 32f9f4d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ public static String toEnum(String value) {
}

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

return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,28 @@ public String toEnumDefaultValue(CodegenProperty property, String value) {
// always default to None in the client, to let the server handle the default value.
return "None";
}

public static String toEnum(String value) {
// we only convert the full lowercase enums as they have a fallback to camelCase enums with the
// same name so no BC is introduced
if (!value.toLowerCase().equals(value)) {
// special edge case for onewaysynonym because there's no way to distinguish the two at the
// generation level
if (value.equals("'oneWaySynonym'")) {
return Helpers.toSnakeCase(value);
}
return value;
}
return Helpers.toScreamingSnakeCase(value);
}

@Override
public String toEnumVariableName(String value, String datatype) {
return super.toEnumVariableName(toEnum(value), datatype);
}

@Override
public String toEnumVarName(String value, String datatype) {
return super.toEnumVarName(toEnum(value), datatype);
}
}
10 changes: 10 additions & 0 deletions generators/src/main/java/com/algolia/codegen/utils/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public static String camelize(String kebabStr) {
return camel;
}

// convert camelCase77String to CAMEL_CASE_77_STRING
public static String toScreamingSnakeCase(String camelCase) {
return camelCase
.replaceAll("-", "_")
.replaceAll("/", "_")
.replaceAll("(?<=.)([A-Z]+|\\d+)", "_$1")
.replaceAll("__", "_")
.toUpperCase(Locale.ROOT);
}

/**
* Will add the boolean `vendorExtensions.x-is-custom-request` to operations if they should not
* escape '/' in the path variable
Expand Down
3 changes: 3 additions & 0 deletions specs/search/paths/synonyms/common/parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ SynonymType:
- altcorrection1
- altcorrection2
- placeholder
- oneWaySynonym
- altCorrection1
- altCorrection2

ObjectID:
name: objectID
Expand Down

0 comments on commit 32f9f4d

Please sign in to comment.