nestjs / swagger

OpenAPI (Swagger) module for Nest framework (node.js) :earth_americas:
https://nestjs.com
MIT License
1.68k stars 469 forks source link

Fastify Swagger + TypeORM break application/x-www-form-urlencoded parsing #891

Closed mohd-akram closed 3 years ago

mohd-akram commented 4 years ago

I'm submitting a...


[ ] 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.

Current behavior

500 error when a body with application/x-www-form-urlencoded type is sent. Happens when Swagger is used in conjunction with TypeORM.

Expected behavior

application/x-www-form-urlencoded body is parsed.

Minimal reproduction of the problem with instructions

Repo

npm run start:dev, then curl -da=b localhost:3000

Commenting out either the SwaggerModule.setup or TypeOrmModule.forRoot call causes the issue not to occur.

Environment


Nest version: 7.0.0

jmcdo29 commented 4 years ago

I haven't figured out why yet, but I thought I should at least report, when the TypeORMModule is used with the SwaggerModule the ContentParser for application/x-www-form-urlencoded does not get registered. I'll try to keep digging in, but thought I should at least bring that up for now.

Summon528 commented 4 years ago

If anyone desperately needs this feature, one can downgrade to fastify v2 temporarily.

npm i fastify-swagger@^2 @nestjs/platform-fastify@~7.2 fastify@^2
Quadriphobs1 commented 4 years ago

Could this also be the reason why my docs page shows blank when visited

windok commented 4 years ago

Any updates regarding this? Facing the same issue:

FastifyError: Unsupported Media Type: application/x-www-form-urlencoded
    at ContentTypeParser.run (/usr/src/app/node_modules/fastify/lib/contentTypeParser.js:95:16)
    at handleRequest (/usr/src/app/node_modules/fastify/lib/handleRequest.js:35:39)
    at runPreParsing (/usr/src/app/node_modules/fastify/lib/route.js:358:5)
    at next (/usr/src/app/node_modules/fastify/lib/hooks.js:155:7)
    at Object.runMiddie (/usr/src/app/node_modules/middie/index.js:46:7)
    at hookIterator (/usr/src/app/node_modules/fastify/lib/hooks.js:218:10)
    at next (/usr/src/app/node_modules/fastify/lib/hooks.js:159:20)
    at hookRunner (/usr/src/app/node_modules/fastify/lib/hooks.js:178:3)
    at Object.routeHandler [as handler] (/usr/src/app/node_modules/fastify/lib/route.js:326:7)
    at Router.lookup (/usr/src/app/node_modules/find-my-way/index.js:356:14)
    at Server.emit (events.js:223:5)
    at parserOnIncoming (_http_server.js:759:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:116:17)

When tried to register additionally fastify-formbody during NestJS bootstrap received error that parser is already present:

/usr/src/app/node_modules/fastify/lib/contentTypeParser.js:42
    throw new FST_ERR_CTP_ALREADY_PRESENT(contentType)

FastifyError [Error]: Content type parser 'application/x-www-form-urlencoded' already present.
    at ContentTypeParser.add (/usr/src/app/node_modules/fastify/lib/contentTypeParser.js:42:11)
    at Object.addContentTypeParser (/usr/src/app/node_modules/fastify/lib/contentTypeParser.js:262:30)
    at formBodyPlugin (/usr/src/app/node_modules/fastify-formbody/formbody.js:13:ll)
    at Plugin.exec (/usr/src/app/node_modules/avvio/plugin.js:131:17)
    at Boot.loadPlugin (/usr/src/app/node_modules/avvio/plugin.js:266:10)
    at Task.release (/usr/src/app/node_modules/fastq/queue.js:140:16)
    at worked (/usr/src/app/node_modules/fastq/queue.js:182:10)
    at /usr/src/app/node_modules/avvio/plugin.js:269:7
    at done (/usr/src/app/node_modules/avvio/plugin.js:201:5)
    at check (/usr/src/app/node_modules/avvio/plugin.js:225:9)
    at internal/process/task_queues.js:150:7
    at AsyncResource.runlnAsyncScope (async_hooks.js:173:16)
    at AsyncResource.runMicrotask (internal/process/task_queues.js:147:8)
    at processTicksAndRejections (internal/process/task_queues.js:94:5) {
  name: 'FastifyError',
  code: 'FST_ERR_CTP_ALREADY_PRESENT',
  message: "Content type parser 'application/x-www-form-urlencoded' already present.",
  statusCode: 500
Summon528 commented 4 years ago

Another temporary workaround (disable built-in bodyParser and enable formbody manually)

  import * as FastifyFormBody from 'fastify-formbody';

  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
    { bodyParser: false },
  );

  app.register(FastifyFormBody as any);
patrickmichalina commented 4 years ago

Same issue here. @Summon528's fix works, thanks

squareloop1 commented 4 years ago

Maybe this is somewhat related (although not related to swagger): I am having similar issues with fastify & TypeORM when using fastify-secure-session. It is not working correctly. Here is a minimal repro with steps to reproduce listed in the readme: https://github.com/squareloop1/nestjs-fastify-plugin-repro

kamilmysliwiec commented 3 years ago
const app = await NestFactory.create<NestFastifyApplication>(
  AppModule,
  new FastifyAdapter(),
);
await app.init(); // <-- this

Adding await app.init() should fix this issue. It seems that the body parser plugin must be registered before fastify-swagger (in fastify v3)

ORzazade commented 2 years ago
const app = await NestFactory.create<NestFastifyApplication>(
  AppModule,
  new FastifyAdapter(),
);
await app.init(); // <-- this

@kamilmysliwiec Thank you it solves my problem