Skip to content

Commit e987b49

Browse files
authored
Merge pull request #286 from jomendez/ignore-getters
fix: Getter methods decorated with @ignore() should not be present
2 parents 7b61dbd + 8cd92ba commit e987b49

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/utils.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,31 @@ describe('Utils', () => {
7676
expect(serializeEntity(rhcp, [])).not.toHaveProperty('temporaryName');
7777
});
7878

79+
it('should not return getter properties with an Ignore() decorator', () => {
80+
class Band implements IEntity {
81+
id: string;
82+
name: string;
83+
84+
get removeFirstLetterOfName() {
85+
if (!this.name) return '';
86+
return this.name.charAt(0);
87+
}
88+
89+
@Ignore()
90+
get capitalizedName() {
91+
if (!this.name) return '';
92+
return this.name.charAt(0).toUpperCase() + this.name.slice(1);
93+
}
94+
}
95+
96+
const rhcp = new Band();
97+
rhcp.name = 'red Hot Chili Peppers';
98+
99+
expect(serializeEntity(rhcp, [])).toHaveProperty('name');
100+
expect(serializeEntity(rhcp, [])).toHaveProperty('removeFirstLetterOfName');
101+
expect(serializeEntity(rhcp, [])).not.toHaveProperty('capitalizedName');
102+
});
103+
79104
it('should serialize object properties with the @Serialize() decorator', () => {
80105
class Address {
81106
streetName: string;

src/utils.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,10 @@ export function serializeEntity<T extends IEntity>(
5555
delete serializableObj[scm.propertyKey];
5656
});
5757

58-
Object.keys(obj).forEach(propertyKey => {
58+
Object.entries(serializableObj).forEach(([propertyKey, propertyValue]) => {
5959
if (Reflect.getMetadata(ignoreKey, obj, propertyKey) === true) {
6060
delete serializableObj[propertyKey];
6161
}
62-
});
63-
64-
Object.entries(serializableObj).forEach(([propertyKey, propertyValue]) => {
6562
if (Reflect.getMetadata(serializeKey, obj, propertyKey) !== undefined) {
6663
if (Array.isArray(propertyValue)) {
6764
(serializableObj as { [key: string]: unknown })[propertyKey] = propertyValue.map(element =>

0 commit comments

Comments
 (0)