Skip to content

fix: use global scope for metadata storage #929

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
May 4, 2024

Conversation

petrzjunior
Copy link
Contributor

@petrzjunior petrzjunior commented Sep 23, 2021

Description

Currently, the type metadata are cached in MetadataStorage which is created src/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 of MetadataStorage 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

  • the pull request title describes what this PR does (not a vague title like Update index.md)
  • the pull request targets the default branch of the repository (develop)
  • the code follows the established code style of the repository
    • npm run prettier:check passes
    • npm run lint:check passes
  • tests are added for the changes I made (if any source code was modified)
  • documentation added or updated
  • I have run the project locally and verified that there are no errors

Fixes

fixes #928

@RodrigoMardones
Copy link

RodrigoMardones commented Nov 3, 2021

this will be useful if you want to use "class-validator" with "class-controller". right now you need to import metadata inside the controller in order to validate if the transformation is working

@ruscon
Copy link
Contributor

ruscon commented Dec 3, 2021

@NoNameProvided
We have this issue while supporting multiple versions of class-transformer in our infrastructure
Is this feature planned for release in the near future?

EndyKaufman referenced this pull request in EndyKaufman/ngx-dynamic-form-builder Jan 22, 2022
BREAKING CHANGE: Version above 2 has a completely rewritten code, partially backwards compatible

Now `@Expose` and `@Exclude` decorators are used to define model fields, the new version is rigidly dependent on class-transform

Dependencies are not used original, but forks with additional necessary properties, when using this library, you need to replace all original imports with forks with modifications

Fork [class-validator-multi-lang](https://github.com/EndyKaufman/class-validator-multi-lang) - adds translation capability for errors (PR:[https://github.com/typestack/class-validator/pull/743](https://github.com/typestack/class-validator/pull/743))

Fork [class-transformer-global-storage](https://github.com/petrzjunior/class-transformer) - adds the ability to get meta information about all used classes (PR:[https://github.com/typestack/class-transformer/pull/929](https://github.com/typestack/class-transformer/pull/929))

For correct parse metadata, need remove `compilerOptions.downlevelIteration` and append `compilerOptions.emitDecoratorMetadata: true` in `tsconfig.json`
@NoNameProvided
Copy link
Member

Thanks!

@NoNameProvided NoNameProvided merged commit c0ec22a into typestack:develop May 4, 2024
5 of 6 checks passed
Copy link

github-actions bot commented Jun 4, 2024

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 4, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

fix: use global scope for metadata storage
4 participants