Created
September 12, 2015 12:15
-
-
Save jloisel/3e21cc14fb5779c2b1d3 to your computer and use it in GitHub Desktop.
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
package com.grinstone.entity.design; | |
import static com.google.common.base.Preconditions.checkNotNull; | |
import static org.junit.Assert.assertEquals; | |
import java.io.IOException; | |
import java.util.Optional; | |
import org.junit.Test; | |
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.fasterxml.jackson.annotation.JsonSubTypes; | |
import com.fasterxml.jackson.annotation.JsonSubTypes.Type; | |
import com.fasterxml.jackson.annotation.JsonTypeInfo; | |
import com.fasterxml.jackson.annotation.JsonTypeInfo.As; | |
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import lombok.Value; | |
public class JacksonBugTest { | |
private static final ObjectMapper MAPPER = new ObjectMapper(); | |
static { | |
// Have JDK 8 module in classpath | |
MAPPER.findAndRegisterModules(); | |
} | |
@Value | |
public static class Container { | |
Optional<Contained> contained; | |
@JsonCreator | |
public Container(@JsonProperty("contained") final Optional<Contained> contained) { | |
super(); | |
this.contained = checkNotNull(contained); | |
} | |
public Optional<Contained> getContained() { | |
return contained; | |
} | |
} | |
@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY) | |
@JsonSubTypes({ | |
@Type(name = "ContainedImpl", value = ContainedImpl.class), | |
@Type(name = "AnotherContainedImpl", value = AnotherContainedImpl.class) | |
}) | |
public static interface Contained { | |
} | |
@Value | |
public static class ContainedImpl implements Contained { | |
} | |
@Value | |
public static class AnotherContainedImpl implements Contained { | |
} | |
@Test | |
public void shouldSerializeCorrectly() throws IOException { | |
final Container dto = new Container(Optional.of(new AnotherContainedImpl())); | |
final String json = MAPPER.writeValueAsString(dto); | |
final Container fromJson = MAPPER.readValue(json, Container.class); | |
assertEquals(dto, fromJson); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment