Я использую Rest Assured в своих интеграционных тестах и получаю исключение при десериализации тел ответов в их соответствующие сущности, когда сущность включает перечисление с пользовательским десериализатором.Я предполагаю, что Rest Assured не знает, не запускает или как-то блокирует пользовательский десериализатор?
Вот исключение, которое я получаю:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "displayName" (class jafwatt.kompreno.model.Project), not marked as ignorable (5 known properties: "workspaceId", "estimateType", "projectId", "phases", "projectName"])
at [Source: (String)"{"projectId":9,"workspaceId":74,"projectName":"Test Project 1dea0d3e-4bba-4cc2-ace3-77dede4990d5","phases":[],"estimateType":{"name":"DAY","displayName":"Days","id":1}}"; line: 1, column: 155] (through reference chain: jafwatt.kompreno.model.Project["displayName"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3042)
at com.fasterxml.jackson.databind.ObjectMapper$readValue$2.call(Unknown Source)
at io.restassured.internal.path.json.mapping.JsonPathJackson2ObjectDeserializer.deserialize(JsonPathJackson2ObjectDeserializer.groovy:47)
at io.restassured.path.json.mapping.JsonPathObjectDeserializer$deserialize.call(Unknown Source)
at io.restassured.internal.mapping.Jackson2Mapper.deserialize(Jackson2Mapper.groovy:58)
at io.restassured.mapper.ObjectMapper$deserialize$0.call(Unknown Source)
at io.restassured.internal.mapping.ObjectMapping.parseWithJackson2(ObjectMapping.groovy:204)
at io.restassured.internal.mapping.ObjectMapping$parseWithJackson2$1.callStatic(Unknown Source)
at io.restassured.internal.mapping.ObjectMapping.deserialize(ObjectMapping.groovy:57)
at io.restassured.internal.mapping.ObjectMapping$deserialize$0.call(Unknown Source)
at io.restassured.internal.RestAssuredResponseOptionsGroovyImpl.as(RestAssuredResponseOptionsGroovyImpl.groovy:220)
at io.restassured.internal.RestAssuredResponseOptionsImpl.as(RestAssuredResponseOptionsImpl.java:166)
at jafwatt.kompreno.tests.steps.APISteps.processAPIResponse(APISteps.java:155)
at jafwatt.kompreno.tests.steps.APISteps.doAPIRequest(APISteps.java:50)
at ✽.a POST request is made to the project API(C:/code/kompreno/tests/src/test/resources/features/api/plan.feature:8)
Вот как я запускаюдесериализация @
private void processAPIResponse(final ExtractableResponse response) {
Object entity = response.body().as(Project.class);
}
Вот целевая сущность:
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Project {
@Column
@NotNull
@Enumerated(EnumType.STRING)
private WorkPackageEstimateType estimateType = WorkPackageEstimateType.DAY;
public WorkPackageEstimateType getEstimateType() {
return estimateType;
}
public void setEstimateType(WorkPackageEstimateType estimateType) {
this.estimateType = estimateType;
}
}
Вот перечисление:
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonSerialize(using = WorkPackageEstimateTypeSerializer.class)
@JsonDeserialize(using = WorkPackageEstimateTypeDeserializer.class)
public enum WorkPackageEstimateType {
HOUR("Hours"),
DAY("Days"),
WEEK("Weeks"),
STORY_POINT("Story Points"),
ITERATION("Iterations");
private String displayName;
WorkPackageEstimateType(String displayName) {
this.displayName = displayName;
}
public Integer getId() {
return ordinal();
}
public String getName() {
return toString();
}
public String getDisplayName() {
return displayName;
}
}
А вот пользовательский десериализатор:
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
public class WorkPackageEstimateTypeDeserializer extends StdDeserializer<WorkPackageEstimateType> {
public WorkPackageEstimateTypeDeserializer() {
super(WorkPackageEstimateType.class);
}
public WorkPackageEstimateTypeDeserializer(Class<WorkPackageEstimateType> t) {
super(t);
}
@Override
public WorkPackageEstimateType deserialize(JsonParser parser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
WorkPackageEstimateType value = null;
JsonToken currentToken = null;
while ((currentToken = parser.nextValue()) != null) {
if (parser.getCurrentName().equals("name")) {
value = WorkPackageEstimateType.valueOf(parser.getText());
break;
}
}
return value;
}
}