swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Customizing Swagger 3.x

Open farhan-ejaz opened this issue 9 years ago • 22 comments

When reporting an issue, please provide the following details:

  • swagger-ui version 3.0.8

The documentation is not clear or old for the current version. Is there any documentation available on how to customize version 3.x

http://swagger.io/docs/swagger-tools/#customization-36

capture

Couldn't find any of the folders / css /js files as mentioned in the guide

farhan-ejaz avatar May 05 '17 11:05 farhan-ejaz

Unfortunately, there is no documentation yet, and it's not going to be immediate. We're fairly overloaded right now, but we'll do our best to put something out there.

webron avatar May 05 '17 21:05 webron

Thanks for the update Ron. Atleast any suggestion on customisation ?

I want to provide user to switch source json url.

farhan-ejaz avatar May 07 '17 06:05 farhan-ejaz

Hi @farhan-ejaz, I don't know if it's the correct way to do it, but I have a dropdown in my page (the dropdown options value are my json urls) and on the dropdown onchange event I inject the selected json url on swagger ui instance

// init swaggerUI
const ui = SwaggerUIBundle({
	url: '',
	dom_id: jqDom_id, // here change your jqDom_id
	presets: [
		SwaggerUIBundle.presets.apis,
		SwaggerUIStandalonePreset
	],
	plugins: [
		SwaggerUIBundle.plugins.DownloadUrl
	],
	layout: "StandaloneLayout",
	validatorUrl: null,
	operationsSorter: "alpha"
})
window.ui = ui

// dropdown onchange event
jQuery('.my-dropdown-jqEl').on("change", function(e) {
	var jsonDocUrl = jQuery(e.target).val();
	ui.specActions.updateUrl(jsonDocUrl);
	ui.specActions.download(jsonDocUrl);
});

I hope this can help you.

zem83 avatar May 08 '17 10:05 zem83

Hello All,

