diff --git a/src/application/environment/environmentCompareIds.ts b/src/application/environment/environmentCompareIds.ts index 67df853f35f..628dbb908b5 100644 --- a/src/application/environment/environmentCompareIds.ts +++ b/src/application/environment/environmentCompareIds.ts @@ -297,22 +297,6 @@ interface LearnContext { readonly identity: boolean; } -/** Two values under one key as a pair of ids, when they are: an identifier field, or outside one only a string the state holds as an id, since a counter that equals an id is not one. */ -const idPair = ( - key: string, - value: JsonValue, - other: JsonValue | undefined, - heldIds: ReadonlySet, -): readonly [string | number, string | number] | undefined => - isScalarId(value) && - isScalarId(other) && - (ID_FIELD.test(key) || - (typeof value === "string" && - typeof other === "string" && - heldIds.has(other))) - ? [value, other] - : undefined; - const childPointer = (at: string, key: string | number): string => `${at}/${escapeJsonPointerToken(String(key))}`; @@ -353,23 +337,29 @@ const learnAt = ( for (const [key, value] of Object.entries(live)) { if (!Object.hasOwn(env, key)) continue; const other = env[key]; - const pair = idPair(key, value, other, context.heldIds); - if (pair !== undefined) { - const stateReferenceField = - !ID_FIELD.test(key) && - typeof value === "string" && - typeof other === "string" && - context.heldIds.has(other); - context.map.learn( - ...pair, - own && IDENTITY_FIELD.test(key) ? context.scope : undefined, - stateReferenceField ? key : undefined, - ); - } else - learnAt(value, other, context, { - at: childPointer(place.at, key), - own: false, - }); + // Outside identifier fields, only strings held as ids can teach a + // mapping. A numeric counter that equals an id must stay literal. + if (isScalarId(value) && isScalarId(other)) { + const identifierField = ID_FIELD.test(key); + if ( + identifierField || + (typeof value === "string" && + typeof other === "string" && + context.heldIds.has(other)) + ) { + context.map.learn( + value, + other, + own && IDENTITY_FIELD.test(key) ? context.scope : undefined, + identifierField ? undefined : key, + ); + continue; + } + } + learnAt(value, other, context, { + at: childPointer(place.at, key), + own: false, + }); } };