Program: 59578
Project: 59578
Buggy source:
protected OEntity toOEntity(EdmEntitySet ees,Object obj,PropertyPathHelper pathHelper){
  InMemoryEntityInfo<?> ei=this.findEntityInfoForClass(obj.getClass());
  final List<OLink> links=new ArrayList<OLink>();
  final List<OProperty<?>> properties=new ArrayList<OProperty<?>>();
  Map<String,Object> keyKVPair=new HashMap<String,Object>();
  for (  String key : ei.getKeys()) {
    Object keyValue=ei.getPropertyModel().getPropertyValue(obj,key);
    keyKVPair.put(key,keyValue);
  }
  addPropertiesFromObject(obj,ei.getPropertyModel(),ees.getType(),properties,pathHelper);
  EdmEntityType edmEntityType=ees.getType();
  for (  final EdmNavigationProperty navProp : ees.getType().getNavigationProperties()) {
    if (!pathHelper.isSelected(navProp.getName())) {
      continue;
    }
    if (!pathHelper.isExpanded(navProp.getName())) {
      if (navProp.getToRole().getMultiplicity() == EdmMultiplicity.MANY) {
        links.add(OLinks.relatedEntities(null,navProp.getName(),null));
      }
 else {
        links.add(OLinks.relatedEntity(null,navProp.getName(),null));
      }
    }
 else {
      pathHelper.navigate(navProp.getName());
      if (navProp.getToRole().getMultiplicity() == EdmMultiplicity.MANY) {
        List<OEntity> relatedEntities=new ArrayList<OEntity>();
        EdmEntitySet relEntitySet=null;
        for (        final Object entity : getRelatedPojos(navProp,obj,ei)) {
          if (relEntitySet == null) {
            InMemoryEntityInfo<?> oei=this.findEntityInfoForClass(entity.getClass());
            relEntitySet=getMetadata().getEdmEntitySet(oei.getEntitySetName());
          }
          relatedEntities.add(toOEntity(relEntitySet,entity,pathHelper));
        }
        links.add(OLinks.relatedEntitiesInline(null,navProp.getName(),null,relatedEntities));
      }
 else {
        final Object entity=ei.getPropertyModel().getPropertyValue(obj,navProp.getName());
        OEntity relatedEntity=null;
        if (entity != null) {
          InMemoryEntityInfo<?> oei=this.findEntityInfoForClass(entity.getClass());
          EdmEntitySet relEntitySet=getMetadata().getEdmEntitySet(oei.getEntitySetName());
          relatedEntity=toOEntity(relEntitySet,entity,pathHelper);
        }
        links.add(OLinks.relatedEntityInline(null,navProp.getName(),null,relatedEntity));
      }
      pathHelper.popPath();
    }
  }
  return OEntities.create(ees,OEntityKey.create(keyKVPair),properties,links,obj);
}

Fixed version (ground truth):
protected OEntity toOEntity(EdmEntitySet ees,Object obj,PropertyPathHelper pathHelper){
  InMemoryEntityInfo<?> ei=this.findEntityInfoForClass(obj.getClass());
  final List<OLink> links=new ArrayList<OLink>();
  final List<OProperty<?>> properties=new ArrayList<OProperty<?>>();
  Map<String,Object> keyKVPair=new HashMap<String,Object>();
  for (  String key : ei.getKeys()) {
    Object keyValue=ei.getPropertyModel().getPropertyValue(obj,key);
    keyKVPair.put(key,keyValue);
  }
  EdmEntityType edmEntityType=(EdmEntityType)this.getMetadata().findEdmEntityType(namespace + "String_Node_Str" + ei.getEntityTypeName());
  addPropertiesFromObject(obj,ei.getPropertyModel(),edmEntityType,properties,pathHelper);
  for (  final EdmNavigationProperty navProp : edmEntityType.getNavigationProperties()) {
    if (!pathHelper.isSelected(navProp.getName())) {
      continue;
    }
    if (!pathHelper.isExpanded(navProp.getName())) {
      if (navProp.getToRole().getMultiplicity() == EdmMultiplicity.MANY) {
        links.add(OLinks.relatedEntities(null,navProp.getName(),null));
      }
 else {
        links.add(OLinks.relatedEntity(null,navProp.getName(),null));
      }
    }
 else {
      pathHelper.navigate(navProp.getName());
      if (navProp.getToRole().getMultiplicity() == EdmMultiplicity.MANY) {
        List<OEntity> relatedEntities=new ArrayList<OEntity>();
        EdmEntitySet relEntitySet=null;
        for (        final Object entity : getRelatedPojos(navProp,obj,ei)) {
          if (relEntitySet == null) {
            InMemoryEntityInfo<?> oei=this.findEntityInfoForClass(entity.getClass());
            relEntitySet=getMetadata().getEdmEntitySet(oei.getEntitySetName());
          }
          relatedEntities.add(toOEntity(relEntitySet,entity,pathHelper));
        }
        links.add(OLinks.relatedEntitiesInline(null,navProp.getName(),null,relatedEntities));
      }
 else {
        final Object entity=ei.getPropertyModel().getPropertyValue(obj,navProp.getName());
        OEntity relatedEntity=null;
        if (entity != null) {
          InMemoryEntityInfo<?> oei=this.findEntityInfoForClass(entity.getClass());
          EdmEntitySet relEntitySet=getMetadata().getEdmEntitySet(oei.getEntitySetName());
          relatedEntity=toOEntity(relEntitySet,entity,pathHelper);
        }
        links.add(OLinks.relatedEntityInline(null,navProp.getName(),null,relatedEntity));
      }
      pathHelper.popPath();
    }
  }
  return OEntities.create(ees,edmEntityType,OEntityKey.create(keyKVPair),properties,links,obj);
}