@@ -67,8 +67,12 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
6767 } ,
6868
6969 $get : [ "$injector" , function ( $injector ) {
70- // map contain self link as key and a list of linkNames which were already fetched
71- var map = { } ;
70+ /**
71+ * Link map which contains the 'self' link as key and the list of already fetched link names as value.
72+ * This is used to check that every link on each entity is only fetched once to avoid an infinite recursive loop.
73+ * @type {{Object} }
74+ */
75+ var linkMap = { } ;
7276
7377 /**
7478 * Returns the Angular $resource method which is configured with the given parameters.
@@ -252,9 +256,11 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
252256
253257 // if there are links to fetch, then process and fetch them
254258 if ( fetchLinkNames != undefined ) {
255- var self = data . _links . self . href ;
256- if ( ! map [ self ] ) {
257- map [ self ] = [ ] ;
259+ var self = data [ config . linksKey ] [ config . linksSelfLinkName ] [ config . linksHrefKey ] ;
260+
261+ // add the self link value as key and add an empty map to store all other links which are fetched for this entity
262+ if ( ! linkMap [ self ] ) {
263+ linkMap [ self ] = [ ] ;
258264 }
259265
260266 // process all links
@@ -264,16 +270,17 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
264270 if ( linkName != config . linksSelfLinkName ) {
265271
266272 // check if:
267- // 1. the all link names key is given then fetch the link
268- // 2. the given key is equal
269- // 3. the given key is inside the array
270- if ( map [ self ] . indexOf ( linkName ) < 0 &&
273+ // 1. the link was not fetched already
274+ // 2. the all link names key is given then fetch the link
275+ // 3. the given key is equal
276+ // 4. the given key is inside the array
277+ if ( linkMap [ self ] . indexOf ( linkName ) < 0 &&
271278 ( fetchLinkNames == config . fetchAllKey ||
272279 ( typeof fetchLinkNames === "string" && linkName == fetchLinkNames ) ||
273280 ( fetchLinkNames instanceof Array && fetchLinkNames . indexOf ( linkName ) >= 0 ) ) ) {
274281 promisesArray . push ( fetchFunction ( getProcessedUrl ( data , linkName ) , linkName ,
275282 processedData , fetchLinkNames , recursive ) ) ;
276- map [ self ] . push ( linkName ) ;
283+ linkMap [ self ] . push ( linkName ) ;
277284 }
278285 }
279286 } ) ;
@@ -368,8 +375,14 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
368375 }
369376 } ;
370377
378+ // empty the map and
371379 // return an object with the processData function
372- return { process : processData } ;
380+ return {
381+ process : function ( promiseOrData , fetchLinkNames , recursive ) {
382+ linkMap = { } ;
383+ return processData ( promiseOrData , fetchLinkNames , recursive ) ;
384+ }
385+ } ;
373386 } ]
374387 } ;
375388
0 commit comments