Swagger UI configuration with swagger-config.yaml
Asked Answered
D

2

24

As per swagger documentation,

Swagger-UI accepts configuration parameters in four locations.

From lowest to highest precedence:

  1. The swagger-config.yaml in the project root directory, if it exists, is baked into the application
  2. configuration object passed as an argument to Swagger-UI (SwaggerUI({ ... }))
  3. configuration document fetched from a specified configUrl
  4. configuration items passed as key/value pairs in the URL query string

I have tried to put swagger-config.yaml in root pat of application but its not working.

I have followed swagger Installation steps and its working correct. but steps for swagger custom config is not working. I have kept files as below,

 swagger-ui
   |--swagger-config.yaml
   |--index.html

swagger-config.yaml

url: "https://petstore.swagger.io/v2/swagger.json"
dom_id: "#swagger-ui"
validatorUrl: "https://online.swagger.io/validator"
oauth2RedirectUrl: "http://localhost:3200/oauth2-redirect.html"

index.html

// Begin Swagger UI call region
      const ui = SwaggerUIBundle({
        //url: "https://petstore.swagger.io/v2/swagger.json",
        //dom_id: '#swagger-ui',
        deepLinking: true,
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIStandalonePreset
        ],
        plugins: [
          SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "StandaloneLayout"
      })

Any idea if I am missing anything ?

Dania answered 21/11, 2018 at 14:57 Comment(6)
How do you run Swagger UI - open the page from the file system, or from a web server, or using npm start?Anabatic
I tried it on JBoss as well as file system.Dania
You say it is not working. Could you please elaborate? When you look at your web browser's console, do you see any error messages? When you look at your browser's network console, do you see any successful attempts to download petstore.swagger.io/v2/swagger.json?Damara
@MahendraKapadne did you solve your problem? I'm running into the exact same issue - swagger-ui does not pick up my swagger-config.yaml I put into the swagger-ui root directory.Waterborne
@Waterborne For time being, I had hard coded relative URI in url.Dania
@MahendraKapadne - could you elaborate on how you hard-coded the relative URI in the url?Tierratiersten
P
5

As mentioned in this Github's issue thread now only .json files are accepted for conf. Probably too late but it can help looking for the answer at November 2021.

Pierian answered 22/11, 2021 at 21:54 Comment(1)
I still had to add the swagger-config.json as configUrl parameter, but it works once this is done.Gendron
S
3

I also have this issue. From the document, it seems we don't need to config anything in index.html if use swagger-config.xml, actually, it doesn't work from my side, I have not find the reason. But if use configUrl instead, it works.

// Begin Swagger UI call region
const ui = SwaggerUIBundle({
  //url: "https://petstore.swagger.io/v2/swagger.json",
  //dom_id: '#swagger-ui',
  configUrl: "../swagger-config.yaml",
  deepLinking: true,
  presets: [
    SwaggerUIBundle.presets.apis,
    SwaggerUIStandalonePreset
  ],
  plugins: [
    SwaggerUIBundle.plugins.DownloadUrl
  ],
  layout: "StandaloneLayout"
})

And it can be configured to support the array.

---
urls:
 - url: "https://petstore.swagger.io/v2/swagger.json"
   name: "url1"
   
 - url: "https://petstore.swagger.io/v2/swagger.json"
   name: "url2"
Shigella answered 8/1, 2021 at 2:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.