Skip to content

Commit

Permalink
[React] Numeric field of an entity is sent as a string in the JSON vi…
Browse files Browse the repository at this point in the history
…a REST API
  • Loading branch information
qmonmert committed Oct 14, 2023
1 parent e14a9e1 commit 54a6ce0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { convertDateTimeFromServer, convertDateTimeToServer, displayDefaultDateT
<%_ if (relationships.filter(e => e.relationshipManyToMany) !== undefined) { _%>
import { mapIdList } from 'app/shared/util/entity-utils';
<%_ } _%>
import { prepareValuesToSave } from 'app/shared/util/entity-utils';
import { useAppDispatch, useAppSelector } from 'app/config/store';

<%_
Expand Down Expand Up @@ -159,7 +160,7 @@ _%>

const entity = {
...<%= entityInstance %>Entity,
...values,
...prepareValuesToSave(values, <%= entityInstance %>Entity),
<%_ manyToManyOwners.forEach(rel => { _%>
<%= rel.relationshipFieldNamePlural %>: mapIdList(values.<%= rel.relationshipFieldNamePlural %>),
<%_ }) _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
-%>


import { cleanEntity, mapIdList } from './entity-utils';
import { cleanEntity, mapIdList, prepareValuesToSave } from './entity-utils';

describe('Entity utils', () => {
describe('cleanEntity', () => {
Expand Down Expand Up @@ -73,4 +73,31 @@ describe('Entity utils', () => {
expect(mapIdList(ids)).toEqual([]);
});
});

describe('prepareValuesToSave', () => {
it("should convert string type to number type if the property type is number", () => {
interface ITestEntity {
id?: number;
name?: string;
age?: number;
};
const entity: ITestEntity = {
id: 1,
name: 'oldName',
age: 20,
};
const values = {
id: 1,
name: 'newTest',
age: '21',
};
const result = {
id: 1,
name: 'newTest',
age: 21,
};

expect(prepareValuesToSave(values, entity)).toEqual(result);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ export const overridePaginationStateWithQueryParams = (paginationBaseState: IPag
}
return sortedPaginationState;
};

export const prepareValuesToSave = (values, entity) => {
const result = { ...values };
for (const property in values) {
if (typeof entity[property] === 'number' && typeof values[property] === 'string') {
result[property] = parseFloat(values[property]);
}
}
return result;
};

0 comments on commit 54a6ce0

Please sign in to comment.