如何导出 swagger.json (或 yaml)

如何导出 Swagger 定义文件? 它应该是 JSON 或 YAML 文件,例如 Swagger.JSON 或 Swagger.YAML。

假设我有一个看起来像 http://example.com//swagger/ui/index#!的端点:

enter image description here

版本是 api version: v1

我看不到“导出”按钮。那么我如何导出它呢?

160494 次浏览

The URL of the API definiton is displayed in the top bar of Swagger UI – in your example it's

/v2/api-docs?group=full-petstore-api

So the full URL appears to be

http://localhost:8080/v2/api-docs?group=full-petstore-api

In newer versions of Swagger UI, the link to the API definition is often displayed below the API title, so you can right-click the link and Save As.

Link to API definition in Swagger UI 3.x


If your Swagger UI does not have a visible link to the API definition, view the page source and look for the url parameter, such as:

const ui = SwaggerUIBundle({
url: "https://petstore.swagger.io/v2/swagger.json",     // <-------
dom_id: '#swagger-ui',

If you don't see the url or if url is a code expression, open the browser dev tools, switch to the Network tab and disable caching. Then refresh the page and search for the API definition file (swagger.json, swagger.yaml, api-docs or similar) among HTTP requests. You can filter by XHR to narrow down the list.

Finding the Swagger file on the Network tab of browser dev tools


Another way to find the actual url is to use the browser console and evaluate one of the following values, depending on your UI version:

  • Swagger UI 3.x:

    ui.getConfigs().url
    
  • Swagger UI 2.x:

    swaggerUi.api.url
    


Sometimes the OpenAPI definition may be embedded within a .js file – in this case take this file and strip out the extra parts.

OpenAPI definition embedded in a JavaScript file

Though it's already been answered and it's the correct one, I thought I shall post the much detailed version of it.. Hope this helps,

  1. If you do have the swagger json file which you feed to the swagger UI, then to generate .yaml file just click on the below link copy-paste your json in the editor and download the yaml file. This is a straight forward method

link : https://editor.swagger.io/#

  1. Now the second way where you don't have any swagger json file then the following steps should help,

Open the swagger ui, inspect (Shift+Ctrl+i), refresh the page and you will get the tabs like below

enter image description here

Choose XHR or All tab under Network tab, check for the file api-doc?group=* and click subtab response. *Now copy the content of ap-doc?group.** file and use the same editor link to convert to yaml file

link : https://editor.swagger.io/#

The JSON may also be inlined in the document, specifically for Swagger version 2.0. If you haven't found anything after walking through @Helen's answer give this a try:

  1. View Page Source
  2. Search for "swagger" or "spec"

If you see a <script type="application/json"> tag with something similar to the following in it, this is effectively your swagger.json content. Copy everything inside of the <script> tags and save into a file named swagger.json and you should be good to go.

<script id="swagger-data" type="application/json">
{"spec":{"definitions":{},"info":{},"paths":{},"schemes":[],"swagger":"2.0"}}
</script>

I'm using Django Rest Framework (so pip package django-rest-swagger==2.2.0) and the above answers weren't really sufficient. There were two options:

1) View the page source with developer tools. When I hit my http://localhost:8000/docs/ endpoint, I see:

enter image description here

The docs/ endpoint was configured in Django, so it may be different for you. When digging into the details of that, I can go to the Response tab (in Chrome) and scroll down to find the actual JSON. It's the value in window.drsSpec

enter image description here

2) The alternative (and perhaps easier) approach is to add ?format=openapi to my endpoint, as suggested in https://github.com/marcgibbons/django-rest-swagger/issues/590

This will directly spit out the JSON you need. I imported it into Postman by changing the swagger field to openapi which seems a little hacky but it worked 🤷🏻‍♂️

for

Swashbuckel.aspnet.core(5.5.0)

try

services.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));

I tried this for a Web API core Project

you have to be using

System.Text.Json.Serialization;

  1. Visit http://localhost:49846/swagger/docs/v1
  2. The above URL will return JSON. Save the JSON as swagger.json

Please replace the port number with your port number.