Skip to content

Commit 517e977

Browse files
author
guylabs
committed
#8 - Fix the issue that the returned response of the fetch function is not processed again
1 parent 44d5cf5 commit 517e977

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

dist/angular-spring-data-rest.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
103103
promisesArray.push($injector.get("$http").get(url)
104104
.then(function (responseData) {
105105

106-
// wrap the response again with the adapter if the recursive flag is set
106+
// wrap the response again with the adapter and return the promise
107107
if (recursive) {
108-
promisesArray.push(processData(responseData.data, fetchLinkNames, true).then(function (processedData) {
108+
return processData(responseData.data, fetchLinkNames, true).then(function (processedData) {
109109
data[key] = processedData;
110-
}));
110+
});
111111
} else {
112-
data[key] = responseData.data;
112+
return processData(responseData.data).then(function (processedData) {
113+
data[key] = processedData;
114+
});
113115
}
114116
}, function (error) {
115117
if (error.status != 404) {

dist/angular-spring-data-rest.min.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@
7979
if (void 0 == e.fetchFunction) {
8080
var h = [];
8181
return h.push(a.get("$http").get(b).then(function (a) {
82-
g ? h.push(i(a.data, f, !0).then(function (a) {
82+
return g ? i(a.data, f, !0).then(function (a) {
8383
d[c] = a
84-
})) : d[c] = a.data
84+
}) : i(a.data).then(function (a) {
85+
d[c] = a
86+
})
8587
}, function (b) {
8688
return 404 != b.status ? a.get("$q").reject(b) : void 0
8789
})), a.get("$q").all(h)

src/angular-spring-data-rest-provider.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
9191
promisesArray.push($injector.get("$http").get(url)
9292
.then(function (responseData) {
9393

94-
// wrap the response again with the adapter if the recursive flag is set
94+
// wrap the response again with the adapter and return the promise
9595
if (recursive) {
96-
promisesArray.push(processData(responseData.data, fetchLinkNames, true).then(function (processedData) {
96+
return processData(responseData.data, fetchLinkNames, true).then(function (processedData) {
9797
data[key] = processedData;
98-
}));
98+
});
9999
} else {
100-
data[key] = responseData.data;
100+
return processData(responseData.data).then(function (processedData) {
101+
data[key] = processedData;
102+
});
101103
}
102104
}, function (error) {
103105
if (error.status != 404) {

test/angular-spring-data-rest-provider.fetch.spec.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ describe("the fetch function", function () {
207207
respond(300, "error");
208208
this.httpBackend.expectGET(testCategoryHref);
209209

210-
SpringDataRestAdapter.process(this.rawResponse, fetchLinkName).then(function (responseData) {
210+
SpringDataRestAdapter.process(this.rawResponse, fetchLinkName).then(function () {
211211
throw new Error("Should not be called when the promise is rejected")
212212
}, function (error) {
213213
expect(error.status).toBe(300);
@@ -243,5 +243,38 @@ describe("the fetch function", function () {
243243
this.httpBackend.verifyNoOutstandingExpectation();
244244
});
245245

246+
247+
it("must process the fetched responses as all the other responses", function () {
248+
249+
var embeddedNewKey = this.config.embeddedNewKey;
250+
var fetchLinkName = 'parentCategory';
251+
252+
// the correct link href url
253+
var firstParentCategoryHref = 'http://localhost:8080/categories/f974f5ef-a951-43b4-9027-4d2163216e54/parentCategory';
254+
var secondParentCategoryHref = 'http://localhost:8080/categories/b5ba38d5-98d3-4579-8709-a28549406697/parentCategory';
255+
256+
// check if the underlying fetch function is called with the correct href url
257+
var firstExpectedResult = mockData();
258+
var secondExpectedResult = mockData();
259+
260+
this.httpBackend.whenGET(firstParentCategoryHref).
261+
respond(200, firstExpectedResult);
262+
this.httpBackend.expectGET(firstParentCategoryHref);
263+
264+
this.httpBackend.whenGET(secondParentCategoryHref).
265+
respond(200, secondExpectedResult);
266+
this.httpBackend.expectGET(secondParentCategoryHref);
267+
268+
SpringDataRestAdapter.process(this.rawResponse, fetchLinkName).then(function (processedData) {
269+
// expect the fetched objects
270+
expect(processedData[embeddedNewKey][0][fetchLinkName][embeddedNewKey][0]['name']).toEqual('Test category 1');
271+
expect(processedData[embeddedNewKey][1][fetchLinkName][embeddedNewKey][1]['name']).toEqual('Test category 2');
272+
});
273+
274+
this.httpBackend.flush();
275+
this.httpBackend.verifyNoOutstandingRequest();
276+
this.httpBackend.verifyNoOutstandingExpectation();
277+
});
278+
246279
});
247280

0 commit comments

Comments
 (0)