Skip to content

Commit 9eb03e6

Browse files
author
guylabs
committed
Add empty string as default parameter value when returning the available resources instead of undefined.
1 parent fdc980e commit 9eb03e6

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
151151
}
152152
}
153153

154+
// remove parameters which have an empty string as value
155+
angular.forEach(parameters, function (value, key) {
156+
if (value === "") {
157+
delete parameters[key];
158+
}
159+
});
160+
154161
// process the url and call the resources function with the given parameters
155162
return resourcesFunction(getProcessedUrl(data, resourceObject.name), parameters, actions, options);
156163
} else if (resourceObject in resources) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ function removeTemplateParameters(url) {
8686
}
8787

8888
/**
89-
* Returns the template parameters of the given url as array. e.g. from this url
90-
* 'http://localhost:8080/categories{?page,size,sort}' it will return the following array:
91-
* ['page', 'size', 'sort']
89+
* Returns the template parameters of the given url as object. e.g. from this url
90+
* 'http://localhost:8080/categories{?page,size,sort}' it will return the following object:
91+
* {'page': "", 'size': "", 'sort': ""}
9292
*
9393
* @param {string} url the url with the template parameters
94-
* @returns {object} the array containing the template parameters
94+
* @returns {object} the object containing the template parameters
9595
*/
9696
function extractTemplateParameters(url) {
9797
var templateParametersObject = {};
@@ -100,7 +100,7 @@ function extractTemplateParameters(url) {
100100
var templateParametersArray = regexp.exec(url)[1].split(',');
101101

102102
angular.forEach(templateParametersArray, function (value) {
103-
templateParametersObject[value] = undefined;
103+
templateParametersObject[value] = "";
104104
});
105105

106106
return templateParametersObject;

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

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,50 @@ describe("the resources property", function () {
4040
this.httpBackend.flush();
4141
});
4242

43+
it("must call the correct href url with parameters if a resource object with parameters is passed to the $resource method", function () {
44+
45+
// define the resource object and the correct link href url
46+
var resourceObject = {
47+
'name': 'self',
48+
'parameters': {
49+
'parameter1': '1',
50+
'parameter2': '2'
51+
}
52+
};
53+
var resourceHref = "http://localhost:8080/categories?parameter1=1&parameter2=2";
54+
55+
// check if the underlying $resource method is called with the correct href url
56+
var expectedResult = {categoryId: '123'};
57+
this.httpBackend.whenGET(resourceHref).respond(200, expectedResult);
58+
this.httpBackend.expectGET(resourceHref);
59+
var result = this.response[this.config.resourcesKey](resourceObject).get(function () {
60+
expect(result.categoryId).toEqual(expectedResult.categoryId);
61+
});
62+
this.httpBackend.flush();
63+
});
64+
65+
it("must call the correct href url without parameters if a resource object with empty parameters is passed to the $resource method", function () {
66+
67+
// define the resource object and the correct link href url
68+
var resourceObject = {
69+
'name': 'self',
70+
'parameters': {
71+
'parameter1': "",
72+
'parameter2': ""
73+
}
74+
};
75+
var resourceHref = "http://localhost:8080/categories";
76+
77+
// check if the underlying $resource method is called with the correct href url
78+
var expectedResult = {categoryId: '123'};
79+
this.httpBackend.whenGET(resourceHref).respond(200, expectedResult);
80+
this.httpBackend.expectGET(resourceHref);
81+
var result = this.response[this.config.resourcesKey](resourceObject).get(function () {
82+
expect(result.categoryId).toEqual(expectedResult.categoryId);
83+
});
84+
this.httpBackend.flush();
85+
});
86+
4387
it("it must call the overridden resource function with the given resource name", function () {
4488
var url = undefined, paramDefaults = undefined, actions = undefined, options = undefined;
4589
var resourcesFunctionConfiguration = {
@@ -184,9 +228,9 @@ describe("the resources property", function () {
184228
{
185229
name: 'self',
186230
parameters: {
187-
page: undefined,
188-
size: undefined,
189-
sort: undefined
231+
page: "",
232+
size: "",
233+
sort: ""
190234
}
191235
},
192236
{
@@ -227,25 +271,25 @@ describe("the resources property", function () {
227271
{
228272
name: 'users',
229273
parameters: {
230-
page: undefined,
231-
size: undefined,
232-
sort: undefined
274+
page: "",
275+
size: "",
276+
sort: ""
233277
}
234278
},
235279
{
236280
name: 'categories',
237281
parameters: {
238-
page: undefined,
239-
size: undefined,
240-
sort: undefined
282+
page: "",
283+
size: "",
284+
sort: ""
241285
}
242286
},
243287
{
244288
name: 'accounts',
245289
parameters: {
246-
page: undefined,
247-
size: undefined,
248-
sort: undefined
290+
page: "",
291+
size: "",
292+
sort: ""
249293
}
250294
}
251295
]

0 commit comments

Comments
 (0)