67

I have some API definitions in OpenAPI 2.0 format, but my tooling requires OpenAPI 3.0 format. How do I convert OpenAPI 2.0 to OpenAPI 3.0? Ideally I want to be able to script the process to avoid manual copying and pasting.

1

1 Answer 1

147

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

Sign up to request clarification or add additional context in comments.

10 Comments

BTW: It can also be achieved with openapi-generator-cli: github.com/OpenAPITools/openapi-generator .
Note that Swagger Editor also uses the online converter. It shows a message before doing so: "Swagger Editor's contents will be sent to converter.swagger.io/api/convert".
openapi-generator-cli uses -g instead of -l shown here. Otherwise, seems to work similarly.
This no longer appears to be an option in that menu...
@meawoppl it's a bug
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.