The Jooq Codegenerator creates Enums (when it can) for database columns that allow it to.
It would be helpful to let the Enums be Deserializable by Libraries like Jackson, which is currently not the case.
My Use-Case:
I have DTOs where i use the same Enums that Jooq has generated and uses in the generated Jooq-Records, because the Enums are helpful to have for Typesafety Reasons when checking the Values.
I have a Frontend Codegenerator (Orval), that creates Frontend Classes for Backend Endpoints and Models (including Enums)
My Frontend sends a POST to the Backend where the content contains such a DTO value, but the Deserialization of Jackson is unable to deserialize the given String value to the expected Enum, as the Enum does not provide Jackson a way to detect which function to call for Deserialization. Typically:
@JsonValue
@JsonCreator
What is considered the Best Practice for this Use-Case?
Example of a Jooq Codegen created Enum:
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes", "this-escape" })
public enum MatchingslotStatus implements EnumType {
AVAILABLE("AVAILABLE"),
SEARCHING("SEARCHING"),
ANSWER_WAITING("ANSWER_WAITING"),
ANSWER_EXPECTED("ANSWER_EXPECTED"),
FINISHED("FINISHED");
private final String literal;
private MatchingslotStatus(String literal) {
this.literal = literal;
}
@Override
public Catalog getCatalog() {
return null;
}
@Override
public Schema getSchema() {
return null;
}
@Override
public String getName() {
return null;
}
@Override
public String getLiteral() {
return literal;
}
/**
* Lookup a value of this EnumType by its literal. Returns
* <code>null</code>, if no such value could be found, see {@link
* EnumType#lookupLiteral(Class, String)}.
*/
public static MatchingslotStatus lookupLiteral(String literal) {
return EnumType.lookupLiteral(MatchingslotStatus.class, literal);
}
}
The problem in the question could not be reproduced (anymore).
When testing the problem, jackson correctly deserializes json to the jooq generated enum:
public class Main {
public static void main(String[] args) throws JsonProcessingException {
String jsonStr = "{\"status\": \"GENDER\" }";
MyTest test = new ObjectMapper().readValue(jsonStr, MyTest.class);
System.out.println(test);
}
public static final class MyTest {
MatchingslotfilterFiltertype status;
public MatchingslotfilterFiltertype getStatus() {
return status;
}
public void setStatus(MatchingslotfilterFiltertype status) {
this.status = status;
}
}
}