Skip to content

Commit 61deaa2

Browse files
Merge pull request #191 from contentstack/fix-refernce-variant-cslp-generation
fix: refernce variant cslp generation fix
2 parents f440877 + 6bb39e9 commit 61deaa2

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed

__test__/entry-editable.test.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,151 @@ describe('Entry editable test', () => {
372372

373373
done()
374374
})
375+
376+
it('Reference fields should not inherit parent variants when they have no applied_variants', done => {
377+
const entryWithReferenceAndVariants = {
378+
"title": "home",
379+
"url": "/data/all_test/first",
380+
"single_line": "ssd",
381+
"tags": ["hi"],
382+
"locale": "en-us",
383+
"uid": "blt827e0ad3608248be",
384+
"created_by": "bltf0d59057590e9b09",
385+
"updated_by": "bltf0d59057590e9b09",
386+
"created_at": "2025-08-25T09:43:49.935Z",
387+
"updated_at": "2025-10-09T11:45:19.967Z",
388+
"ACL": [] as any[],
389+
"_version": 40,
390+
"_in_progress": false,
391+
"json_rte": "<p>hisdassf</p>",
392+
"select": "1",
393+
"group": {
394+
"single_line": ""
395+
},
396+
"non_single_line_textbox": "",
397+
"reference": [
398+
{
399+
"title": "base variant",
400+
"single_line": "bases",
401+
"tags": [] as any[],
402+
"locale": "en-us",
403+
"uid": "blt07a6c7258ddba844",
404+
"created_by": "bltf0d59057590e9b09",
405+
"updated_by": "bltf0d59057590e9b09",
406+
"created_at": "2025-10-01T03:10:10.701Z",
407+
"updated_at": "2025-10-09T11:44:44.981Z",
408+
"_content_type_uid": "all_test_3",
409+
"ACL": [] as any[],
410+
"_version": 3,
411+
"_in_progress": false,
412+
"multi_line_reference": "hii\n"
413+
// Note: This reference object has NO _applied_variants
414+
}
415+
],
416+
"taxonomies": [] as any[],
417+
"multi_line": "woek",
418+
"_applied_variants": {
419+
"single_line": "csfff653e89df54e8c",
420+
"tags": "csfff653e89df54e8c",
421+
"multi_line": "csfff653e89df54e8c"
422+
}
423+
}
424+
425+
addTags(entryWithReferenceAndVariants, 'all_test', false)
426+
427+
// Parent entry fields with variants should get v2 prefix
428+
expect((entryWithReferenceAndVariants as any)['$']['single_line']).toEqual('data-cslp=v2:all_test.blt827e0ad3608248be_csfff653e89df54e8c.en-us.single_line')
429+
expect((entryWithReferenceAndVariants as any)['$']['tags']).toEqual('data-cslp=v2:all_test.blt827e0ad3608248be_csfff653e89df54e8c.en-us.tags')
430+
expect((entryWithReferenceAndVariants as any)['$']['multi_line']).toEqual('data-cslp=v2:all_test.blt827e0ad3608248be_csfff653e89df54e8c.en-us.multi_line')
431+
432+
// Reference fields should NOT get v2 prefix since they have no _applied_variants
433+
expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['title']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.title')
434+
expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['single_line']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.single_line')
435+
expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['tags']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.tags')
436+
expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['multi_line_reference']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.multi_line_reference')
437+
438+
done()
439+
})
440+
441+
it('Reference fields with their own applied_variants should use their variants', done => {
442+
const entryWithReferenceHavingVariants = {
443+
"title": "home",
444+
"locale": "en-us",
445+
"uid": "blt827e0ad3608248be",
446+
"ACL": [] as any[],
447+
"_version": 40,
448+
"_in_progress": false,
449+
"single_line": "parent field",
450+
"reference": [
451+
{
452+
"title": "base variant",
453+
"single_line": "reference field",
454+
"locale": "en-us",
455+
"uid": "blt07a6c7258ddba844",
456+
"_content_type_uid": "all_test_3",
457+
"ACL": [] as any[],
458+
"_version": 3,
459+
"_in_progress": false,
460+
"_applied_variants": {
461+
"single_line": "ref_variant_123"
462+
}
463+
}
464+
],
465+
"_applied_variants": {
466+
"single_line": "parent_variant_456"
467+
}
468+
}
469+
470+
addTags(entryWithReferenceHavingVariants, 'all_test', false)
471+
472+
// Parent entry field should get parent variant
473+
expect((entryWithReferenceHavingVariants as any)['$']['single_line']).toEqual('data-cslp=v2:all_test.blt827e0ad3608248be_parent_variant_456.en-us.single_line')
474+
475+
// Reference field should get its own variant, not parent variant
476+
expect((entryWithReferenceHavingVariants as any)['reference'][0]['$']['title']).toEqual('data-cslp=all_test_3.blt07a6c7258ddba844.en-us.title')
477+
expect((entryWithReferenceHavingVariants as any)['reference'][0]['$']['single_line']).toEqual('data-cslp=v2:all_test_3.blt07a6c7258ddba844_ref_variant_123.en-us.single_line')
478+
479+
done()
480+
})
481+
482+
it('Reference fields should work correctly with tagsAsObject=true', done => {
483+
const entryWithReferenceAndVariants = {
484+
"title": "home",
485+
"single_line": "ssd",
486+
"locale": "en-us",
487+
"uid": "blt827e0ad3608248be",
488+
"ACL": [] as any[],
489+
"_version": 40,
490+
"_in_progress": false,
491+
"reference": [
492+
{
493+
"title": "base variant",
494+
"single_line": "bases",
495+
"locale": "en-us",
496+
"uid": "blt07a6c7258ddba844",
497+
"_content_type_uid": "all_test_3",
498+
"ACL": [] as any[],
499+
"_version": 3,
500+
"_in_progress": false
501+
// No _applied_variants
502+
}
503+
],
504+
"_applied_variants": {
505+
"single_line": "csfff653e89df54e8c"
506+
}
507+
}
508+
509+
addTags(entryWithReferenceAndVariants, 'all_test', true)
510+
511+
// Parent entry field with variant should get v2 prefix as object
512+
expect((entryWithReferenceAndVariants as any)['$']['single_line']).toEqual({'data-cslp': 'v2:all_test.blt827e0ad3608248be_csfff653e89df54e8c.en-us.single_line'})
513+
514+
// Reference fields should NOT get v2 prefix as objects
515+
expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['title']).toEqual({'data-cslp': 'all_test_3.blt07a6c7258ddba844.en-us.title'})
516+
expect((entryWithReferenceAndVariants as any)['reference'][0]['$']['single_line']).toEqual({'data-cslp': 'all_test_3.blt07a6c7258ddba844.en-us.single_line'})
517+
518+
done()
519+
})
375520
})
376521

377522
})

src/entry-editable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function getTag(content: object, prefix: string, tagsAsObject: boolean, locale:
6262
* }]
6363
* }
6464
*/
65-
const newAppliedVariants = obj._applied_variants || obj?.system?.applied_variants || _applied_variants;
65+
const newAppliedVariants = obj._applied_variants || obj?.system?.applied_variants || null //check for _applied_variants in the reference object only return null if not present , do not check in the parent object;
6666
const newShouldApplyVariant = !!newAppliedVariants
6767
value[index].$ = getTag(obj, `${obj._content_type_uid}.${obj.uid}.${obj.locale || locale}`, tagsAsObject, locale, { _applied_variants: newAppliedVariants, shouldApplyVariant: newShouldApplyVariant, metaKey: "" })
6868
} else if (typeof obj === "object") {

0 commit comments

Comments
 (0)