Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: decimals in structs should display as numeric #4165

Merged
merged 1 commit into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.util.Collections;
import org.apache.kafka.connect.data.Struct;
import org.apache.kafka.connect.json.DecimalFormat;
import org.apache.kafka.connect.json.JsonConverter;
import org.apache.kafka.connect.json.JsonConverterConfig;

public class StructSerializationModule extends SimpleModule {

Expand All @@ -32,7 +34,10 @@ public class StructSerializationModule extends SimpleModule {

public StructSerializationModule() {
super();
jsonConverter.configure(Collections.singletonMap("schemas.enable", false), false);
jsonConverter.configure(ImmutableMap.of(
JsonConverterConfig.SCHEMAS_ENABLE_CONFIG, false,
JsonConverterConfig.DECIMAL_FORMAT_CONFIG, DecimalFormat.NUMERIC.name()
), false);
addSerializer(Struct.class, new StructSerializationModule.Serializer());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.confluent.ksql.util.DecimalUtil;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -48,6 +50,7 @@ public class StructSerializationModuleTest {
.field("ITEMID", Schema.INT64_SCHEMA)
.field("NAME", Schema.STRING_SCHEMA)
.field("CATEGORY", categorySchema)
.field("COST", DecimalUtil.builder(4, 2).build())
.optional().build();

private ObjectMapper objectMapper;
Expand Down Expand Up @@ -82,9 +85,10 @@ public void shouldSerializeStructWithNestedStructCorrectly() throws JsonProcessi
item.put("ITEMID", 1L);
item.put("NAME", "ICE CREAM");
item.put("CATEGORY", category);
item.put("COST", new BigDecimal("10.01"));
final byte[] serializedBytes = objectMapper.writeValueAsBytes(item);
final String jsonString = new String(serializedBytes, StandardCharsets.UTF_8);
assertThat(jsonString, equalTo("{\"ITEMID\":1,\"NAME\":\"ICE CREAM\",\"CATEGORY\":{\"ID\":1,\"NAME\":\"Food\"}}"));
assertThat(jsonString, equalTo("{\"ITEMID\":1,\"NAME\":\"ICE CREAM\",\"CATEGORY\":{\"ID\":1,\"NAME\":\"Food\"},\"COST\":10.01}"));
}

@Test
Expand All @@ -99,7 +103,7 @@ public void shouldSerializeStructWithNestedStructAndNullFieldsCorrectly() throws
item.put("CATEGORY", null);
final byte[] serializedBytes = objectMapper.writeValueAsBytes(item);
final String jsonString = new String(serializedBytes, StandardCharsets.UTF_8);
assertThat(jsonString, equalTo("{\"ITEMID\":1,\"NAME\":\"ICE CREAM\",\"CATEGORY\":null}"));
assertThat(jsonString, equalTo("{\"ITEMID\":1,\"NAME\":\"ICE CREAM\",\"CATEGORY\":null,\"COST\":null}"));
}

@Test
Expand All @@ -122,6 +126,6 @@ public void shouldSerializeStructInsideListCorrectly() throws JsonProcessingExce

final byte[] serializedBytes = objectMapper.writeValueAsBytes(list);
final String jsonString = new String(serializedBytes, StandardCharsets.UTF_8);
assertThat(jsonString, equalTo("[\"Hello\",1,1,1.0,{\"ITEMID\":1,\"NAME\":\"ICE CREAM\",\"CATEGORY\":null}]"));
assertThat(jsonString, equalTo("[\"Hello\",1,1,1.0,{\"ITEMID\":1,\"NAME\":\"ICE CREAM\",\"CATEGORY\":null,\"COST\":null}]"));
}
}