-
-
Notifications
You must be signed in to change notification settings - Fork 260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jsonContext.toJson(o, generator, path) can not apply path on @Transient field that is an entity bean #502
Comments
Hello @rbygrave ? |
Hello
|
Haha, you are funny! @rbygrave This is a bug or issue? |
I had a quick look and it didn't make sense to me. If you want to update it so that it makes more sense I'll have another look. |
on jax-rs , i give a fields params (a,list(oneField)), but can not filter Transient field |
jackson module call |
Or other function filter |
@rbygrave Are you understand ? |
No, this does not make sense. There is no explanation of what 'twoField' is nor "oneField" ... neither are in the model where you are trying to explain the issue. |
o, generator and path ... are also not defined in your example code. |
@Test
public void testTransientPath() throws Throwable {
ModelA modelA = Ebean.find(ModelA.class, 1);
modelA.list = Ebean.createQuery(ModelB.class).select("oneField").findList();
String json = Ebean.json().toJson(modelA, PathProperties.parse("(a,list(oneField))"));
Assert.assertEquals(json, "{\"a\":\"a\",\"list\":[{\"oneField\":1}]}");
}
@Entity
class ModelA {
String a;
@Transient // it's not has mapping, but client need result
List<ModelB> list;
}
@Entity
class ModelB {
Integer oneField;
Integer twoField;
} |
@rbygrave please see test case, understand ? |
Very close. You now just need to state:
|
I want this |
now , |
It would be clear if you state exactly what the value of json is. Specifically the first assertNotEquals() passing is confusing me. |
ok, i change it. are you understand? |
It would be clear if you state exactly what the value of json is. |
json |
What is wrong with that? It is the same as the assertEquals ... so you are saying the assert does not fail? Assert.assertEquals(json, "{\"a\":\"a\",\"list\":[{\"oneField\":1}]}"); |
I get |
import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.EbeanServerFactory;
import com.avaje.ebean.config.DataSourceConfig;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebean.text.PathProperties;
import com.google.common.collect.Lists;
import org.avaje.agentloader.AgentLoader;
import org.h2.Driver;
import org.junit.Assert;
import org.junit.Test;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.io.IOException;
import java.lang.instrument.IllegalClassFormatException;
import java.util.List;
/**
* @author icode
*/
public class EbeanTest {
@Test
public void JsonTest() throws IOException, IllegalClassFormatException, ClassNotFoundException {
AgentLoader.loadAgentFromClasspath("avaje-ebeanorm-agent", "debug=5");
final ServerConfig config = new ServerConfig();
config.setClasses(Lists.<Class<?>>newArrayList(ModelA.class, ModelB.class));
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setUrl("jdbc:h2:file:./db");
dataSourceConfig.setDriver(Driver.class.getName());
dataSourceConfig.setUsername("sa");
dataSourceConfig.setPassword("");
config.setDataSourceConfig(dataSourceConfig);
config.setName("default");
config.setDdlGenerate(true);
config.setDdlRun(true);
config.setDefaultServer(true);
EbeanServer server = EbeanServerFactory.create(config);
ModelA a = new ModelA();
a.setId(1);
a.setA("a");
server.save(a);
ModelB b = new ModelB();
b.setOneField(1);
b.setTwoField(1);
server.save(b);
ModelA sa = server.find(ModelA.class, 1);
sa.setList(server.find(ModelB.class).findList());
String json = Ebean.json().toJson(sa, PathProperties.parse("(a,list(oneField))"));
Assert.assertEquals(json, "{\"a\":\"a\",\"list\":[{\"oneField\":1}]}");
}
@Entity
public static class ModelA {
@Id
int id;
String a;
@Transient // it's not has mapping, but client need result
List<ModelB> list;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public List<ModelB> getList() {
return list;
}
public void setList(List<ModelB> list) {
this.list = list;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@Entity
public static class ModelB {
Integer oneField;
Integer twoField;
public Integer getOneField() {
return oneField;
}
public void setOneField(Integer oneField) {
this.oneField = oneField;
}
public Integer getTwoField() {
return twoField;
}
public void setTwoField(Integer twoField) {
this.twoField = twoField;
}
}
} this is clear test case! thanks test it! |
can fix it? @rbygrave thanks |
Running this test produces: org.junit.ComparisonFailure: Hmm, there is no null there. |
BeanPropertyAssocMany line 891. transient properties written via the Jackson ObjectMapper ... so that means there is for example no check that the target type is actually an entity bean. This is probably the issue. |
…transient field that is an entity bean
Right, the fix was that for transient ToMany properties to also check the targetDescriptor to see if they are entity beans (in which case we can keep processing with the built in JSON processing rather than use Jackson ObjectMapper). |
The text was updated successfully, but these errors were encountered: