How to hide the Models section in Swagger UI?

前端 未结 5 1264
遇见更好的自我
遇见更好的自我 2020-12-20 17:03

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

How to hide it?

相关标签:
5条回答
  • 2020-12-20 17:35

    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 :)

    0 讨论(0)
  • 2020-12-20 17:36

    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.

    0 讨论(0)
  • 2020-12-20 17:36

    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");
    });
    
    0 讨论(0)
  • 2020-12-20 17:42

    In Docket bean just add new

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

    I hope its helpful

    0 讨论(0)
  • 2020-12-20 17:58

    If using Django add this inside your settings.py:

    SWAGGER_SETTINGS = {
        'DEFAULT_MODEL_DEPTH':-1
    }
    
    0 讨论(0)
提交回复
热议问题