Skip to content

Commit 4034689

Browse files
authoredMar 12, 2025··
fix(studio): handle document-level copy/paste reference mismatches (#8895)
1 parent a4f4af3 commit 4034689

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed
 

‎packages/sanity/src/core/studio/copyPaste/__test__/transferValue.test.ts

+48
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,54 @@ describe('transferValue', () => {
526526
])
527527
expect(transferValueResult?.targetValue).toEqual(undefined)
528528
})
529+
530+
test('will properly preserve _weak and _strengthenOnPublish when copying a whole document with references', async () => {
531+
const sourceValue = {
532+
_type: 'referencesDocument',
533+
_id: 'doc1',
534+
reference: {
535+
_type: 'reference',
536+
_ref: 'yyy',
537+
_weak: true,
538+
_strengthenOnPublish: {
539+
type: 'editor',
540+
},
541+
},
542+
}
543+
const targetRootValue = {
544+
_type: 'referencesDocument',
545+
_id: 'doc2',
546+
}
547+
548+
const transferValueResult = await transferValue({
549+
sourceRootSchemaType: schema.get('referencesDocument')!,
550+
sourcePath: [],
551+
sourceValue,
552+
targetDocumentSchemaType: schema.get('referencesDocument')!,
553+
targetRootSchemaType: schema.get('referencesDocument')!,
554+
targetPath: [],
555+
targetRootValue,
556+
targetRootPath: [],
557+
currentUser,
558+
options: {
559+
validateReferences: true,
560+
client: createMockClient([{_type: 'editor', _id: 'yyy', name: 'John Doe'}]),
561+
},
562+
})
563+
564+
expect(transferValueResult?.errors).toEqual([])
565+
expect(transferValueResult?.targetValue).toMatchObject({
566+
_type: 'referencesDocument',
567+
reference: {
568+
_type: 'reference',
569+
_ref: 'yyy',
570+
_weak: true,
571+
_strengthenOnPublish: {
572+
type: 'editor',
573+
},
574+
},
575+
})
576+
})
529577
})
530578

531579
describe('booleans', () => {

‎packages/sanity/src/core/studio/copyPaste/transferValue.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -794,9 +794,13 @@ async function collateObjectValue({
794794
})
795795
}
796796

797-
// Special handling for weak references
798-
if (isReferenceSchemaType(targetSchemaType) && targetSchemaType.weak) {
799-
resultingValue._weak = true
797+
// Special handling to avoid reference strength mismatches
798+
if (isReferenceSchemaType(targetSchemaType)) {
799+
if (targetSchemaType.weak) {
800+
resultingValue._weak = true
801+
} else {
802+
delete resultingValue._weak
803+
}
800804
}
801805

802806
// Special handling for weak references that will be strengthened on publish
@@ -1034,7 +1038,7 @@ function collatePrimitiveValue({
10341038
}
10351039

10361040
function cleanObjectKeys(obj: TypedObject): TypedObject {
1037-
const disallowedKeys = ['_id', '_createdAt', '_updatedAt', '_rev', '_weak']
1041+
const disallowedKeys = ['_id', '_createdAt', '_updatedAt', '_rev']
10381042
return Object.keys(obj).reduce((acc, key) => {
10391043
if (disallowedKeys.includes(key)) {
10401044
return acc

0 commit comments

Comments
 (0)
Please sign in to comment.