Skip to content

Commit

Permalink
fix: incorrect inherit _transformmetadatas nestjs#1224
Browse files Browse the repository at this point in the history
  • Loading branch information
KuanWenChen committed Nov 14, 2023
1 parent d98e446 commit 94de40a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/type-helpers.utils.ts
Expand Up @@ -152,10 +152,17 @@ function inheritTransformerMetadata(
if (metadataMap.has(targetClass)) {
const existingRules = metadataMap.get(targetClass)!.entries();
const mergeMap = new Map<string, any[]>();

// _transformMetadatas is array, should merge not overwrite
[existingRules, targetMetadataEntries].forEach((entries) => {
for (const [valueKey, value] of entries) {
if (mergeMap.has(valueKey)) {
mergeMap.get(valueKey)!.push(...value);
if (Array.isArray(mergeMap.get(valueKey))) {
mergeMap
.get(valueKey)!
.push(...(Array.isArray(value) ? value : [value])); // merge, if value is not array, transfer to Array
}
// doNothing, should keep current rules, parent should not overwrite children metadata.
} else {
mergeMap.set(valueKey, value);
}
Expand Down
58 changes: 57 additions & 1 deletion tests/omit-type.helper.spec.ts
@@ -1,4 +1,10 @@
import { instanceToInstance, Transform } from 'class-transformer';
import {
Expose,
instanceToInstance,
plainToInstance,
Transform,
Type,
} from 'class-transformer';
import { MinLength, validate } from 'class-validator';
import { OmitType } from '../lib';
import { getValidationMetadataByTarget } from './type-helpers.test-utils';
Expand Down Expand Up @@ -58,6 +64,56 @@ describe('OmitType', () => {
const transformedDto = instanceToInstance(updateDto);
expect(transformedDto.password).toEqual(password + '_transformed');
});

it('should inherit expose and type transformer metadata', () => {
let isGrandparentTypeExecute = false;
let isParentTypeExecute = false;
class Grandparent {
@Expose()
a!: string | number;

@Expose()
@Type(function grandparentType() {
isGrandparentTypeExecute = true;
return Number;
})
b!: string | number;
}
class Parent extends OmitType(Grandparent, ['a']) {
@Expose()
@Type(function parentType() {
isParentTypeExecute = true;
return String;
})
b!: number;
c!: string; // not expose

@Type(() => Number)
d!: string | number;
}
class Children extends OmitType(Parent, ['d']) {
@Expose()
d!: string;
}

const childrenOmitInstance = plainToInstance(
Children,
{ a: 'a', b: 'b', c: 'c', d: 'd' },
{ excludeExtraneousValues: true },
);

const expectKeySet = new Set(['b', 'd']);
const concreteKeyList = Object.keys(childrenOmitInstance);
for (let i = 0; i < concreteKeyList.length; i++) {
expect(expectKeySet.has(concreteKeyList[i])).toEqual(true);
}
expect(concreteKeyList.length).toEqual(expectKeySet.size);
expect(isGrandparentTypeExecute).toEqual(false);
expect(isParentTypeExecute).toEqual(true);
expect(childrenOmitInstance.b).toEqual('b');
expect(childrenOmitInstance.c).toEqual(undefined);
expect(childrenOmitInstance.d).toEqual('d');
});
});

describe('Property initializers', () => {
Expand Down

0 comments on commit 94de40a

Please sign in to comment.