Gherman
Gherman

Reputation: 7436

How to update NestJS Swagger module but use OpenAPI 2.0(Swagger)?

We are making an API in Node.js/Typescript with NestJS framework. We use @nestjs/swagger package to make it conform to OpenAPI(formerly known as Swagger). It is inteneded to be used with Azure/autorest to generate client code.

Autorest supports OpenAPI 2.0 and not 3.0 yet. @nestjs/swagger 3.*.* implemented OpenAPI 2.0. When we update @nestjs/swagger to 4.*.* it turns into OpenAPI 3.0. This doesn't fit our needs because we can't use Autorest anymore. On the other hand not updating the package means we might miss out security updates or not even be able to update the entire NestJS framework.

Is there any way to update @nestjs/swagger and stay with OpenAPI 2.0?

Upvotes: 3

Views: 2170

Answers (2)

Wendryo
Wendryo

Reputation: 1

Maybe you can use the library

api-spec-converter enter link description here

const apiConverter = require('api-spec-converter');
const yaml = require('yaml');
const fs = require('fs');

apiConverter.convert(
  {
    sintax: 'yaml',
    order: 'openapi',
    from: 'openapi_3',
    to: 'swagger_2',
    source: './swagger-v3.json',
  },
  function(err, converted) {
    if (err) {
      console.log(err);
      console.log('Error converting file');
      return;
    }
    const yamlString = yaml.stringify(converted.spec);
    fs.writeFileSync('./swagger-v2.yaml', yamlString);

    console.log('Done!');
  },
);

Add a call script in the package.json:

"convert:swagger": "node ./swagger-converter.js"

Please note that the source path is the one generated by NestJS.

Another point is that I'm generating the output in .yaml, but you can generate it in .json by just removing the syntax options and using converter.stringify() option

Upvotes: 0

Chau Tran
Chau Tran

Reputation: 5098

Unfortunately no. Internally, @nestjs/swagger v4 is specifically generating OpenAPI 3.0 specification specifically. There are two options right now while you wait for autorest 3 to be released officially.

  • Stay on @nestjs/swagger v3. As for update the entire NestJS framework, you don't have to worry about this because @nestjs/swagger is a pretty much stand alone package. Security issue regarding dependencies, I can see your concern is valid there.
  • Try autorest 3.beta out to see if it's stable enough for you to use.

Upvotes: 4

Related Questions