Skip to content
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

Fixed attaching a self-referenced entity to an entity manager #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/mapping-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataServiceAdapter } from './interface-registry';
import { DataServiceAdapter } from './interface-registry';
import { core } from './core';
import { DataType } from './data-type';
import { DataService, JsonResultsAdapter, NodeContext, NodeMeta } from './data-service';
Expand Down Expand Up @@ -304,8 +304,8 @@ function mergeEntity(mc: MappingContext, node: any, meta: NodeMeta) {
if (meta.extraMetadata) {
targetEntity.entityAspect.extraMetadata = meta.extraMetadata;
}
// em._attachEntityCore(targetEntity, EntityState.Unchanged, MergeStrategy.Disallowed);
em._attachEntityCore(targetEntity, EntityState.Unchanged, mergeStrategy);
// return value === targetEntity EXCEPT in the case of a merge with a self reference.
targetEntity = em._attachEntityCore(targetEntity, EntityState.Unchanged, mergeStrategy);
targetEntity.entityAspect.wasLoaded = true;
em.entityChanged.publish({ entityAction: EntityAction.AttachOnQuery, entity: targetEntity });
}
Expand Down Expand Up @@ -395,6 +395,13 @@ function mergeRelatedEntityCore(mc: MappingContext, rawEntity: any, navigationPr
let relatedRawEntity = rawEntity[navigationProperty.nameOnServer];
if (!relatedRawEntity) return null;

if (rawEntity.$type === relatedRawEntity.$type) {
if (navigationProperty.parentType.keyProperties.every(kp => rawEntity[kp.nameOnServer] === relatedRawEntity[kp.nameOnServer])) {
//the related entity is a duplicate of the parent entity, ignore it
return null;
}
}

let relatedEntity = mc.visitAndMerge(relatedRawEntity, { nodeType: "navProp", navigationProperty: navigationProperty });
return relatedEntity;
}
Expand Down