Skip to content

Commit 529715c

Browse files
authored
Merge pull request #9025 from DavidResende0/provider-truncation-fix
Fixed `no implicit conversion of String into Integer` Error when Adding/Editing Container Provider
2 parents 9eb9aef + 458cb18 commit 529715c

File tree

3 files changed

+141
-3
lines changed

3 files changed

+141
-3
lines changed

app/javascript/components/provider-form/helper.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
/** Field names that needs to be trimmed. */
22
const trimFields = ['hostname'];
33

4+
const isObject = (data) => (typeof data === 'object' && data !== null);
5+
46
/** Function to trim a field's value.
57
* If the nested data object contains the key which includes in variable 'trimFields',
68
* then, trim the spaces from the value before sending to API.
79
*/
810
export const trimFieldValue = (resource) => {
11+
if (!isObject(resource)) {
12+
return resource;
13+
}
14+
15+
if (Array.isArray(resource)) {
16+
resource.forEach((value, index) => {
17+
resource[index] = trimFieldValue(value);
18+
});
19+
return resource;
20+
}
21+
922
const keys = Object.keys(resource);
1023

1124
keys.find((key) => {

app/javascript/components/provider-form/index.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ const ProviderForm = ({
174174
}
175175

176176
// Construct the full form data with all the necessary items
177-
const data = {
177+
const data = trimFieldValue({
178178
...rest,
179-
endpoints: trimFieldValue(endpoints),
179+
endpoints,
180180
authentications,
181181
...(edit ? undefined : { type }),
182182
ddf: true,
183-
};
183+
});
184184

185185
const request = providerId ? API.patch(`/api/providers/${providerId}`, data) : API.post('/api/providers', data);
186186
request.then(() => miqRedirectBack(message, 'success', redirect)).catch(miqSparkleOff);
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { trimFieldValue } from '../../components/provider-form/helper';
2+
3+
describe('trimFieldValue function', () => {
4+
it('should trim the hostname field', () => {
5+
const resource = {
6+
hostname: ' example.com ',
7+
otherField: 'value',
8+
};
9+
10+
const trimmedResource = trimFieldValue(resource);
11+
12+
expect(trimmedResource.hostname).toEqual('example.com');
13+
expect(trimmedResource.otherField).toEqual('value');
14+
});
15+
16+
it('should handle nested objects and trim the hostname field', () => {
17+
const resource = {
18+
nestedObject: {
19+
hostname: ' nested-example.com ',
20+
},
21+
otherField: 'value',
22+
};
23+
24+
const trimmedResource = trimFieldValue(resource);
25+
26+
expect(trimmedResource.nestedObject.hostname).toEqual('nested-example.com');
27+
expect(trimmedResource.otherField).toEqual('value');
28+
});
29+
30+
it('should handle multiple nested objects and trim the hostname field', () => {
31+
const resource = {
32+
firstLevel: {
33+
nestedObject: {
34+
hostname: ' nested-example.com ',
35+
},
36+
},
37+
otherField: 'value',
38+
};
39+
40+
const trimmedResource = trimFieldValue(resource);
41+
42+
expect(trimmedResource.firstLevel.nestedObject.hostname).toEqual('nested-example.com');
43+
expect(trimmedResource.otherField).toEqual('value');
44+
});
45+
46+
it('should handle undefined values', () => {
47+
const resource = {
48+
hostname: undefined,
49+
};
50+
51+
const trimmedResource = trimFieldValue(resource);
52+
53+
expect(trimmedResource.hostname).toEqual('');
54+
});
55+
56+
it('should handle null values', () => {
57+
const resource = {
58+
hostname: null,
59+
};
60+
61+
const trimmedResource = trimFieldValue(resource);
62+
63+
expect(trimmedResource.hostname).toEqual('');
64+
});
65+
66+
it('should not modify non-trimmed fields', () => {
67+
const resource = {
68+
username: 'user123',
69+
70+
};
71+
72+
const trimmedResource = trimFieldValue(resource);
73+
74+
expect(trimmedResource.username).toEqual('user123');
75+
expect(trimmedResource.email).toEqual('[email protected]');
76+
});
77+
78+
it('should handle non-object values', () => {
79+
const resource = 'Hello';
80+
81+
const trimmedResource = trimFieldValue(resource);
82+
83+
expect(trimmedResource).toEqual('Hello');
84+
});
85+
86+
it('should handle non-object values inside an array', () => {
87+
const resource = [' example.com ', ' nested-example.com ', { hostname: ' test ' }];
88+
89+
const trimmedResource = trimFieldValue(resource);
90+
91+
expect(trimmedResource).toEqual([' example.com ', ' nested-example.com ', { hostname: 'test' }]);
92+
});
93+
94+
it('should handle objects nested inside an array', () => {
95+
const resource = {
96+
otherField: ' example.com ',
97+
otherField2: [
98+
{
99+
firstLevel: {
100+
nestedObject: {
101+
hostname: ' nested-example.com ',
102+
},
103+
},
104+
nestedField: 'value',
105+
},
106+
],
107+
};
108+
109+
const trimmedResource = trimFieldValue(resource);
110+
111+
expect(trimmedResource).toEqual({
112+
otherField: ' example.com ',
113+
otherField2: [
114+
{
115+
firstLevel: {
116+
nestedObject: {
117+
hostname: 'nested-example.com',
118+
},
119+
},
120+
nestedField: 'value',
121+
},
122+
],
123+
});
124+
});
125+
});

0 commit comments

Comments
 (0)