Skip to content

Commit

Permalink
fix(clients/java): ignore unknown JSON properties with object mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
menski committed Sep 23, 2019
1 parent 57d17e5 commit a5b0af1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.zeebe.client.api.command.InternalClientException;
import java.io.IOException;
Expand All @@ -31,6 +32,10 @@ public class ZeebeObjectMapper extends ObjectMapper {
private static final TypeReference<Map<String, String>> STRING_MAP_TYPE_REFERENCE =
new TypeReference<Map<String, String>>() {};

public ZeebeObjectMapper() {
this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

public <T> T fromJson(String json, Class<T> typeClass) {
try {
return readValue(json, typeClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import io.zeebe.client.api.command.ClientException;
import io.zeebe.client.api.response.ActivateJobsResponse;
import io.zeebe.client.impl.ZeebeObjectMapper;
import io.zeebe.client.impl.response.ActivatedJobImpl;
import io.zeebe.client.util.ClientTest;
import io.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest;
import io.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob;
Expand Down Expand Up @@ -225,4 +227,36 @@ public void shouldSetRequestTimeout() {
// then
assertThat(request.getRequestTimeout()).isEqualTo(requestTimeout.toMillis());
}

@Test
public void shouldDeserializePartiallyToPojo() {
// given
final ActivatedJobImpl activatedJob =
new ActivatedJobImpl(
new ZeebeObjectMapper(),
ActivatedJob.newBuilder()
.setCustomHeaders("{}")
.setVariables("{\"a\": 1, \"b\": 2}")
.build());

// when
final VariablesPojo variablesPojo = activatedJob.getVariablesAsType(VariablesPojo.class);

// then
assertThat(variablesPojo.getA()).isEqualTo(1);
}

static class VariablesPojo {

int a;

public int getA() {
return a;
}

public VariablesPojo setA(int a) {
this.a = a;
return this;
}
}
}

0 comments on commit a5b0af1

Please sign in to comment.