Skip to content
This repository was archived by the owner on Jan 5, 2021. It is now read-only.

Commit 26a2a5a

Browse files
authored
Merge pull request #1 from swissredcross/fix/circular
Fix for circular data structures
2 parents a9de704 + c1a44fe commit 26a2a5a

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/Contentfully.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export class Contentfully {
142142
return links;
143143
}
144144

145-
private _dereferenceLink(reference: any, links: any) {
145+
private _dereferenceLink(reference: any, links: any, level: number=0) {
146146

147147
const sys = reference.sys;
148148
const modelId = sys.id;
@@ -163,7 +163,7 @@ export class Contentfully {
163163
link._type = deferredSys.contentType.sys.id;
164164

165165
// update entry with parsed value
166-
assign(link, this._parseEntry(link._deferred, links));
166+
assign(link, this._parseEntry(link._deferred, links, level));
167167

168168
// prune deferral
169169
delete link._deferred;
@@ -174,7 +174,6 @@ export class Contentfully {
174174
}
175175

176176
private _parseEntries(entries: any, links: any) {
177-
178177
// convert entries to models and return result
179178
return map(entries, entry => {
180179
// process entry if not processed
@@ -203,30 +202,36 @@ export class Contentfully {
203202
});
204203
}
205204

206-
private _parseEntry(entry: any, links: any) {
205+
private _parseEntry(entry: any, links: any, level: number=0 ) {
206+
level+=1;
207+
// This Value has to be somewhat small, as it will otherwise 'detect' circular structures when there are none
208+
//TODO: Make this configurable in the request (possibly by using the include property of the request)
209+
if(level>=10)
210+
return
211+
207212

208213
// transform entry to model and return result
209214
forEach(entry.fields, (value, key) => {
210215
// parse array of values
211216
if (isArray(value)) {
212-
entry.fields[key] = compact(map(value, item => this._parseValue(item, links)));
217+
entry.fields[key] = compact(map(value, item => this._parseValue(item, links, level)));
213218
}
214219

215220
// or parse value
216221
else {
217222
// handle null values otherwise pass back the values
218-
if(this._parseValue(value, links) === undefined) {
223+
if(this._parseValue(value, links, level) === undefined) {
219224
unset(entry.fields, key)
220225
} else {
221-
entry.fields[key] = this._parseValue(value, links);
226+
entry.fields[key] = this._parseValue(value, links, level);
222227
}
223228
}
224229
});
225230

226231
return entry.fields;
227232
}
228233

229-
private _parseValue(value: any, links: any) {
234+
private _parseValue(value: any, links: any, level: number=0) {
230235

231236
// handle values without a link
232237
const sys = value.sys;
@@ -235,6 +240,6 @@ export class Contentfully {
235240
}
236241

237242
// dereference link
238-
return this._dereferenceLink(value, links);
243+
return this._dereferenceLink(value, links, level);
239244
}
240245
}

0 commit comments

Comments
 (0)