Hi there, thanks for the lib!
I have the following use case and wondering if there is a workaround/fix for the issue I'm facing:
I've made an isolated data fetching logic module and put it into a separate npm package to reuse across my projects. class-transformer is it's dependency and there is a method like below in the package:
getDataFromApi(MyModelType)
The idea was that the data will be converted into MyModelType automatically when it's returned from the api.
It's working fine for the top level object. However it doesn't transform the nested objects, even though I set @Type annotation.
The transformation is working fine if I move the "transforming" logic from the npm package back to my app (basically I just call plainToClass(clazz, data), where clazz is MyModelType in this case).
I'm wondering what might cause the problem and if there is any way to overcome it?
If I can provide any additional info to better cover the use case, please let me know.
Same issue, inside given package in TS all is working.
But as soon as I try to serialize class returned from given package in JS project, it transform as with all decorators forgotten.
reflect-metadata is imported at the root of the project.
If that might help anyone, I ended up with a hack-like workaround passing the top project's classToPlain function to the package and using it inside that package to decode objects which gives the desired result and overcomes the problem in this case.
Try to put a paths mapping into your root tsconfig.json:
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"class-transformer": [
"./node_modules/class-transformer"
]
}
}
}
This is most likely because your dependencies are not flattened and your main code and the external code uses a different version of the library. So your node_modules should look something like this:
node_modules\
- external_lib\
- some-code.js
- node_modules\
- class-transformer\
- class-transformer\
your-main-code.js
Because of this structure
node_modules\external_lib\node_modules\class-transformernode_modules/class-transformerThis means the metadata is stored in the extnernal lib's class-validator and you are trying to transform via your main class-validator instance which doesn't know anything about the operators and classes you have created.
To fix this, make sure your node_modules folder contains exactly one class-validator folder.
Tracked in #384.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Try to put a paths mapping into your root tsconfig.json: