Сгенерируйте yaml swagger 2.0 с помощью пакета swagger 4.x - PullRequest
2 голосов
/ 19 июня 2020

Я собираюсь интегрировать свой сервер API с конечными точками Google Cloud.

А конечные точки Google Cloud на данный момент поддерживают swagger 2.0.

Но мои зависимости / библиотеки сейчас обновлены. Итак, я хочу сгенерировать yaml-файл swagger 2.0 без понижения версии библиотеки swagger (конечные точки api уже описаны в swagger 4.x - openapi 3.0 spe c).

Nest js и зависимости swagger (пакет . json):

...

"@nestjs/common": "^7.0.0",
"@nestjs/config": "^0.4.0",
"@nestjs/core": "^7.0.0",
"@nestjs/platform-express": "^7.0.0",
"js-yaml": "^3.14.0",

...

"@nestjs/swagger": "^4.5.4",
"swagger-ui-express": "^4.1.4",

...

Скрипт генератора чванства:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as fs from 'fs'
import * as yaml from 'js-yaml'

const generateSwaggerYaml = async () => {
  const app = await NestFactory.create(AppModule);
  const options = new DocumentBuilder()
    .setTitle('API Title')
    .setDescription('API Description')
    .build()

  const document = SwaggerModule.createDocument(app, options)

  fs.writeFileSync("./openapi-run.yaml", yaml.safeDump(document))
}


generateSwaggerYaml()

И вывод скрипта - openapi 3.0 spe c: (

openapi: 3.0.0
info:
  title: API Title
  description: API Description.
  version: 1.0.0
  contact: {}
tags: []
servers: []

...

Есть ли вариант / способ сгенерировать yaml swagger2.0 из документа openapi 3.0?

Как я могу понизить версию openapi 3.0 spe c до swagger 2.0 spe c?

1 Ответ

2 голосов
/ 19 июня 2020

Я использую этот проект из github именно для этой цели: https://github.com/LucyBot-Inc/api-spec-converter

Для openapi 3 yaml to swagger 2 yaml это так же просто, как $ api-spec-converter --from openapi_3 --syntax yaml --to swagger_2 ${f} > ${SWAGGER_V2_FILE}

...