Skip to content

Commit 46ec4db

Browse files
fix: guard against null/undefined in getTag function
- Add null check at the top of getTag to return empty object early, preventing TypeError when Object.entries receives null or undefined (typeof null === "object" in JS, so null slipped through the switch) - Add test cases covering null/undefined field values, null/undefined entries, multiple null fields, and nested null objects (Issue #193) Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 1d60d75 commit 46ec4db

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

__test__/entry-editable.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,77 @@ describe('Entry editable test', () => {
519519
})
520520
})
521521

522+
describe('Null and undefined content handling (Issue #193)', () => {
523+
it('should not throw when entry has a null field value', done => {
524+
const entryWithNullField: any = {
525+
"uid": "entry_uid_null",
526+
"locale": "en-us",
527+
"title": "Valid title",
528+
"description": null
529+
}
530+
531+
expect(() => addTags(entryWithNullField, 'content_type', false)).not.toThrow()
532+
expect((entryWithNullField as any)['$']['title']).toEqual('data-cslp=content_type.entry_uid_null.en-us.title')
533+
done()
534+
})
535+
536+
it('should not throw when entry has an undefined field value', done => {
537+
const entryWithUndefinedField: any = {
538+
"uid": "entry_uid_undef",
539+
"locale": "en-us",
540+
"title": "Valid title",
541+
"description": undefined
542+
}
543+
544+
expect(() => addTags(entryWithUndefinedField, 'content_type', false)).not.toThrow()
545+
expect((entryWithUndefinedField as any)['$']['title']).toEqual('data-cslp=content_type.entry_uid_undef.en-us.title')
546+
done()
547+
})
548+
549+
it('should return empty tags for a null entry', done => {
550+
const nullEntry: any = null
551+
552+
expect(() => addTags(nullEntry, 'content_type', false)).not.toThrow()
553+
done()
554+
})
555+
556+
it('should return empty tags for an undefined entry', done => {
557+
const undefinedEntry: any = undefined
558+
559+
expect(() => addTags(undefinedEntry, 'content_type', false)).not.toThrow()
560+
done()
561+
})
562+
563+
it('should handle entry with multiple null field values', done => {
564+
const entryWithMultipleNulls: any = {
565+
"uid": "entry_uid_multi_null",
566+
"locale": "en-us",
567+
"title": "Valid title",
568+
"field_a": null,
569+
"field_b": null,
570+
"field_c": "valid"
571+
}
572+
573+
expect(() => addTags(entryWithMultipleNulls, 'content_type', true)).not.toThrow()
574+
expect((entryWithMultipleNulls as any)['$']['title']).toEqual({'data-cslp': 'content_type.entry_uid_multi_null.en-us.title'})
575+
expect((entryWithMultipleNulls as any)['$']['field_c']).toEqual({'data-cslp': 'content_type.entry_uid_multi_null.en-us.field_c'})
576+
done()
577+
})
578+
579+
it('should handle entry with nested null object', done => {
580+
const entryWithNestedNull: any = {
581+
"uid": "entry_uid_nested_null",
582+
"locale": "en-us",
583+
"title": "Valid title",
584+
"group": null
585+
}
586+
587+
expect(() => addTags(entryWithNestedNull, 'content_type', false)).not.toThrow()
588+
expect((entryWithNestedNull as any)['$']['title']).toEqual('data-cslp=content_type.entry_uid_nested_null.en-us.title')
589+
done()
590+
})
591+
})
592+
522593
describe('useLowerCaseLocale option', () => {
523594
it('should preserve locale casing when useLowerCaseLocale is false', done => {
524595
const entry = {

src/entry-editable.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export function addTags(entry: EntryModel, contentTypeUid: string, tagsAsObject:
3434
}
3535

3636
function getTag(content: object, prefix: string, tagsAsObject: boolean, locale: string, appliedVariants: AppliedVariants): object {
37+
if (content == null) {
38+
return {}
39+
}
3740
const tags: any = {}
3841
const { metaKey, shouldApplyVariant, _applied_variants } = appliedVariants
3942
Object.entries(content).forEach(([key, value]) => {

0 commit comments

Comments
 (0)