fix: made metadata global to prevent issues with multiple package installations #1440
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
The
MetadataStorage
object currently acts as a singleton within the module by using thedefaultMetadataStorage
constant. However, this doesn't account for the scenario in which there are multiple instances of theclass-transformer
library installed; this seems to be common in two cases:class-transformer
as a prod dependency. If the library and its consumer are on two different versions ofclass-validator
then we end up with aMetadataStorage
object for each version installedHaving duplicate metadata stores can have some really subtle behavior for users. In our case we were using
class-transformer
alongsideclass-validator
to validate incoming messages; because our validators were coming in from a shared library the@Type()
decorator was silently failing its lookup, resulting in all of our nested validators failing to run.We were made aware of this issue by the breaking change in
[email protected]
(changelog) to forbid unknown values by default. Because our objects were silently being only partially transformed to DTOs by this bug, they were being considered as an "unknown value". I imagine that this may be affecting other users as well since there's been a decent amount of chatter about this on the NestJS discord and issue boards (examples: typestack/class-validator#1873, nestjs/nest#10683)This implementation is lifted from what was done in typestack/class-validator#265 to use a global store to avoid these conflicts.
Checklist
Update index.md
)develop
)npm run prettier:check
passesnpm run lint:check
passesFixes
fixes #1043, fixes #928
Update: found PR #929 from 2021 which is similar but contains a lot of automatic dependency updates. This change might be easier to merge at this point so going to leave it up for one of the maintainers to decide between