Skip to content

Commit

Permalink
Fix #592
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 29, 2014
1 parent 7471116 commit bcccd3e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
7 changes: 5 additions & 2 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ Steve Sanbeg: (sanbeg@github)
(2.4.1)

Ian Barfield: (tea-dragon@github)
* Reported #580: delegate deserializers choke on a (single) abstract/polymorphic parameter

* Reported #580: delegate deserializers choke on a (single) abstract/polymorphic parameter
(2.4.4)

Eugene Lukash
* Reported #592: Wrong `TokenBuffer` delegate deserialization using `@JsonCreator`
(2.4.4)
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project: jackson-databind
(reported by Ian B, tea-dragon@github)
#590: Binding invalid Currency gives nonsense at end of the message
(reported by Jerbell@github)
#592: Wrong `TokenBuffer` delegate deserialization using `@JsonCreator`
(reported by Eugene L)

2.4.3 (02-Oct-2014)

Expand Down
21 changes: 18 additions & 3 deletions src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,25 @@ public void serialize(JsonGenerator jgen)
*
* @since 2.3
*/
public TokenBuffer deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
public TokenBuffer deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException
{
copyCurrentStructure(jp);
if (jp.getCurrentTokenId() != JsonToken.FIELD_NAME.id()) {
copyCurrentStructure(jp);
return this;
}
/* 28-Oct-2014, tatu: As per #592, need to support a special case of starting from
* FIELD_NAME, which is taken to mean that we are missing START_OBJECT, but need
* to assume one did exist.
*/
JsonToken t;
writeStartObject();
do {
copyCurrentStructure(jp);
} while ((t = jp.nextToken()) == JsonToken.FIELD_NAME);
if (t != JsonToken.END_OBJECT) {
throw ctxt.mappingException("Expected END_OBJECT after copying contents of a JsonParser into TokenBuffer, got "+t);
}
writeEndObject();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JacksonInject;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.util.TokenBuffer;

public class TestCreatorsDelegating extends BaseMapTest
{
Expand Down Expand Up @@ -51,7 +53,21 @@ public static FactoryBean711 create(@JacksonInject String n1, int a, @JacksonInj
return new FactoryBean711(a, n1, n2);
}
}


static class Value592
{
protected Object stuff;

protected Value592(Object ob, boolean bogus) {
stuff = ob;
}

@JsonCreator
public static Value592 from(TokenBuffer buffer) {
return new Value592(buffer, false);
}
}

/*
/**********************************************************
/* Unit tests
Expand Down Expand Up @@ -103,4 +119,27 @@ public void testWithFactoryAndDelegate() throws Exception
assertEquals("Fygar", bean.name1);
assertEquals("Fygar", bean.name2);
}

// [databind#592]
public void testDelegateWithTokenBuffer() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Value592 value = mapper.readValue("{\"a\":1,\"b\":2}", Value592.class);
assertNotNull(value);
Object ob = value.stuff;
assertEquals(TokenBuffer.class, ob.getClass());
JsonParser jp = ((TokenBuffer) ob).asParser();
assertToken(JsonToken.START_OBJECT, jp.nextToken());
assertToken(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("a", jp.getCurrentName());
assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
assertEquals(1, jp.getIntValue());
assertToken(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("b", jp.getCurrentName());
assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
assertEquals(2, jp.getIntValue());
assertToken(JsonToken.END_OBJECT, jp.nextToken());
jp.close();
}

}

0 comments on commit bcccd3e

Please sign in to comment.