Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking change in 2.0.3 for override fields #1224

Closed
2 tasks done
vsamofal opened this issue Nov 14, 2023 · 5 comments
Closed
2 tasks done

Breaking change in 2.0.3 for override fields #1224

vsamofal opened this issue Nov 14, 2023 · 5 comments

Comments

@vsamofal
Copy link

Did you read the migration guide?

  • I have read the whole migration guide

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Potential Commit/PR that introduced the regression

c65d434

Versions

2.0.0 -> 2.0.3

Describe the regression

sample entities hierarchy:

abstract class BaseEntityHelper {
        @IsUUID()
        @Expose()
        id!: string | number;
        
        @Expose()
        anotherField!: string;
    }

    class SampleEntity extends BaseEntityHelper {

        @Expose()
        @IsUUID()
        override id!: string;

        @Expose()
        @IsString()
        name!: string;

    }

    class SampleEntityDto extends OmitType(SampleEntity, ['name'] as const) {

    }

So we have an abstract class with an id that can be a number or string overriding it and exposing it. Using omit type to create DTO, that has just a name field.

Let's implement a simple test:

it('instance to instance', () => {


            let sampleEntityWithoutId = new SampleEntity();

            sampleEntityWithoutId.id = 'just id';
            sampleEntityWithoutId.name = 'name';
            sampleEntityWithoutId.anotherField = 'anotherField';


            let dto = plainToInstance(SampleEntityDto, sampleEntityWithoutId, {
              excludeExtraneousValues: true,
              exposeDefaultValues: true,
              ignoreDecorators: true,
              exposeUnsetFields: false,
            });

            let justCopy = plainToInstance(SampleEntity, sampleEntityWithoutId, {
                excludeExtraneousValues: true,
                exposeDefaultValues: true,
                ignoreDecorators: true,
                exposeUnsetFields: false,
            });

            // this is failing but should be ok
            expect(dto.anotherField).toEqual('anotherField');
        })

The error is handled over here
image

but thrown over here
image

this behavior recently changed, and it's a breaking change for this case at least.

I'm ok to fix it by myself, just some comments from maintainers on expected behavior will be good to hear

as for me we should apply the most closest decorators or probably merge it somehow, but not sure is it that obvious to do the merge or no

Minimum reproduction code

No response

Input code

provided

Expected behavior

need suggestion

Other

No response

@vsamofal
Copy link
Author

@KuanWenChen probably you can take a look, I understand that you were fixing something, but we also break some, so need to come up with some plan for all known cases to work

@KuanWenChen
Copy link
Contributor

I thing problem is happen at value is not a iterable type. I am not sure why value is not a iterable type.
I thing this could fix it:

[existingRules, targetMetadataEntries].forEach((entries) => {
  for (const [valueKey, value] of entries) {
    const arrayValue = Array.isArray(value) ? value : [value];
    if (mergeMap.has(valueKey)) {
      mergeMap.get(valueKey)!.push(...arrayValue);
    } else {
      mergeMap.set(valueKey, arrayValue);
    }
  }
});

@kamilmysliwiec
Copy link
Member

Could you create a PR for this issue?

@KuanWenChen
Copy link
Contributor

KuanWenChen commented Nov 14, 2023

OK, I think I found the problem.
_excludeMetadatas, _exposeMetadatas, _typeMetadatas should not be a Array
So, they should be overwriten by children.
I would create PR as soon as.

KuanWenChen added a commit to KuanWenChen/mapped-types that referenced this issue Nov 14, 2023
KuanWenChen added a commit to KuanWenChen/mapped-types that referenced this issue Nov 14, 2023
KuanWenChen added a commit to KuanWenChen/mapped-types that referenced this issue Nov 14, 2023
@kamilmysliwiec
Copy link
Member

Let's track this here #1225

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants