Skip to content

fix(specs): support synonyms type in camel case [skip-bc] #4031

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 4 commits into from
Oct 28, 2024
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 @@ -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
Loading