swagger icon indicating copy to clipboard operation
swagger copied to clipboard

@nestjs/swagger and bson lib collision problem

Open WumaCoder opened this issue 2 years ago • 20 comments

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:

WumaCoder avatar Sep 22 '21 07:09 WumaCoder

I am experiencing the same issue, when using abstract or just extending another class.

niklaszantner avatar Jan 01 '22 20:01 niklaszantner

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.

agravelot avatar Jan 18 '22 15:01 agravelot

After more investigation, downgrading bson from 4.6.1 to 1.1.5 into packages.json seems to fix the issue.

agravelot avatar Jan 18 '22 15:01 agravelot

Any solution for this problem? The error continues in the latest versions to date.

CAEG-DEV avatar Apr 10 '22 17:04 CAEG-DEV

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

image

micalevisk avatar Apr 12 '22 18:04 micalevisk

I can't reproduce it. Close it first, and open it later.

WumaCoder avatar May 02 '22 04:05 WumaCoder

having this problem:

rubiin avatar Aug 06 '22 09:08 rubiin

If I comment the setting swagger line, the app works fine. Seems like a dependency collision . @WumaCoder can you reopen again

rubiin avatar Aug 06 '22 09:08 rubiin

@rubiin can you share a minimum reproduction of that?

why reproductions are required

micalevisk avatar Aug 06 '22 14:08 micalevisk

Yeah. I will try to provide a minimal reproduction asap

rubiin avatar Aug 06 '22 14:08 rubiin

@micalevisk here is a reproduction https://github.com/rubiin/nest-swagger-bug . Basically problem from file common.entity.ts

rubiin avatar Aug 08 '22 17:08 rubiin

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

rubiin avatar Aug 08 '22 17:08 rubiin

Having the same issue

mrshaq avatar Aug 29 '22 23:08 mrshaq

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'"
    },
}

mrshaq avatar Sep 01 '22 20:09 mrshaq

Any fixes so far. Having problem still

rubiin avatar Sep 09 '22 07:09 rubiin

@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 🤔

micalevisk avatar Nov 07 '22 00:11 micalevisk

Did you run mongodb instance locally? Without that the orm wont bootup You can see here the logs https://imgur.com/a/K7VbNCL @micalevisk

rubiin avatar Nov 07 '22 06:11 rubiin

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.

shwaddell28 avatar Dec 21 '22 17:12 shwaddell28

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

KolevDarko avatar Jul 19 '23 16:07 KolevDarko

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

nglazov avatar Feb 16 '24 21:02 nglazov