02040402
02040402

Reputation: 799

How to hide the Models section in Swagger UI?

I use Swagger UI to display API documentation. By default, it displays the "Models" section at the bottom:

enter image description here

How to hide it?

Upvotes: 29

Views: 50286

Answers (7)

EAmez
EAmez

Reputation: 895

I'm using swagger-ui-express for having my API documentation on my website and I ended up with this solution after several tries:

const docSwagger = require("./some-path/your-swagger-spec.json");
const swaggerOptions = {
    // This option is the one you're supposed to use
    defaultModelsExpandDepth: -1, 
    // Tried this one also
    defaultModelExpandDepth: -1,
    // Ended up using this option because previous ones didn't work
    customCss: '.models {display: none !important}' 
}

app.use('/your-path', swaggerUI.serve, (...args) => swaggerUI.setup(docSwagger, swaggerOptions)(...args));

Final solution came to after reading this response on another site

Upvotes: 0

Luciano Ortiz
Luciano Ortiz

Reputation: 41

Using Spring Boot:

@Bean
public UiConfiguration uiConfiguration() {
    return UiConfigurationBuilder//
            .builder()//
            .defaultModelsExpandDepth(-1)//
            .build();//
}

Upvotes: 3

PSN
PSN

Reputation: 2516

If using Django add this inside your settings.py:

SWAGGER_SETTINGS = {
    'DEFAULT_MODEL_DEPTH':-1
}

Upvotes: 1

Jaime Torner
Jaime Torner

Reputation: 331

For .Net Core 3.0 just Add c.DefaultModelsExpandDepth(-1); on your Startup.cs

// Startup.cs

app.UseSwaggerUI(c =>
{
    c.DefaultModelsExpandDepth(-1); // Disable swagger schemas at bottom
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Your API V1");
});

Upvotes: 24

ImI
ImI

Reputation: 208

In Docket bean just add new

Docket(DocumentationType.SWAGGER_2).ignoredParameterTypes(YourClass.class,YourAnother.class)

I hope its helpful

Upvotes: 5

Helen
Helen

Reputation: 97677

To hide the "Models" section, add defaultModelsExpandDepth: -1 to the Swagger UI configuration code in your index.html.

Note the option name uses plural Model*s* not Model.

// index.html

<script>
window.onload = function() {
  // Begin Swagger UI call region
  const ui = SwaggerUIBundle({
    url: "https://petstore.swagger.io/v2/swagger.json",
    dom_id: '#swagger-ui',
    defaultModelsExpandDepth: -1,   // <-------

Swagger UI also has many other configuration options that control API documentation rendering.

Upvotes: 29

numediaweb
numediaweb

Reputation: 17010

Although not the exact results you are looking for but i find it perfect to just disable the properties of the model you don't want via:

<?php
// src/AppBundle/Entity/User.php

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;

...

 * @ApiResource(
 *     attributes={
 *         "normalization_context"={"groups"={"api"}},
 *         "denormalization_context"={"groups"={"api"}}
 *     },

...

 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Groups({"api","user:read"})
     */
    protected $id;

    /**
     * @var \DateTime
     */
    private $disabledProperty;

This way you will get a model just with the props you exposed via the group api. Hope this helps someone :)

Upvotes: -2

Related Questions