Skip to content

Commit

Permalink
CastableInformation does not support interface types #53
Browse files Browse the repository at this point in the history
  • Loading branch information
remmeier committed Jun 21, 2019
1 parent 7743f41 commit 472c278
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 238 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.crnk.client.response;

import java.io.IOException;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import io.crnk.core.engine.internal.utils.CastableInformation;
import io.crnk.core.resource.links.LinksInformation;

import java.io.IOException;

public class JsonLinksInformation implements LinksInformation, CastableInformation<LinksInformation> {

private JsonNode data;
Expand All @@ -32,9 +32,13 @@ public JsonNode asJsonNode() {
@Override
public <L extends LinksInformation> L as(Class<L> linksClass) {
try {
if (linksClass.isInterface()) {
return JsonMetaInformation.createInterfaceJsonAdapter(linksClass, data, mapper);
}
ObjectReader reader = mapper.readerFor(linksClass);
return reader.readValue(data);
} catch (IOException e) {
}
catch (IOException e) {
throw new IllegalStateException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package io.crnk.client.response;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import io.crnk.core.engine.internal.utils.CastableInformation;
import io.crnk.core.engine.internal.utils.ClassUtils;
import io.crnk.core.resource.meta.MetaInformation;

import java.io.IOException;

public class JsonMetaInformation implements MetaInformation, CastableInformation<MetaInformation> {

private JsonNode data;
Expand All @@ -31,9 +36,34 @@ public JsonNode asJsonNode() {
@Override
public <M extends MetaInformation> M as(Class<M> metaClass) {
try {
if (metaClass.isInterface()) {
return createInterfaceJsonAdapter(metaClass, data, mapper);
}
return mapper.readerFor(metaClass).readValue(data);
} catch (IOException e) {
}
catch (IOException e) {
throw new IllegalStateException(e);
}
}

protected static <T> T createInterfaceJsonAdapter(Class<T> interfaceClass, JsonNode data, final ObjectMapper mapper) {
Class[] interfaces = new Class[] { interfaceClass };
return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getDeclaringClass().equals(Object.class)) {
throw new UnsupportedOperationException("not implemented");
}

String name = ClassUtils.getGetterFieldName(method);
JsonNode jsonNode = data.get(name);
if(jsonNode != null) {
Class<?> returnType = method.getReturnType();
ObjectReader objectReader = mapper.readerFor(returnType);
return objectReader.readValue(jsonNode);
}
return null;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package io.crnk.client.response;

import java.io.IOException;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.crnk.core.resource.links.LinksInformation;
import io.crnk.test.mock.models.Task;
import io.crnk.test.mock.repository.ScheduleRepository;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class JsonLinksInformationTest {

private ObjectMapper mapper;

private JsonNode node;

@Before
Expand All @@ -35,6 +37,20 @@ public void testParse() {
Assert.assertEquals("test", meta.value);
}

@Test
public void testInterfaceProxy() {
JsonLinksInformation info = new JsonLinksInformation(node, mapper);

LinksInterface meta = info.as(LinksInterface.class);
Assert.assertEquals("test", meta.getValue());
}

interface LinksInterface extends LinksInformation {

String getValue();
}


@Test(expected = IllegalStateException.class)
public void testParseException() {
JsonLinksInformation info = new JsonLinksInformation(node, mapper);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package io.crnk.client.response;

import java.io.IOException;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.crnk.core.resource.meta.MetaInformation;
import io.crnk.test.mock.models.Task;
import io.crnk.test.mock.repository.ScheduleRepository;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class JsonMetaInformationTest {

private ObjectMapper mapper;

private JsonNode node;

@Before
Expand All @@ -35,6 +37,20 @@ public void testParse() {
Assert.assertEquals("test", meta.value);
}

@Test
public void testInterfaceProxy() {
JsonMetaInformation info = new JsonMetaInformation(node, mapper);

MetaInterface meta = info.as(MetaInterface.class);
Assert.assertEquals("test", meta.getValue());
}

interface MetaInterface extends MetaInformation {

String getValue();
}


@Test(expected = IllegalStateException.class)
public void testParseException() {
JsonMetaInformation info = new JsonMetaInformation(node, mapper);
Expand Down
Loading

0 comments on commit 472c278

Please sign in to comment.