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

Allow overriding serde feature defaults for jackson object mapper in json serializer and deserializer #327

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -31,11 +31,12 @@

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toMap;

/**
* Glue Schema Registry Configuration entries.
*/
Expand Down Expand Up @@ -65,8 +66,8 @@ public class GlueSchemaRegistryConfiguration {
*/
private String userAgentApp = "default";

private List<SerializationFeature> jacksonSerializationFeatures;
private List<DeserializationFeature> jacksonDeserializationFeatures;
private Map<SerializationFeature, Boolean> jacksonSerializationFeatures;
private Map<DeserializationFeature, Boolean> jacksonDeserializationFeatures;

public GlueSchemaRegistryConfiguration(String region) {
Map<String, Object> config = new HashMap<>();
Expand Down Expand Up @@ -297,12 +298,12 @@ private void validateAndSetMetadata(Map<String, ?> configs) {

private void validateAndSetJacksonSerializationFeatures(Map<String, ?> configs) {
if (isPresent(configs, AWSSchemaRegistryConstants.JACKSON_SERIALIZATION_FEATURES)) {
if (configs.get(AWSSchemaRegistryConstants.JACKSON_SERIALIZATION_FEATURES) instanceof List) {
List<String> serialzationFeatures =
(List<String>) configs.get(AWSSchemaRegistryConstants.JACKSON_SERIALIZATION_FEATURES);
this.jacksonSerializationFeatures = serialzationFeatures.stream()
.map(sf -> SerializationFeature.valueOf(sf))
.collect(Collectors.toList());
if (configs.get(AWSSchemaRegistryConstants.JACKSON_SERIALIZATION_FEATURES) instanceof Map) {
Map<String, Boolean> serializationFeatures =
(Map<String, Boolean>) configs.get(AWSSchemaRegistryConstants.JACKSON_SERIALIZATION_FEATURES);
this.jacksonSerializationFeatures = serializationFeatures.entrySet()
.stream()
.collect(toMap(x -> SerializationFeature.valueOf(x.getKey()), Map.Entry::getValue));
} else {
throw new AWSSchemaRegistryException("Jackson Serialization features should be a list");
}
Expand All @@ -311,12 +312,12 @@ private void validateAndSetJacksonSerializationFeatures(Map<String, ?> configs)

private void validateAndSetJacksonDeserializationFeatures(Map<String, ?> configs) {
if (isPresent(configs, AWSSchemaRegistryConstants.JACKSON_DESERIALIZATION_FEATURES)) {
if (configs.get(AWSSchemaRegistryConstants.JACKSON_DESERIALIZATION_FEATURES) instanceof List) {
List<String> deserialzationFeatures =
(List<String>) configs.get(AWSSchemaRegistryConstants.JACKSON_DESERIALIZATION_FEATURES);
this.jacksonDeserializationFeatures = deserialzationFeatures.stream()
.map(dsf -> DeserializationFeature.valueOf(dsf))
.collect(Collectors.toList());
if (configs.get(AWSSchemaRegistryConstants.JACKSON_DESERIALIZATION_FEATURES) instanceof Map) {
Map<String, Boolean> deserializationFeatures =
(Map<String, Boolean>) configs.get(AWSSchemaRegistryConstants.JACKSON_DESERIALIZATION_FEATURES);
this.jacksonDeserializationFeatures = deserializationFeatures.entrySet()
.stream()
.collect(toMap(x -> DeserializationFeature.valueOf(x.getKey()), Map.Entry::getValue));
} else {
throw new AWSSchemaRegistryException("Jackson Deserialization features should be a list");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.amazonaws.services.schemaregistry.exception.AWSSchemaRegistryException;
import com.amazonaws.services.schemaregistry.serializers.json.JsonDataWithSchema;
import com.amazonaws.services.schemaregistry.common.Schema;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
Expand All @@ -29,7 +30,6 @@
import lombok.NonNull;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.io.IOException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -60,13 +60,9 @@ public JsonDeserializer(GlueSchemaRegistryConfiguration configs) {
this.objectMapper = new ObjectMapper();
this.objectMapper.setNodeFactory(jsonNodeFactory);
if (configs != null) {
if (!CollectionUtils.isEmpty(configs.getJacksonSerializationFeatures())) {
configs.getJacksonSerializationFeatures()
.forEach(this.objectMapper::enable);
}
if (!CollectionUtils.isEmpty(configs.getJacksonDeserializationFeatures())) {
if (configs.getJacksonDeserializationFeatures() != null && !configs.getJacksonDeserializationFeatures().isEmpty()) {
configs.getJacksonDeserializationFeatures()
.forEach(this.objectMapper::enable);
.forEach(this::overrideObjectMapperFeature);
}
}
}
Expand Down Expand Up @@ -109,4 +105,12 @@ public Object deserialize(@NonNull ByteBuffer buffer,
throw new AWSSchemaRegistryException(message, e);
}
}

private void overrideObjectMapperFeature(DeserializationFeature deserializationFeature, Boolean flag) {
if (flag) {
this.objectMapper.enable(deserializationFeature);
} else {
this.objectMapper.disable(deserializationFeature);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.kjetland.jackson.jsonSchema.JsonSchemaGenerator;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.nio.charset.StandardCharsets;

Expand All @@ -38,6 +38,7 @@
public class JsonSerializer implements GlueSchemaRegistryDataFormatSerializer {
private static final JsonValidator JSON_VALIDATOR = new JsonValidator();
private final JsonSchemaGenerator jsonSchemaGenerator;
@Getter
private final ObjectMapper objectMapper;
@Getter
@Setter
Expand All @@ -55,13 +56,9 @@ public JsonSerializer(GlueSchemaRegistryConfiguration configs) {
this.objectMapper = new ObjectMapper();
this.objectMapper.setNodeFactory(jsonNodeFactory);
if (configs != null) {
if (!CollectionUtils.isEmpty(configs.getJacksonSerializationFeatures())) {
if (configs.getJacksonSerializationFeatures() != null && !configs.getJacksonSerializationFeatures().isEmpty()) {
configs.getJacksonSerializationFeatures()
.forEach(this.objectMapper::enable);
}
if (!CollectionUtils.isEmpty(configs.getJacksonDeserializationFeatures())) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanted to check if this should be kept in?

configs.getJacksonDeserializationFeatures()
.forEach(this.objectMapper::enable);
.forEach(this::overrideObjectMapperFeature);
}
}
this.jsonSchemaGenerator = new JsonSchemaGenerator(this.objectMapper);
Expand Down Expand Up @@ -184,4 +181,12 @@ public void validate(Object jsonDataWithSchema) {
JsonNode dataNode = getDataNode(jsonDataWithSchema);
JSON_VALIDATOR.validateDataWithSchema(schemaNode, dataNode);
}

private void overrideObjectMapperFeature(SerializationFeature serializationFeature, Boolean flag) {
if (flag) {
this.objectMapper.enable(serializationFeature);
} else {
this.objectMapper.disable(serializationFeature);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.EncoderFactory;
import org.apache.commons.collections4.map.SingletonMap;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -591,7 +592,7 @@ public void testDeserialize_withSerdeConfigs_recordMatches(DataFormat dataFormat
configs.put(AWSSchemaRegistryConstants.COMPRESSION_TYPE, compressionType.name());
configs.put(AWSSchemaRegistryConstants.AVRO_RECORD_TYPE, avroRecordType);
configs.put(AWSSchemaRegistryConstants.JACKSON_DESERIALIZATION_FEATURES,
Arrays.asList(DeserializationFeature.EAGER_DESERIALIZER_FETCH.name()));
new SingletonMap<>(DeserializationFeature.EAGER_DESERIALIZER_FETCH.name(), true));
byte[] serializedData = createSerializedData(record, dataFormat, inputSchemaDefinition, schemaVersionId);

GetSchemaVersionResponse schemaVersionResponse = GetSchemaVersionResponse.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,31 @@
*/
package com.amazonaws.services.schemaregistry.deserializers.json;

import com.amazonaws.services.schemaregistry.common.configs.GlueSchemaRegistryConfiguration;
import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import com.amazonaws.services.schemaregistry.common.Schema;
import software.amazon.awssdk.services.glue.model.DataFormat;

import java.util.UUID;
import java.util.HashMap;
import java.util.Map;

import static com.amazonaws.services.schemaregistry.utils.AWSSchemaRegistryConstants.AWS_REGION;
import static com.amazonaws.services.schemaregistry.utils.AWSSchemaRegistryConstants.JACKSON_DESERIALIZATION_FEATURES;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static java.util.Collections.singletonMap;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JsonDeserializerTest {
private JsonDeserializer jsonDeserializer = new JsonDeserializer(null);

@Test
public void testDeserialize_nullArgs_throwsException() {
JsonDeserializer jsonDeserializer = new JsonDeserializer(null);
String testSchemaDefinition = "{\"$id\":\"https://example.com/geographical-location.schema.json\","
+ "\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"title\":\"Longitude "
+ "and Latitude Values\",\"description\":\"A geographical coordinate.\","
Expand All @@ -46,4 +55,28 @@ public void testDeserialize_nullArgs_throwsException() {
assertThrows(IllegalArgumentException.class, () -> jsonDeserializer.deserialize(ByteBuffer.wrap(testBytes),
null));
}

@Test
public void testDeserialize_overridesDeserializationFeatureToFalse() {
Map<String, Object> config = new HashMap<>();
config.put(JACKSON_DESERIALIZATION_FEATURES, singletonMap(FAIL_ON_UNKNOWN_PROPERTIES.name(), false));
config.put(AWS_REGION, "us-east-1");

GlueSchemaRegistryConfiguration glueSchemaRegistryConfiguration = new GlueSchemaRegistryConfiguration(config);
JsonDeserializer jsonDeserializer = new JsonDeserializer(glueSchemaRegistryConfiguration);

assertFalse(jsonDeserializer.getObjectMapper().isEnabled(FAIL_ON_UNKNOWN_PROPERTIES));
}

@Test
public void testDeserialize_overridesDeserializationFeatureToTrue() {
Map<String, Object> config = new HashMap<>();
config.put(JACKSON_DESERIALIZATION_FEATURES, singletonMap(FAIL_ON_NULL_FOR_PRIMITIVES.name(), true));
config.put(AWS_REGION, "us-east-1");

GlueSchemaRegistryConfiguration glueSchemaRegistryConfiguration = new GlueSchemaRegistryConfiguration(config);
JsonDeserializer jsonDeserializer = new JsonDeserializer(glueSchemaRegistryConfiguration);

assertTrue(jsonDeserializer.getObjectMapper().isEnabled(FAIL_ON_NULL_FOR_PRIMITIVES));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.collections4.map.SingletonMap;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -641,12 +642,12 @@ public void testSerializer_registerSchemaVersion_whenPutSchemaVersionMetadataThr
public void testRegisterSchemaVersion_withCustomJacksonConfiguration_succeeds(DataFormat dataFormat,
Object record,
AWSSchemaRegistryConstants.COMPRESSION compressionType) {
List<String> jacksonSerializationFeatures =
Arrays.asList(SerializationFeature.FLUSH_AFTER_WRITE_VALUE.name());
Map<String, Boolean> jacksonSerializationFeatures =
new SingletonMap<>(SerializationFeature.FLUSH_AFTER_WRITE_VALUE.name(), true);
configs.put(AWSSchemaRegistryConstants.JACKSON_SERIALIZATION_FEATURES,
jacksonSerializationFeatures);
List<String> jacksonDeserializationFeatures =
Arrays.asList(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS.name());
Map<String, Boolean> jacksonDeserializationFeatures =
new SingletonMap<>(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS.name(), true);
configs.put(AWSSchemaRegistryConstants.JACKSON_DESERIALIZATION_FEATURES,
jacksonDeserializationFeatures);
configs.put(AWSSchemaRegistryConstants.COMPRESSION_TYPE, compressionType.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import static com.amazonaws.services.schemaregistry.utils.AWSSchemaRegistryConstants.AWS_REGION;
import static com.amazonaws.services.schemaregistry.utils.AWSSchemaRegistryConstants.JACKSON_SERIALIZATION_FEATURES;
import static com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_SELF_REFERENCES;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_SELF_REFERENCES_AS_NULL;
import static java.util.Collections.singletonMap;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JsonSerializerTest {
private static final JsonDataWithSchema GENERIC_TEST_RECORD =
Expand Down Expand Up @@ -146,4 +154,30 @@ public void testGetSchemaDefinition_nullObject_throwsException() {
public void testSerialize_nullObject_throwsException() {
assertThrows(IllegalArgumentException.class, () -> jsonSerializer.serialize(null));
}

@Test
public void testSerialize_overridesSerializationFeatureToFalse() {
Map<String, Object> config = new HashMap<>();
config.put(JACKSON_SERIALIZATION_FEATURES, singletonMap(FAIL_ON_SELF_REFERENCES.name(), false));
config.put(AWS_REGION, "us-east-1");


GlueSchemaRegistryConfiguration glueSchemaRegistryConfiguration = new GlueSchemaRegistryConfiguration(config);
JsonSerializer jsonSerializer = new JsonSerializer(glueSchemaRegistryConfiguration);

assertFalse(jsonSerializer.getObjectMapper().isEnabled(FAIL_ON_SELF_REFERENCES));
}

@Test
public void testSerialize_overridesSerializationFeatureToTrue() {
Map<String, Object> config = new HashMap<>();
config.put(JACKSON_SERIALIZATION_FEATURES, singletonMap(WRITE_SELF_REFERENCES_AS_NULL.name(), true));
config.put(AWS_REGION, "us-east-1");


GlueSchemaRegistryConfiguration glueSchemaRegistryConfiguration = new GlueSchemaRegistryConfiguration(config);
JsonSerializer jsonSerializer = new JsonSerializer(glueSchemaRegistryConfiguration);

assertTrue(jsonSerializer.getObjectMapper().isEnabled(WRITE_SELF_REFERENCES_AS_NULL));
}
}