FIX: Json deserialize references

This commit is contained in:
Jonas Pöhler authored and Noemi Szemenyei committed 2021-11-16 11:57:30 +01:00
1 parent 5807874c9e
commit d3e07defa0
3 files changed
+15 -7

No files matched your search

@@ -3038,6 +3038,10 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType, SpiBeanType {
return ebi.isReference() || referenceIdPropertyOnly(ebi); return ebi.isReference() || referenceIdPropertyOnly(ebi);
} }
boolean hasIdPropertyOnly(EntityBeanIntercept ebi) {
return propertiesBaseScalar.length > 0 && ebi.hasIdOnly(idPropertyIndex);
}
public boolean referenceIdPropertyOnly(EntityBeanIntercept ebi) { public boolean referenceIdPropertyOnly(EntityBeanIntercept ebi) {
return idOnlyReference && ebi.hasIdOnly(idPropertyIndex); return idOnlyReference && ebi.hasIdOnly(idPropertyIndex);
} }
@@ -3055,6 +3059,12 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType, SpiBeanType {
return versionPropertyIndex > -1 && ebi.isLoadedProperty(versionPropertyIndex); return versionPropertyIndex > -1 && ebi.isLoadedProperty(versionPropertyIndex);
} }
void setReferenceIfIdOnly(EntityBeanIntercept ebi) {
if (hasIdPropertyOnly(ebi)) {
ebi.setReference(idPropertyIndex);
}
}
/** /**
* Set the version value returning it in primitive long form. * Set the version value returning it in primitive long form.
*/ */
@@ -152,6 +152,9 @@ final class BeanDescriptorJsonHelp<T> {
} }
if (contextBean == null) { if (contextBean == null) {
readJson.beanVisitor(bean, unmappedProperties); readJson.beanVisitor(bean, unmappedProperties);
if (!isNullOrZero(id)) {
desc.setReferenceIfIdOnly(bean._ebean_getIntercept());
}
} }
if (path != null) { if (path != null) {
readJson.popPath(); readJson.popPath();
@@ -47,15 +47,10 @@ public class TestTextJsonReferenceBean extends BaseTestCase {
prodDesc.isReference(eb._ebean_getIntercept()); prodDesc.isReference(eb._ebean_getIntercept());
BeanState beanState = DB.beanState(refProd); BeanState beanState = DB.beanState(refProd);
assertTrue(beanState.isNew()); assertTrue(beanState.isReference());
String name = refProd.getName(); String name = refProd.getName();
assertNull(name); assertNotNull(name);
// Set to be 'loaded' to invoke lazy loading
beanState.setLoaded();
String name2 = refProd.getName();
assertNotNull(name2);
} }
List<Order> orders = DB.find(Order.class) List<Order> orders = DB.find(Order.class)