Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

explorer select resets to first option upon refresh #8233

Closed
talkohavy opened this issue Oct 15, 2022 · 1 comment
Closed

explorer select resets to first option upon refresh #8233

talkohavy opened this issue Oct 15, 2022 · 1 comment

Comments

@talkohavy
Copy link

Summary

I opened an issue for swagger-ui-express's maintainer, and he replied with:

I think this issue is with the Swagger UI and would be best placed fixed by them. Happy to take a PR to fix in this module but feel it would he best place fixed in Swagger UI therefore its fixed across the board.

The original issue I place over there

What I am using:

I'm using the explorer's select in the topbar to view several swagger docs.
The select is working great, and upon clicking on each it switches to the relevant url that hosts the desired swagger json.

What is the problem:

The problem starts when I refresh the page.
Let's say I have 3 swagger schemas/options in the dropdown:

  • All
  • Images
  • Users

presented just as in the above order. "All" is first, "Images" is second, "Users" is third.
Let's say i'm now on Users schema, so my url looks like this:

http://localhost:5000/api-docs/?urls.primaryName=Users

If I were to refresh the page at this point, the url would stay the same, however the select would revert back to the first option ("All"), and the schema to be loaded would match that first option!
The Question is:
How can I make it so that upon refresh, the select will preserve my current selected option and load the swagger json that matches?
I know that I can pass a custom.js file, and I could probably implement a waitForSelector function to wait for the "select" id, and then do a programmatic click() somehow, but it sounds like too much of a work-around, and this seems like an operation that should be given out-of-the-box by swagger-ui-express, isn't it?
Could you please implement some logic that uses the "urls.primaryName" query param to load a swagger json based on its value? 🙏🏼

Additional information:

This is how I am using swagger-ui-express:

server.js

// Step 1: import swagger's extra options
import { swaggerExtraOptions } from './swagger';
// Step 2: host all swagger.json files (i.e. all.json, images.json, users.json)
app.use('/api-docs', express.static('./src/swagger'));
// Step 3: basic swagger-ui-express usage according to the documentation
app.use(
    '/api-docs',
    swaggerUi.serve,
    swaggerUi.setup(null, swaggerExtraOptions)
  );

swaggerExtraOptions object

const swaggerExtraOptions = {
    // customJs: './custom.js', // I'd prefer not to use it if possible...
    explorer: true,
    swaggerOptions: { 
        urls: [
            {
                name: "All",
                url: "http://localhost:5000/api-docs/configs/all.json",
            },
            {
                name: "Images",
                url: "http://localhost:5000/api-docs/configs/images.json",
            },
            {
                name: "Users",
                url: "http://localhost:5000/api-docs/configs/users.json",
            }
        ]
    },
  };
@talkohavy
Copy link
Author

Never mind,
this issue can be closed.
I see now you solved it just recently.

Reference here:
scottie1984/swagger-ui-express#307 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant