From 59e8a373cea3f911a1bb7542a1cc7027fd8482d2 Mon Sep 17 00:00:00 2001 From: Martin Klemsa Date: Fri, 15 Nov 2024 13:00:28 +0100 Subject: [PATCH 1/2] Fixed attaching a self-referenced entity to an entity manager --- src/mapping-context.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mapping-context.ts b/src/mapping-context.ts index a347c25..5d75d63 100644 --- a/src/mapping-context.ts +++ b/src/mapping-context.ts @@ -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'; @@ -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 }); } From c52bb38bcdbecdc95f19abf07f303fbef858e6a2 Mon Sep 17 00:00:00 2001 From: Martin Klemsa Date: Fri, 20 Dec 2024 11:05:13 +0100 Subject: [PATCH 2/2] Fixed issue with loading mavigation properties --- src/mapping-context.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/mapping-context.ts b/src/mapping-context.ts index 5d75d63..9777100 100644 --- a/src/mapping-context.ts +++ b/src/mapping-context.ts @@ -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; }