Skip to content

Commit

Permalink
feat(jackson): enable 'accept single value as array' globally (#3851)
Browse files Browse the repository at this point in the history
* feat(jackson): enable 'accept single value as array' globally

* add single value deserialization tests

* lint
  • Loading branch information
chillleader authored Jan 16, 2025
1 parent 84bade1 commit 8b19159
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class ConnectorsObjectMapperSupplier {
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.build();

private ConnectorsObjectMapperSupplier() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,53 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import io.camunda.connector.document.annotation.jackson.DocumentReferenceModel.CamundaDocumentReferenceModel;
import io.camunda.connector.document.annotation.jackson.JacksonModuleDocumentDeserializer.DocumentModuleSettings;
import io.camunda.document.Document;
import io.camunda.document.factory.DocumentFactoryImpl;
import io.camunda.document.store.InMemoryDocumentStore;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

class ConnectorsObjectMapperSupplierTest {

@Test
void objectMapperConfigTest() throws JsonProcessingException {
void java8DatesShouldBeSupported() throws JsonProcessingException {
final var objectMapper = ConnectorsObjectMapperSupplier.getCopy();
final var json = "{\"data\":\"2024-01-01\"}";
final var jsonDeserialized = Map.of("data", LocalDate.of(2024, 1, 1));
Assertions.assertThat(objectMapper.writeValueAsString(jsonDeserialized)).isEqualTo(json);
var actual = objectMapper.readValue(json, new TypeReference<Map<String, LocalDate>>() {});
Assertions.assertThat(actual).isEqualTo(jsonDeserialized);
}

@Test
void singlePrimitiveValueShouldBeAcceptedAsArray() throws JsonProcessingException {
final var objectMapper = ConnectorsObjectMapperSupplier.getCopy();
final var json = "1";
var actual = objectMapper.readValue(json, int[].class);
Assertions.assertThat(actual).isEqualTo(new int[] {1});
}

@Test
void singleDocumentShouldBeAcceptedAsArray() throws JsonProcessingException {
final var objectMapper =
ConnectorsObjectMapperSupplier.getCopy(
new DocumentFactoryImpl(InMemoryDocumentStore.INSTANCE),
DocumentModuleSettings.create());
final var documentReference =
new CamundaDocumentReferenceModel(
"default", UUID.randomUUID().toString(), null, Optional.empty());
final var json = "{\"documents\":" + objectMapper.writeValueAsString(documentReference) + "}";
var actual = objectMapper.readValue(json, TestRecordWithDocumentList.class);
Assertions.assertThat(actual.documents()).hasSize(1);
Assertions.assertThat(actual.documents().get(0).reference()).isEqualTo(documentReference);
}

private record TestRecordWithDocumentList(List<Document> documents) {}
}

0 comments on commit 8b19159

Please sign in to comment.