You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 14 KiB

10 years ago
13 years ago
9 years ago
12 years ago
11 years ago
10 years ago
10 years ago
10 years ago
13 years ago
9 years ago
10 years ago
10 years ago
13 years ago
12 years ago
11 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
13 years ago
11 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
12 years ago
10 years ago
11 years ago
9 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
12 years ago
9 years ago
10 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # Swagger UI
  2. [![Build Status](https://travis-ci.org/swagger-api/swagger-ui.svg?branch=master)](https://travis-ci.org/swagger-api/swagger-ui)
  3. Swagger UI is part of the Swagger project. The Swagger project allows you to produce, visualize and consume your OWN RESTful services. No proxy or 3rd party services required. Do it your own way.
  4. Swagger UI is a dependency-free collection of HTML, Javascript, and CSS assets that dynamically
  5. generate beautiful documentation and sandbox from a Swagger-compliant API. Because Swagger UI has no dependencies, you can host it in any server environment, or on your local machine.
  6. ## What's Swagger?
  7. The goal of Swagger™ is to define a standard, language-agnostic interface to REST APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. When properly defined via Swagger, a consumer can understand and interact with the remote service with a minimal amount of implementation logic. Similar to what interfaces have done for lower-level programming, Swagger removes the guesswork in calling the service.
  8. Check out [Swagger-Spec](https://github.com/swagger-api/swagger-spec) for additional information about the Swagger project, including additional libraries with support for other languages and more.
  9. ## Compatibility
  10. The Swagger Specification has undergone 4 revisions since initial creation in 2010. Compatibility between swagger-ui and the Swagger specification is as follows:
  11. Swagger UI Version | Release Date | Swagger Spec compatibility | Notes | Status
  12. ------------------ | ------------ | -------------------------- | ----- | ------
  13. 2.1.1 | 2015-06-06 | 1.1, 1.2, 2.0 | [master](https://github.com/swagger-api/swagger-ui) |
  14. 2.0.24 | 2014-09-12 | 1.1, 1.2 | [tag v2.0.24](https://github.com/swagger-api/swagger-ui/tree/v2.0.24) |
  15. 1.0.13 | 2013-03-08 | 1.1, 1.2 | [tag v1.0.13](https://github.com/swagger-api/swagger-ui/tree/v1.0.13) |
  16. 1.0.1 | 2011-10-11 | 1.0, 1.1 | [tag v1.0.1](https://github.com/swagger-api/swagger-ui/tree/v1.0.1) |
  17. ## How to Use It
  18. ### Download
  19. You can use the swagger-ui code AS-IS! No need to build or recompile--just clone this repo and use the pre-built files in the `dist` folder. If you like swagger-ui as-is, stop here.
  20. ##### Browser support
  21. Swagger UI works in all evergreen desktop browsers (Chrome, Safari, Firefox). Internet Explorer support is version 8 (IE8) and above.
  22. ### Build
  23. You can rebuild swagger-ui on your own to tweak it or just so you can say you did. To do so, follow these steps:
  24. 1. `npm install`
  25. 2. `gulp`
  26. 3. You should see the distribution under the dist folder. Open [`./dist/index.html`](./dist/index.html) to launch Swagger UI in a browser
  27. ### Development
  28. Use `gulp watch` to make a new build and watch for changes in files.
  29. ### Build using Docker
  30. To build swagger-ui using a docker container:
  31. ```
  32. docker build -t swagger-ui-builder .
  33. docker run -p 127.0.0.1:8080:8080 swagger-ui-builder
  34. ```
  35. This will start Swagger UI at `http://localhost:8080`.
  36. ### Use
  37. Once you open the Swagger UI, it will load the [Swagger Petstore](http://petstore.swagger.io/v2/swagger.json) service and show its APIs. You can enter your own server url and click explore to view the API.
  38. ### Customize
  39. You may choose to customize Swagger UI for your organization. Here is an overview of whats in its various directories:
  40. - dist: Contains a distribution which you can deploy on a server or load from your local machine.
  41. - dist/lang: The swagger localization
  42. - lib: Contains javascript dependencies which swagger-ui depends on
  43. - node_modules: Contains node modules which swagger-ui uses for its development.
  44. - src
  45. - src/main/templates: [handlebars](http://handlebarsjs.com/) templates used to render swagger-ui
  46. - src/main/html: the html files, some images and css
  47. - src/main/javascript: main code
  48. ### SwaggerUi
  49. To use swagger-ui you should take a look at the [source of swagger-ui html page](https://github.com/swagger-api/swagger-ui/blob/master/dist/index.html) and customize it. This basically requires you to instantiate a SwaggerUi object and call load() on it as below:
  50. ```javascript
  51. var swaggerUi = new SwaggerUi({
  52. url:"http://petstore.swagger.io/v2/swagger.json",
  53. dom_id:"swagger-ui-container"
  54. });
  55. swaggerUi.load();
  56. ```
  57. ##### Parameters
  58. Parameter Name | Description
  59. --- | ---
  60. url | The url pointing to `swagger.json` (Swagger 2.0) or the resource listing (earlier versions) as per [Swagger Spec](https://github.com/swagger-api/swagger-spec/).
  61. spec | A JSON object describing the Swagger specification. When used, the `url` parameter will not be parsed. This is useful for testing manually-generated specifications without hosting them. Works for Swagger 2.0 specs only.
  62. validatorUrl | By default, Swagger-UI attempts to validate specs against swagger.io's online validator. You can use this parameter to set a different validator URL, for example for locally deployed validators ([Validator Badge](https://github.com/swagger-api/validator-badge)). Setting it to `null` will disable validation. This parameter is relevant for Swagger 2.0 specs only.
  63. dom_id | The id of a dom element inside which SwaggerUi will put the user interface for swagger.
  64. booleanValues | SwaggerUI renders boolean data types as a dropdown. By default it provides a 'true' and 'false' string as the possible choices. You can use this parameter to change the values in dropdown to be something else, for example 0 and 1 by setting booleanValues to new Array(0, 1).
  65. docExpansion | Controls how the API listing is displayed. It can be set to 'none' (default), 'list' (shows operations for each resource), or 'full' (fully expanded: shows operations and their details).
  66. apisSorter | Apply a sort to the API/tags list. It can be 'alpha' (sort by name) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged.
  67. operationsSorter | Apply a sort to the operation list of each API. It can be 'alpha' (sort by paths alphanumerically), 'method' (sort by HTTP method) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged.
  68. onComplete | This is a callback function parameter which can be passed to be notified of when SwaggerUI has completed rendering successfully.
  69. onFailure | This is a callback function parameter which can be passed to be notified of when SwaggerUI encountered a failure was unable to render.
  70. highlightSizeThreshold | Any size response below this threshold will be highlighted syntactically, attempting to highlight large responses can lead to browser hangs, not including a threshold will default to highlight all returned responses.
  71. supportedSubmitMethods | An array of of the HTTP operations that will have the 'Try it out!' option. An empty array disables all operations. This does not filter the operations from the display.
  72. oauth2RedirectUrl | OAuth redirect URL
  73. showRequestHeaders | Whether or not to show the headers that were sent when making a request via the 'Try it out!' option. Defaults to `false`.
  74. * All other parameters are explained in greater detail below
  75. ### HTTP Methods and API Invocation
  76. swagger-ui supports invocation of all HTTP methods APIs including GET, PUT, POST, DELETE, PATCH, OPTIONS. These are handled in the [swagger-js](https://github.com/swagger-api/swagger-js) project, please see there for specifics on their usage.
  77. ### Header Parameters
  78. Header params are supported through a pluggable mechanism in [swagger-js](https://github.com/swagger-api/swagger-js). You can see the [index.html](https://github.com/swagger-api/swagger-ui/blob/master/dist/index.html) for a sample of how to dynamically set headers:
  79. ```js
  80. // add a new SwaggerClient.ApiKeyAuthorization when the api-key changes in the ui.
  81. $('#input_apiKey').change(function() {
  82. var key = $('#input_apiKey')[0].value;
  83. if(key && key.trim() != "") {
  84. swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("api_key", key, "header"));
  85. }
  86. })
  87. ```
  88. This will add header `api_key` with value `key` on every call to the server. You can substitute `query` to send the values as a query param.
  89. ### Custom Header Parameters - (For Basic auth etc)
  90. If you have some header parameters which you need to send with every request, use the headers as below:
  91. ```js
  92. swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("Authorization", "XXXX", "header"));
  93. ```
  94. Note! You can pass multiple header params on a single request, just use unique names for them (`key` is used in the above example).
  95. ### Localization and translation
  96. The localization files are in the [lang](/lang) directory. Note that language files and translator is not included in SwaggerUI by default. You need to add them manually.
  97. To enable translation you should append next two lines in your Swagger's index.html (or another entry point you use)
  98. ```html
  99. <script src='lang/translator.js' type='text/javascript'></script>
  100. <script src='lang/en.js' type='text/javascript'></script>
  101. ```
  102. The first line script is a translator and the second one is your language lexemes.
  103. If you wish to append support for new language you just need to create lang/your_lang.js and fill it like it's done in existing files.
  104. To append new lexemex for translation you shoul do two things:
  105. 1. Add lexeme into the language file.
  106. Example of new line: "new sentence":"translation of new sentence".
  107. 2. Mark this lexeme in source html with attribute data-sw-translate.
  108. Example of changed source:
  109. ```html
  110. <anyHtmlTag data-sw-translate>new sentence</anyHtmlTag>
  111. or <anyHtmlTag data-sw-translate value='new sentence'/>
  112. ```
  113. .
  114. At this moment only inner html, title-attribute and value-attribute are going to be translated.
  115. ## CORS Support
  116. ### OR: How to deal with "Can't read from server. It may not have the appropriate access-control-origin settings."
  117. CORS is a technique to prevent websites from doing bad things with your personal data. Most browsers + javascript toolkits not only support CORS but enforce it, which has implications for your API server which supports Swagger.
  118. You can read about CORS here: http://www.w3.org/TR/cors.
  119. There are two cases where no action is needed for CORS support:
  120. 1. swagger-ui is hosted on the same server as the application itself (same host *and* port).
  121. 2. The application is located behind a proxy that enables the requires CORS headers. This may already be covered within your organization.
  122. Otherwise, CORS support needs to be enabled for:
  123. 1. Your Swagger docs. For Swagger 2.0 it's the `swagger.json` and any externally `$ref`ed docs, and for prior version it's the `Resource Listing` and `API Declaration` files.
  124. 2. For the `Try it now` button to work, CORS needs to be enabled on your API endpoints as well.
  125. ### Testing CORS Support
  126. You can verify CORS support with one of three techniques:
  127. - Curl your API and inspect the headers. For instance:
  128. ```bash
  129. $ curl -I "http://petstore.swagger.io/v2/swagger.json"
  130. HTTP/1.1 200 OK
  131. Date: Sat, 31 Jan 2015 23:05:44 GMT
  132. Access-Control-Allow-Origin: *
  133. Access-Control-Allow-Methods: GET, POST, DELETE, PUT, PATCH, OPTIONS
  134. Access-Control-Allow-Headers: Content-Type, api_key, Authorization
  135. Content-Type: application/json
  136. Content-Length: 0
  137. ```
  138. This tells us that the petstore resource listing supports OPTIONS, and the following headers: `Content-Type`, `api_key`, `Authorization`.
  139. - Try swagger-ui from your file system and look at the debug console. If CORS is not enabled, you'll see something like this:
  140. ```
  141. XMLHttpRequest cannot load http://sad.server.com/v2/api-docs. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
  142. ```
  143. Swagger-UI cannot easily show this error state.
  144. - Using the http://www.test-cors.org website. Keep in mind this will show a successful result even if `Access-Control-Allow-Headers` is not available, which is still required for Swagger-UI to function properly.
  145. ### Enabling CORS
  146. The method of enabling CORS depends on the server and/or framework you use to host your application. http://enable-cors.org provides information on how to enable CORS in some common web servers.
  147. Other servers/frameworks may provide you information on how to enable it specifically in their use case.
  148. ### CORS and Header Parameters
  149. Swagger lets you easily send headers as parameters to requests. The name of these headers *MUST* be supported in your CORS configuration as well. From our example above:
  150. ```
  151. Access-Control-Allow-Headers: Content-Type, api_key, Authorization
  152. ```
  153. Only headers with these names will be allowed to be sent by Swagger-UI.
  154. ## How to Improve It
  155. Create your own fork of [swagger-api/swagger-ui](https://github.com/swagger-api/swagger-ui)
  156. To share your changes, [submit a pull request](https://github.com/swagger-api/swagger-ui/pull/new/develop_2.0).
  157. ## Change Log
  158. Plsee see [releases](https://github.com/swagger-api/swagger-ui/releases) for change log.
  159. ## License
  160. Copyright 2011-2015 SmartBear Software
  161. Licensed under the Apache License, Version 2.0 (the "License");
  162. you may not use this file except in compliance with the License.
  163. You may obtain a copy of the License at [apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
  164. Unless required by applicable law or agreed to in writing, software
  165. distributed under the License is distributed on an "AS IS" BASIS,
  166. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  167. See the License for the specific language governing permissions and
  168. limitations under the License.