swagger
swagger copied to clipboard
@nestjs/swagger and bson lib collision problem
I'm submitting a Bug report
[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
import { PrimaryKey, Property, SerializedPrimaryKey } from '@mikro-orm/core';
import { ObjectId } from 'bson';
export abstract class CommonEntity {
@PrimaryKey()
_id: ObjectId;
@SerializedPrimaryKey()
id!: string; // string variant of PK, will be handled automatically
@Property()
createdAt = new Date();
@Property({ onUpdate: () => new Date() })
updatedAt = new Date();
}
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"projects": {},
"compilerOptions": {
"webpack": false,
"plugins": [
{
"name": "@nestjs/swagger",
"options": {
"classValidatorShim": true,
"introspectComments": true
}
}
]
}
}
Current behavior
Error: Cannot find module 'bson/bson' Require stack:
- /Volumes/HD/workspace/_temp/hello-nest/dist/src/entities/common.entity.js
- /Volumes/HD/workspace/_temp/hello-nest/dist/src/modules/users/entities/user.entity.js
- /Volumes/HD/workspace/_temp/hello-nest/dist/src/modules/users/users.service.js
- /Volumes/HD/workspace/_temp/hello-nest/dist/src/modules/users/users.module.js
- /Volumes/HD/workspace/_temp/hello-nest/dist/src/app.module.js
- /Volumes/HD/workspace/_temp/hello-nest/dist/src/main.js at Function.Module._resolveFilename (node:internal/modules/cjs/loader:927:15)
Expected behavior
Expected import bson,dont bson/bson
Minimal reproduction of the problem with instructions
https://github.com/WumaCoder/hello-nest/blob/a27ab9a6eb82d99890a6a2ce56ca2e71057579b9/src/entities/common.entity.ts
What is the motivation / use case for changing the behavior?
npm i npm run start
Environment
Nest version: 8.0
For Tooling issues:
- Node version: node 16
- Platform: mac 15
Others:
I am experiencing the same issue, when using abstract or just extending another class.
Just found a hotfix to replace all bson/bson
to bson
:
grep -rl bson/bson . | grep dist/src | xargs sed -i '' -e 's/bson\/bson/bson/g'
Note: sed command is adapted to run on macOS env.
After more investigation, downgrading bson
from 4.6.1
to 1.1.5
into packages.json seems to fix the issue.
Any solution for this problem? The error continues in the latest versions to date.
Hey @WumaCoder I didn't manage to reproduce this in my Ubuntu.
git clone --depth=1 https://github.com/WumaCoder/hello-nest
cd hello-nest
npm i
npm run start
I can't reproduce it. Close it first, and open it later.
having this problem:
If I comment the setting swagger line, the app works fine. Seems like a dependency collision . @WumaCoder can you reopen again
Yeah. I will try to provide a minimal reproduction asap
@micalevisk here is a reproduction https://github.com/rubiin/nest-swagger-bug . Basically problem from file common.entity.ts
Also i noted
@ApiHideProperty()
@PrimaryKey()
_id: ObjectId;
Doing this does not produce any error . However on other cases swagger plugin tries to introspect the ObjectId
type and thats when bson/bson
lib collision occurs
Having the same issue
I managed to fix this issue by adding @agravelot's custom script in package.json and forcing it to run after nest build script like so.
{
"scripts": {
"build": "nest build",
"postbuild": "npm run fix:bson",
"start": "nest start --exec 'npm run fix:bson; node'",
"fix:bson": "grep -rl bson/bson . | grep dist | xargs sed -i -e 's/bson\\/bson/bson/g'"
},
}
Any fixes so far. Having problem still
@rubiin please share the steps to reproduce this using your repo https://github.com/rubiin/nest-swagger-bug npm i && npm start
went fine so far 🤔
Did you run mongodb instance locally? Without that the orm wont bootup You can see here the logs https://imgur.com/a/K7VbNCL @micalevisk
This appears to be an issue with the CLI Plugin. I was able to solve this issue by removing the @nestjs/swagger
plugin declaration from the nest-cli.json
file. We have declared decorators manually so the plugin was not needed. As soon as it was removed, this problem went away.
One workaround is to install 'bson' version 1.1.5 directly and use:
import {ObjectId} from 'bson';
instead of import {ObjectId} from 'mongodb'}
Another workaround is to not have prefix in the entity filename.
So instead of common.entity.js
just use entity.js
not ideal I know but it works
One more solution - manual property declaration
@ApiProperty({ required: true, type: String })
@PrimaryKey()
_id: ObjectId;
swagger does not have ObjectId as an allowed type and anyway you have to transform payload from string to object id manually