swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.58k stars 8.96k forks source link

Expand all Models at once #6494

Open shrirambalaji opened 4 years ago

shrirambalaji commented 4 years ago

Content & configuration

Swagger-UI configuration options:

    SwaggerUI({
      spec,
      domNode: swaggerRef.current,
      defaultModelsExpandDepth: 10,
      defaultModelExpandDepth: 10,
      docExpansion: 'list',
      plugins: swaggerPlugins,
      showExtensions: true,
    });

Is your feature request related to a problem?

Users have to expand each schema under the "Model" section individually.

Describe the solution you'd like

Having an "Expand All" button to expand all schemas by default.

Describe alternatives you've considered

The defaultModelsExpandDepth seems to only work at the top level to expand the models, and doesn't seem to have any effect on expanding the individual models itself. The only significant change I see is when I set it to 0 the Models section is collapsed, and -1 hides it.

hkosova commented 3 years ago

Workaround (sort of): open the browser console and run:

document.querySelectorAll("#swagger-ui section.models button.model-box-control").forEach(btn => btn.click())

This will "click" all models in the "Models" section to expand them.

oshliaer commented 2 years ago

If I need to open all trees for the default UI

  (() => {
    const i = setInterval(() => {
      [...document.querySelectorAll('[aria-expanded="false"]')].map((el) =>
        el.click()
      ).length || clearInterval(i);
    }, 1000);
  })();

This can expand very deep trees.

leafOfTree commented 2 years ago

Workaround (sort of): open the browser console and run:

document.querySelectorAll("#swagger-ui section.models button.model-box-control").forEach(btn => btn.click())

This will "click" all models in the "Models" section to expand them.

You can put it in onComplete if you want to expand all models when Swagger UI has finished rendering a newly provided definition.

SwaggerUI({
    // ...
    onComplete: () => {
      document.querySelectorAll("#swagger-ui section.models button.model-box-control").forEach(btn => btn.click())
    }

})
zlr-raja commented 1 year ago

Expend all tabs Add the below key: value docExpansion: 'full', image

DarkNami commented 10 months ago

I wrote a help function to nested expand schema.

SwaggerUI({
    // ...
    onComplete: () => {
        document.addEventListener('click', (event) => {
            const target = event.target;
            const expandNested = (rootNode) => {
                if (!(rootNode instanceof Element)) return;
                const i = setInterval(() => {
                    const btns = rootNode.querySelectorAll('button.model-box-control[aria-expanded="false"]');
                    if (btns.length) {
                        btns.forEach((el) => el.click());
                    } else {
                        clearInterval(i);
                    }
                }, 50);
            };
            if (event.shiftKey && target.matches('li.tabitem:last-child > button.tablinks')) {
                expandNested(target.closest('div.model-example')?.querySelector('div[data-name="modelPanel"]'));
            }
            if (event.shiftKey && target.matches('span.model-toggle')) {
                if (!target.classList.contains('collapsed')) expandNested(target.closest('span.model'));
            }
        });
        document.querySelectorAll('section.models button.model-box-control').forEach((btn) => btn.click());
    }
});

圖片

raimondsL commented 6 months ago

I was trying to create a logic in UI, but f* it. Ended up with a loop to expand sections continuously:

(() => {
  const i = setInterval(() => {
    [...document.querySelectorAll('.opblock.is-open button[aria-expanded="false"]')].map(el => el.click())
  }, 300)
})()

Or add event to F2 key:

document.addEventListener('keydown', function(event) {
    if (event.keyCode === 113) {
        const i = setInterval(() => {
            [...document.querySelectorAll('.opblock.is-open button[aria-expanded="false"]')].map(el => el.click()).length || clearInterval(i)
        }, 100)
    }
})