Skip to content

Commit

Permalink
Add failing test for #1831
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 6, 2017
1 parent 11fb5fa commit 8a2be69
Showing 1 changed file with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import java.io.IOException;
import java.util.*;

import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;

Expand All @@ -15,6 +17,7 @@
* Unit tests for verifying that "updating reader" works as
* expected.
*/
@SuppressWarnings("serial")
public class TestUpdateViaObjectReader extends BaseMapTest
{
static class Bean {
Expand Down Expand Up @@ -75,7 +78,43 @@ public DataA deserialize(JsonParser p, DeserializationContext ctxt) throws IOExc
return da;
}
}


// [databind#1831]
@JsonTypeInfo(use = Id.NAME)
@JsonSubTypes({ @JsonSubTypes.Type(value = Cat.class) })
static abstract public class AbstractAnimal { }

@JsonDeserialize(using = AnimalWrapperDeserializer.class)
static class AnimalWrapper {
@JsonUnwrapped
protected AbstractAnimal animal;

public void setAnimal(AbstractAnimal animal) {
this.animal = animal;
}
}

static class Cat extends AbstractAnimal { }

static class AnimalWrapperDeserializer extends StdDeserializer<AnimalWrapper> {
public AnimalWrapperDeserializer() {
super(AnimalWrapper.class);
}

@Override
public AnimalWrapper deserialize(JsonParser json, DeserializationContext context) throws IOException {
AnimalWrapper msg = new AnimalWrapper();
msg.setAnimal(json.readValueAs(AbstractAnimal.class));
return msg;
}

@Override
public AnimalWrapper deserialize(JsonParser json, DeserializationContext context, AnimalWrapper intoValue) throws IOException {
intoValue.setAnimal(json.readValueAs(AbstractAnimal.class));
return intoValue;
}
}

/*
/********************************************************
/* Test methods
Expand Down Expand Up @@ -218,4 +257,21 @@ public void testIssue744() throws IOException
assertEquals(5, dbUpdViaNode.da.i);
assertEquals(13, dbUpdViaNode.k);
}

// [databind#1831]
public void test1831UsingNode() throws IOException {
String catJson = MAPPER.writeValueAsString(new Cat());
JsonNode jsonNode = MAPPER.readTree(catJson);
AnimalWrapper optionalCat = new AnimalWrapper();
ObjectReader r = MAPPER.readerForUpdating(optionalCat);
AnimalWrapper result = r.readValue(jsonNode);
assertSame(optionalCat, result);
}

public void test1831UsingString() throws IOException {
String catJson = MAPPER.writeValueAsString(new Cat());
AnimalWrapper optionalCat = new AnimalWrapper();
AnimalWrapper result = MAPPER.readerForUpdating(optionalCat).readValue(catJson);
assertSame(optionalCat, result);
}
}

0 comments on commit 8a2be69

Please sign in to comment.