In my Java 11 / Spring Boot codebase I have enums that don't respect the convention to have all uppercase constants because when we move to frontend the data there has to be camelCase (or even random / weird case).
I am not designing this from scratch, more dealing with something pre-existing.
Here's an Enum example:
public enum Direction {
Up,
Down
}
If I run SonarLint I get "code smell java:S115 - Constant names should comply with a naming convention".
This means that my enum should look like:
public enum Direction {
UP,
DOWN
}
This leads to other problems in the code because when I serialize / deserialize data (that I also get from clients) it has different case differently.
I have read this article on Baeldung and implemented a possible solution:
public enum Direction {
UP("Up"),
DOWN("Down");
private final String value;
Direction(String value) {
this.value = value;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
public static Direction fromValue(String value) {
for (Direction b : Direction.values()) {
if (b.value.equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}
This leads to issues in my code where instead of using Direction.valueOf(); I should be using Direction.fromValue();.
While this is feasible for code in my control all the libraries that are using valueOf (for example using Jackson?)...
Will get the wrong data?
How can I solve the issue in the cleanest way?
Edit
- As per suggestion I changed the Person example to a more suitable one.