|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonSetter; |
| 6 | +import com.fasterxml.jackson.annotation.JsonSubTypes; |
| 7 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 8 | +import com.fasterxml.jackson.annotation.Nulls; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 12 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 13 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | + |
| 17 | +// [databind#4309] : Use @JsonSetter(nulls=...) handling of null values during deserialization with |
| 18 | +public class NullsSkip4309Test |
| 19 | +{ |
| 20 | + static class Data1 { |
| 21 | + public List<Type> types; |
| 22 | + } |
| 23 | + |
| 24 | + static enum Type { |
| 25 | + ONE, TWO |
| 26 | + } |
| 27 | + |
| 28 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", include = JsonTypeInfo.As.EXISTING_PROPERTY, visible = true) |
| 29 | + @JsonSubTypes(value = { @JsonSubTypes.Type(value = DataType1.class, names = { "TYPE1" }) }) |
| 30 | + static abstract class Data2 { |
| 31 | + public String type; |
| 32 | + } |
| 33 | + |
| 34 | + static class DataType1 extends Data2 { } |
| 35 | + |
| 36 | + @Test |
| 37 | + void shouldSkipUnknownEnumDeserializationWithSetter() throws Exception |
| 38 | + { |
| 39 | + // Given |
| 40 | + String json = "{ \"types\" : [\"TWO\", \"THREE\"] }"; |
| 41 | + |
| 42 | + // When |
| 43 | + Data1 data = JsonMapper.builder() |
| 44 | + .defaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP)) |
| 45 | + .enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL) |
| 46 | + .build() |
| 47 | + .readValue(json, Data1.class); // will be [TWO, null] |
| 48 | + |
| 49 | + // Then |
| 50 | + assertEquals(1, data.types.size()); |
| 51 | + assertEquals(Type.TWO, data.types.get(0)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void shouldSkipUnknownSubTypeDeserializationWithSetter() throws Exception |
| 56 | + { |
| 57 | + // Given |
| 58 | + String json = "[ {\"type\":\"TYPE1\" }, { \"type\" : \"TYPE2\" } ]"; |
| 59 | + |
| 60 | + // When |
| 61 | + List<Data2> actual = JsonMapper.builder() |
| 62 | + .defaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP)) |
| 63 | + .disable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE) |
| 64 | + .build() |
| 65 | + .readValue(json, new TypeReference<List<Data2>>() {}); |
| 66 | + |
| 67 | + // Then |
| 68 | + assertEquals(1, actual.size()); |
| 69 | + assertEquals(DataType1.class, actual.get(0).getClass()); |
| 70 | + } |
| 71 | +} |
0 commit comments