Casting an unknown enum value to default enum in spring boot mongo repository actions

Viewed 237

I have problem while casting an unknown enum to a default enum in spring boot while using mongo repository.

This is the enum.

public enum EventType implements Serializable
{
  WORKDONE("WORKDONE"),
  ODRCOM("ODRCOM"),
  EXECUTED("EXECUTED"),
  REBOOK("REBOOK"),
  MANUAL("MANUAL"),
  UNKNOWN("UNKNOWN");
  
  private String value;
  EventType(final String type) {
    this.value = type;
  }
  
  @Override
  public String toString() {
      return value;
  }
}

And here is my model class

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Event {
  
  //other properties
  
  @JsonProperty("eventType")
  private EventType eventType;
  
  @JsonIgnore
  private Map<String, Object> additionalProperties = new HashMap<String, Object>();
  
  @JsonProperty("eventType")
  public EventType getEventType() {
    return eventType;
  }
  
  
  @JsonProperty("eventType")
  public void setEventType(String eventType) {
    this.eventType = Optional.ofNullable(EventType.valueOf(eventType)).orElse(EventType.UNKNOWN);
  }
  
  //other getters and setters
  
}

Here is the mongo repository

public interface EventRepository extends MongoRepository<Event, String> {
  
}

The document stored in the db is of the following stucture

{
...
"eventType" : "REBOOK1",
...
}

Please note that the REBOOK1 is not a valid enum. But the setter should be able to able to cast anything else to type UNKNOWN. However it gives this exception everytime

No enum constant dk.nuuday.ossieventprocessor.app.model.EventType.REBOOK1

I have tried with adding a custom converter as a configuration but no luck Any help is greatly appreciated

0 Answers
Related