Skip to content
This repository has been archived by the owner on Jun 26, 2021. It is now read-only.

Commit

Permalink
Id field was not parsed when placed before the type field.
Browse files Browse the repository at this point in the history
This fix makes sure the id field is parsed when present before the type field by using the buffer parser.
  • Loading branch information
ghillairet committed Dec 19, 2015
1 parent 5c53faf commit 9809683
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 11 deletions.
Expand Up @@ -114,7 +114,11 @@ protected EObject doDeserialize(JsonParser jp, EClass defaultType, Deserializati

} else if (options.idField.equalsIgnoreCase(fieldName)) {

options.idDeserializer.deserialize(jp, current, ctxt);
if (current != null) {
options.idDeserializer.deserialize(jp, current, ctxt);
} else {
buffer.copyCurrentStructure(jp);
}

} else {

Expand Down Expand Up @@ -145,7 +149,11 @@ protected EObject postDeserialize(TokenBuffer buffer, EObject object, EClass def
final JsonParser bufferedParser = buffer.asParser();

while (bufferedParser.nextToken() != null) {
readFeature(bufferedParser, object, bufferedParser.getCurrentName(), ctxt, resource, entries);
if (options.idField.equals(bufferedParser.getCurrentName())) {
options.idDeserializer.deserialize(bufferedParser, object, ctxt);
} else {
readFeature(bufferedParser, object, bufferedParser.getCurrentName(), ctxt, resource, entries);
}
}

bufferedParser.close();
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/emfjson/jackson/junit/TestSuite.java
Expand Up @@ -28,12 +28,13 @@
EnumTest.class,
ExternalReferencesTest.class,
FeatureMapTest.class,
GenericTest.class,
IdTest.class,
JacksonOptionTest.class,
MapTest.class,
ModuleTest.class,
NoTypeTest.class,
PolymorphicTest.class,
GenericTest.class,
ProxyAttributeTest.class,
ReaderTest.class,
ReferenceTest.class,
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/org/emfjson/jackson/junit/tests/IdTest.java
@@ -0,0 +1,63 @@
package org.emfjson.jackson.junit.tests;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import org.eclipse.emf.ecore.resource.Resource;
import org.emfjson.jackson.JacksonOptions;
import org.emfjson.jackson.junit.model.ModelFactory;
import org.emfjson.jackson.junit.model.User;
import org.emfjson.jackson.junit.support.TestSupport;
import org.emfjson.jackson.resource.JsonResource;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class IdTest extends TestSupport {

final JacksonOptions options = new JacksonOptions.Builder()
.withID(true)
.build();

@Test
public void testWriteId() {
JsonNode expected = mapper.createObjectNode()
.put("eClass", "http://www.emfjson.org/jackson/model#//User")
.put("_id", "1")
.put("name", "Joe");

JsonResource resource = new JsonResource();
User user = ModelFactory.eINSTANCE.createUser();
user.setName("Joe");
resource.setID(user, "1");
resource.getContents().add(user);

assertEquals(expected, mapper(options).valueToTree(resource));
}

@Test
public void testReadId() throws JsonProcessingException {
JsonNode data = mapper.createObjectNode()
.put("eClass", "http://www.emfjson.org/jackson/model#//User")
.put("_id", "1")
.put("name", "Joe");

JsonResource resource = (JsonResource) mapper(options).treeToValue(data, Resource.class);
User user = (User) resource.getContents().get(0);

assertEquals("1", resource.getID(user));
}

@Test
public void testReadId_WhenIdBeforeTypeField() throws JsonProcessingException {
JsonNode data = mapper.createObjectNode()
.put("_id", "1")
.put("eClass", "http://www.emfjson.org/jackson/model#//User")
.put("name", "Joe");

JsonResource resource = (JsonResource) mapper(options).treeToValue(data, Resource.class);
User user = (User) resource.getContents().get(0);

assertEquals("1", resource.getID(user));
}

}
11 changes: 3 additions & 8 deletions src/test/resources/model/model.xcore
Expand Up @@ -14,6 +14,7 @@ import org.eclipse.emf.ecore.EStringToStringMapEntry
import java.util.Map

annotation "JSON" as JSON @JSON(root="true")

class User {
id String userId
String name
Expand Down Expand Up @@ -133,20 +134,14 @@ class Node {
contains resolving Node uniqueChild
}

class ObjectWithMap {
@JSON(dynamicMap="true")
contains resolving EStringToStringMapEntry[] entries
contains resolving EStringToStringMapEntry[] dependencies
}

enum SomeKind {
one = 0
Two as "two" = 1
Three as "Three-is-Three" = 3
}

class GenericContainer {
contains resolving GenericType < ? >[] values
contains resolving GenericType<?>[] values
}

abstract class GenericType<T> {
Expand All @@ -155,4 +150,4 @@ abstract class GenericType<T> {

class SpecialTypeOne extends GenericType<String> {}

class SpecialTypeTwo extends GenericType<Boolean> {}
class SpecialTypeTwo extends GenericType<Boolean> {}

0 comments on commit 9809683

Please sign in to comment.