scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.41k stars 225 forks source link

Respect urls.primaryName query parameter on initial requests #300

Open SAJLinders opened 2 years ago

SAJLinders commented 2 years ago

Background:

As a part of my job I work on a distributed web application containing of several (micro-) services. We have a Swagger file for every one of these services and store them in a raw repository. I have deployed a NodeJS project using swagger-ui-express to easily visualize the various API definitions from the raw repository.

A simplified example of how this is done can be seen below:

const repository = require('./repository')
const swaggerUi = require('swagger-ui-express');

app.use('/', swaggerUi.serve);
app.get('/', (req, res) => {
    repository.getAllArtefacts().then(artefacts => {
        output = swaggerUi.setup(null, {
            explorer: true,
            swaggerOptions: {
                urls: artefacts
            }
        })
        return output(req, res)
    })
})

Problem:

When a link to a specific definition is shared to someone in the team, the query parameter is not respected. Imagine I wanted to share the URL to the following definition:

http:///?urls.primaryName=juice-shop

Regardless of the value of urls.primaryName, the default definition (being the first in the list) would always be rendered. Ideally, the value of this query parameter would be used to determine which definition should be displayed.

Hacky fix:

Because the first element in the swaggeroptions.urls array is used to determine which definition should be loaded first, I extracted the urls.primaryName parameter from the URL by hand. And then if I find a definition matching that name, I move it to the front of the array.

scottie1984 commented 2 years ago

Guess this is related to this issue: https://github.com/swagger-api/swagger-ui/issues/7835

unglaublicherdude commented 1 year ago

I just tested it locally. I got it working with setting DEEP_LINKING=true But only in combination with QUERY_CONFIG_ENABLED=true