how can we call in angularJS 2 below code. const ui = SwaggerUIBundle({ url: '', dom_id: jqDom_id, // here change your jqDom_id presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout", validatorUrl: null, operationsSorter: "alpha" }) window.ui = ui

siddamreddy avatar Jun 07 '17 13:06 siddamreddy

I am importing import { SwaggerUIBundle, SwaggerUIStandalonePreset } from 'swagger-ui-dist' its getting cannot find module.

siddamreddy avatar Jun 07 '17 13:06 siddamreddy

We are also eager to customize the look & feel of swagger-ui 3 for our project. We are embedding swagger-ui within another web app, and we require a swagger-ui look & feel that works a bit better within the limited screen real estate we have available for it. Ideally we would like to re-skin swagger-ui without modifying core code so it is easy to stay up-to-date with the latest version of swagger-ui.

We are unclear what extension points have been built into swagger-ui 3, and are hoping that docs will be coming soon. Even some brief comments posted here about the extension points could be very helpful until more complete docs are available. Any advise from the developers would be much appreciated!

banders avatar Jul 04 '17 23:07 banders

@webron a quick question for customizing swagger-ui 3.*: If our front-end is not based on React, could we customize swagger-ui 3.* to render some x- attribute? I'm back-end developer and new to swagger-ui, appreciate if you could help, thanks!

hellosnow avatar Sep 03 '17 14:09 hellosnow

@hellosnow yes, you can still customize it.

webron avatar Sep 05 '17 20:09 webron

@webron thanks! We'll explore how to customize swagger-ui 3.*, if any updated documentation please let use know. 😄

hellosnow avatar Sep 06 '17 03:09 hellosnow

Hi All, I am also trying to customise the swagger ui 3.x but i am not able to inject authorization header from my external JS function. What is the configuration needed to add authorization header?

  • I need to add bearer token Please help

pravinmane85 avatar Sep 19 '17 09:09 pravinmane85

Hi for bearer token: in index.html: < input id="bearer-code-input" type="text" placeholder="Enter Bearer Token here" style="width: auto" value="yourtoken" /> then in SwaggerUIBundle constructor: const ui = SwaggerUIBundle({ ..., requestInterceptor: function (req) { req.headers = { 'Authorization': 'Bearer ' + document.getElementById('bearer-code-input').value , 'Accept': 'application/json', 'Content-Type': 'application/json' }; return req; }, ... })

xavave avatar Oct 26 '17 14:10 xavave

Good morning, do you know when the documentation will be in order to customize the ui?

zapico20 avatar Nov 16 '17 12:11 zapico20

When @shockey decides to merge his doc branch ;) In the meantime, you can take a look at https://github.com/swagger-api/swagger-ui/tree/ft/docs/docs to get you started.

webron avatar Nov 16 '17 13:11 webron

Customization documentation is now available 😄 https://github.com/swagger-api/swagger-ui#customization

Closing!

shockey avatar Dec 06 '17 04:12 shockey

@hellosnow, FYI, we just added a showExtensions option: https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md#display

shockey avatar Dec 06 '17 04:12 shockey

@webron can you please help in fixing apiKey issue. Even though i have added this , it is not taking apiKey in the request url

"security": [ { "api_key": [ "qwerty" ] } ],

"components": { "securitySchemes": { "api_key": { "type": "apiKey", "in": "query", "name": "apiKey" } } }

What should i do??

shatestest avatar Mar 02 '18 11:03 shatestest

@zem83 Can you help me, I'm trying to do something similar to be able to choose from multiples API specs when I landed on the main swagger page. Can you tell me what else I need to change here in order for it to work?

// init swaggerUI
const ui = SwaggerUIBundle({
	url: '',
	dom_id: jqDom_id, // here change your jqDom_id
	presets: [
		SwaggerUIBundle.presets.apis,
		SwaggerUIStandalonePreset
	],
	plugins: [
		SwaggerUIBundle.plugins.DownloadUrl
	],
	layout: "StandaloneLayout",
	validatorUrl: null,
	operationsSorter: "alpha"
})
window.ui = ui

// dropdown onchange event
jQuery('.my-dropdown-jqEl').on("change", function(e) {
	var jsonDocUrl = jQuery(e.target).val();
	ui.specActions.updateUrl(jsonDocUrl);
	ui.specActions.download(jsonDocUrl);
});

Where I need to define '.my-dropdown-jqEl' ?

Thanks!

edesdan avatar Apr 23 '18 10:04 edesdan

@edesdan, I have both (jqDom_id and my-dropdown-jqEl) defined into the same HTML page, like this:

<select class="apiSelector">
	<option value="path_1">/api/v1</option>
	<option value="path_2">/api/v2</option>
</select>

<div id="swagger-ui" class="custom-swagger-ui"></div>

and then into the js file I have the swaggerUI init function that point to 'swagger-ui' div (the jqDom_id in my previous example) and the jQuery function for the onChange event on the HTML 'apiSelector' select (the my-dropdown-jqEl in my previous example).

This should be enough.

zem83 avatar Apr 23 '18 10:04 zem83

@zem83 thanks for your reply. BTW at the end I decided to use the Swagger-UI 3.13.6 release. They added support for a drop-down list menu, pretty handy and very easy to configure!

// Build a system
      const ui = SwaggerUIBundle({
        urls: [{url: "https://localhost/service-api1.yaml", name: "service-api1"},
                  {url: "https://localhost/service-api2.yaml", name: "service-api2"},
                  {url: "https://localhost/service-api3.yaml", name: "service-api3"}],
        dom_id: '#swagger-ui',
        deepLinking: true,
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIStandalonePreset
        ],
        plugins: [
          SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "StandaloneLayout"
      })

      window.ui = ui

edesdan avatar Apr 24 '18 11:04 edesdan

@edesdan, yes I saw that feature (and I'm sorry because I forgot to mention it in my answer xD) but I need to use a special style for the dropdown and other HTML components outside the Swagger "dom_id" HTML block, so I used my solution :)

zem83 avatar Apr 24 '18 11:04 zem83