46

Are there any tools/libraries to convert OpenAPI 2.0 definitions to OpenAPI 3.0, without doing it one per row?

Rumesh Madhusanka
  • 1,105
  • 3
  • 12
  • 26
  • 1
    Related: [Convert OpenAPI 3.0 to Swagger 2.0](https://stackoverflow.com/q/56637299/113116) – Helen Jan 15 '20 at 10:43

1 Answers1

120

Swagger Editor

Paste your OpenAPI 2.0 definition into https://editor.swagger.io and select Edit > Convert to OpenAPI 3 from the menu.

Swagger Editor conversion from OpenAPI 2.0 to OpenAPI 3.0

Swagger Converter

Converts OpenAPI 2.0 and Swagger 1.x definitions to OpenAPI 3.0.

https://converter.swagger.io/api/convert?url=OAS2_YAML_OR_JSON_URL

This gives you JSON. If you want YAML, send the request with the Accept: application/yaml header:

curl "https://converter.swagger.io/api/convert?url=OAS2_YAML_OR_JSON_URL" -H "Accept: application/yaml" -o ./openapi.yaml

API docs: https://converter.swagger.io

GitHub repo: https://github.com/swagger-api/swagger-converter

Swagger Codegen version 3.x

Can also convert OpenAPI 2.0 and Swagger 1.x definitions to OpenAPI 3.0. Swagger Codegen has a CLI version, Maven plugin, Docker images.

Here's an example using the command-line version (you can download the latest JAR from Maven Central). Write the entire command on one line. Use openapi-yaml to get YAML or openapi to get JSON.

java -jar swagger-codegen-cli-3.0.19.jar generate
     -l openapi-yaml
     -i https://petstore.swagger.io/v2/swagger.yaml
     -o OUT_DIR

GitHub repo: https://github.com/swagger-api/swagger-codegen

Helen
  • 87,344
  • 17
  • 243
  • 314
  • 2
    BTW: It can also be achieved with `openapi-generator-cli`: https://github.com/OpenAPITools/openapi-generator . – Michal Foksa Jan 15 '20 at 13:53
  • @MichalFoksa Feel free to post an answer with openapi-generator usage example. There are a few other converters (Mermade, API Transformer, ...), I just listed those that I've personally used. – Helen Jan 15 '20 at 14:03
  • 4
    Note that Swagger Editor also uses the online converter. It shows a message before doing so: "Swagger Editor's contents will be sent to https://converter.swagger.io/api/convert". – EndlosSchleife May 08 '20 at 10:22
  • 3
    `openapi-generator-cli` uses `-g` instead of `-l` shown here. Otherwise, seems to work similarly. – OneCricketeer May 06 '21 at 20:00
  • 1
    [swagger2openapi](https://github.com/Mermade/oas-kit/blob/main/packages/swagger2openapi/README.md) can also convert OpenAPI 2.0 to OpenAPI 3.0 – Delthas Oct 11 '21 at 10:34
  • 6
    This no longer appears to be an option in that menu... – meawoppl May 11 '22 at 18:47
  • 3
    @meawoppl [it's a bug](https://github.com/swagger-api/swagger-editor/issues/3055) – Helen May 12 '22 at 07:05
  • 2
    @meawoppl the issue with the missing "Convert to OpenAPI 3" command was [fixed](https://github.com/swagger-api/swagger-editor/pull/3083) in Swagger Editor v. 4.2.9. The command is now visible again on https://editor.swagger.io. – Helen Jun 16 '22 at 19:30
  • If you wanna run with swagger2openapi you wan use this command: swagger2openapi --yaml --outfile "$path/${service}.yaml" "$path/${service}.json" – EliasM Aug 03 '22 at 16:53