-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/test/java/com/fasterxml/jackson/failing/NullsSkip4309Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.fasterxml.jackson.failing; | ||
|
||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSetter; | ||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.annotation.Nulls; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
// [databind#4309] : Use @JsonSetter(nulls=...) handling of null values during deserialization with | ||
public class NullsSkip4309Test | ||
{ | ||
static class Data1 { | ||
public List<Type> types; | ||
} | ||
|
||
static enum Type { | ||
ONE, TWO | ||
} | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", include = JsonTypeInfo.As.EXISTING_PROPERTY, visible = true) | ||
@JsonSubTypes(value = { @JsonSubTypes.Type(value = DataType1.class, names = { "TYPE1" }) }) | ||
static abstract class Data2 { | ||
public String type; | ||
} | ||
|
||
static class DataType1 extends Data2 { } | ||
|
||
@Test | ||
void shouldSkipUnknownEnumDeserializationWithSetter() throws Exception | ||
{ | ||
// Given | ||
String json = "{ \"types\" : [\"TWO\", \"THREE\"] }"; | ||
|
||
// When | ||
Data1 data = JsonMapper.builder() | ||
.defaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP)) | ||
.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL) | ||
.build() | ||
.readValue(json, Data1.class); // will be [TWO, null] | ||
|
||
// Then | ||
assertEquals(1, data.types.size()); | ||
assertEquals(Type.TWO, data.types.get(0)); | ||
} | ||
|
||
@Test | ||
void shouldSkipUnknownSubTypeDeserializationWithSetter() throws Exception | ||
{ | ||
// Given | ||
String json = "[ {\"type\":\"TYPE1\" }, { \"type\" : \"TYPE2\" } ]"; | ||
|
||
// When | ||
List<Data2> actual = JsonMapper.builder() | ||
.defaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP)) | ||
.disable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE) | ||
.build() | ||
.readValue(json, new TypeReference<List<Data2>>() {}); | ||
|
||
// Then | ||
assertEquals(1, actual.size()); | ||
assertEquals(DataType1.class, actual.get(0).getClass()); | ||
} | ||
} |