Skip to content

Commit

Permalink
Merge pull request #2 from connorbo97/override-read-unknowns-config
Browse files Browse the repository at this point in the history
add flag to SerializationConfig to disable READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE config
  • Loading branch information
henryabra authored Aug 4, 2021
2 parents 8eba69b + 00832eb commit 464ca20
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
public class SerializationConfig {
private Map<Class<?>, Class<?>> mixins;
private boolean shouldIgnoreAnySetterAnnotation;
private boolean shouldDisableReadUnknownEnumValuesAsDefaultValue;

public SerializationConfig() {
mixins = new HashMap<>();
shouldIgnoreAnySetterAnnotation = false;
shouldDisableReadUnknownEnumValuesAsDefaultValue = false;
}

public SerializationConfig setMixins(Map<Class<?>, Class<?>> mixins) {
Expand All @@ -22,13 +24,21 @@ public SerializationConfig setShouldIgnoreAnySetterAnnotation() {
return this;
}

public SerializationConfig setShouldDisableReadUnknownEnumValuesAsDefaultValue() {
this.shouldDisableReadUnknownEnumValuesAsDefaultValue = true;
return this;
}

public Map<Class<?>, Class<?>> getMixins() {
return mixins;
}

public boolean shouldIgnoreAnySetterAnnotation() {
return shouldIgnoreAnySetterAnnotation;
}
public boolean shouldDisableReadUnknownEnumValuesAsDefaultValue() {
return shouldDisableReadUnknownEnumValuesAsDefaultValue;
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ public static ObjectMapper createObjectMapper(SerializationConfig serializationC
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true);

if (!serializationConfig.shouldDisableReadUnknownEnumValuesAsDefaultValue()) {
objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true);
}

serializationConfig.getMixins().forEach(objectMapper::addMixIn);

if (serializationConfig.shouldIgnoreAnySetterAnnotation()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.taboola.rest.api.internal.serialization;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.taboola.rest.api.internal.config.SerializationConfig;

import org.junit.Assert;
Expand Down Expand Up @@ -68,17 +70,82 @@ public void createObjectMapper_serializationConfigWithIgnoreAnySetterAndApiObjec
Assert.assertNull("Name is parsed incorrectly", sampleApi.name);
}

@Test
public void createObjectMapper_defaultSerializationConfigAndApiObjectHasValidEnum_enumParsedCorrectly() throws IOException {
SerializationConfig serializationConfig = new SerializationConfig();
ObjectMapper objectMapper = SerializationMapperCreator.createObjectMapper(serializationConfig);

SampleApi sampleApi = objectMapper.readValue("{ \"letter\": \"B\" }", SampleApi.class);

Assert.assertEquals("Unknown enum is parsed incorrectly", SampleEnum.B, sampleApi.letter);
}

@Test
public void createObjectMapper_defaultSerializationConfigAndApiObjectHasInvalidEnum_defaultValueUsedOnSerialization() throws IOException {
SerializationConfig serializationConfig = new SerializationConfig();
ObjectMapper objectMapper = SerializationMapperCreator.createObjectMapper(serializationConfig);

SampleApi sampleApi = objectMapper.readValue("{ \"letter\": \"D\" }", SampleApi.class);

Assert.assertEquals("Unknown enum is parsed incorrectly", SampleEnum.UNKNOWN, sampleApi.letter);
}

@Test
public void createObjectMapper_defaultSerializationConfigAndApiObjectHasEmptyEnumValue_defaultValueUsedOnSerialization() throws IOException {
SerializationConfig serializationConfig = new SerializationConfig();
ObjectMapper objectMapper = SerializationMapperCreator.createObjectMapper(serializationConfig);

SampleApi sampleApi = objectMapper.readValue("{ \"letter\": \"D\" }", SampleApi.class);

Assert.assertEquals("Unknown enum is parsed incorrectly", SampleEnum.UNKNOWN, sampleApi.letter);
}

@Test
public void createObjectMapper_serializationConfigWithReadUnknownEnumsDisabledAndApiObjectHasValidEnum_enumParsedCorrectly() throws IOException {
SerializationConfig serializationConfig = new SerializationConfig().setShouldDisableReadUnknownEnumValuesAsDefaultValue();
ObjectMapper objectMapper = SerializationMapperCreator.createObjectMapper(serializationConfig);

SampleApi sampleApi = objectMapper.readValue("{ \"letter\": \"B\" }", SampleApi.class);

Assert.assertEquals("Unknown enum is parsed incorrectly", SampleEnum.B, sampleApi.letter);
}

@Test(expected = InvalidFormatException.class)
public void createObjectMapper_serializationConfigWithReadUnknownEnumsDisabledAndApiObjectHasInvalidEnum_InvalidFormatExceptionIsThrown() throws IOException {
SerializationConfig serializationConfig = new SerializationConfig().setShouldDisableReadUnknownEnumValuesAsDefaultValue();
ObjectMapper objectMapper = SerializationMapperCreator.createObjectMapper(serializationConfig);

objectMapper.readValue("{ \"letter\": \"D\" }", SampleApi.class);
}

@Test(expected = InvalidFormatException.class)
public void createObjectMapper_serializationConfigWithReadUnknownEnumsDisabledAndApiObjectHasEmptyEnumValue_InvalidFormatExceptionIsThrown() throws IOException {
SerializationConfig serializationConfig = new SerializationConfig().setShouldDisableReadUnknownEnumValuesAsDefaultValue();
ObjectMapper objectMapper = SerializationMapperCreator.createObjectMapper(serializationConfig);

objectMapper.readValue("{ \"letter\": \"\" }", SampleApi.class);
}

private enum SampleEnum {
A, B, @JsonEnumDefaultValue UNKNOWN
}

private static class SampleApi {
@JsonProperty("id")
int id;

@JsonProperty("letter")
SampleEnum letter;

@JsonProperty("name")
String name;

@JsonAnySetter
public void handlerUnknownSetter(String field, Object value) throws Exception {
throw new Exception("unknown field");
}


}

private abstract class SampleMixIn {
Expand Down

0 comments on commit 464ca20

Please sign in to comment.