fix: use global scope for metadata storage #929
Merged
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
Currently, the type metadata are cached in
MetadataStorage
which is createdsrc/storage.ts
and exported. As long as this file is imported once, it works as intended. When importing (requiring) file, Node.js looks it up in the import cache so that the every file is imported only once. This however relies on consistent file names and has some caveats as covered in the documentation. For instance file case sensitiveness or using symlinks inside node_modules. This behavior can lead to having more than one instance ofMetadataStorage
and failing to resolve the correct data types, consequently spitting up random errors.Solution
Taking inspiration from class-validator I propose to use
global
as a place which persists through the whole lifetime and is guaranteed to create a true singleton.In the meantime, I am publishing my own fork containing this fix at class-transformer-global-storage
Caveats
This solution might cause some problems if class-transformer is transitively required by multiple libraries using different versions. First package to import class-transformer will seize the global scope and creating the (possibly outdated) singleton.
Checklist
Update index.md
)develop
)npm run prettier:check
passesnpm run lint:check
passesFixes
fixes #928