Skip to content

Commit

Permalink
Merge pull request #804 from camunda-community-hub/connectors-issues-…
Browse files Browse the repository at this point in the history
…2648 (#805)

bug(object-mapper): Fix our custom mappers creation to avoid mutating the initial ObjectMapper
  • Loading branch information
johnBgood authored May 31, 2024
1 parent 7344664 commit 9ae5bed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public JsonMapper jsonMapper() {
if (objectMapper == null) {
return new ZeebeObjectMapper();
}
return new ZeebeObjectMapper(objectMapper);
return new ZeebeObjectMapper(objectMapper.copy());
}

@Bean(name = "commonJsonMapper")
Expand All @@ -33,6 +33,6 @@ public io.camunda.common.json.JsonMapper commonJsonMapper() {
if (objectMapper == null) {
return new SdkObjectMapper();
}
return new SdkObjectMapper(objectMapper);
return new SdkObjectMapper(objectMapper.copy());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.camunda.zeebe.spring.client.config;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.camunda.common.json.JsonMapper;
import io.camunda.zeebe.spring.client.configuration.JsonMapperConfiguration;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = {JacksonAutoConfiguration.class, JsonMapperConfiguration.class})
public class JsonMapperConfigurationTest {

@Autowired private io.camunda.zeebe.client.api.JsonMapper zeebeJsonMapper;

@Autowired private JsonMapper commonJsonMapper;

@Test
public void shouldSerializeNullValuesInJson() throws JsonProcessingException {
// given
final Map<String, Object> map = new HashMap<>();
map.put("key", null);
map.put("key2", "value2");
// when
final JsonNode zeebeJsonNode = new ObjectMapper().readTree(zeebeJsonMapper.toJson(map));
final JsonNode commonJsonNode = new ObjectMapper().readTree(commonJsonMapper.toJson(map));

// then
assertThat(zeebeJsonNode.get("key")).isNotNull();
assertThat(commonJsonNode.get("key")).isNull();

assertThat(zeebeJsonNode.get("key2").asText()).isEqualTo("value2");
assertThat(commonJsonNode.get("key2").asText()).isEqualTo("value2");
}
}

0 comments on commit 9ae5bed

Please sign in to comment.