Search code examples
javaspring-bootswagger-uiopenapispringdoc-openapi-ui

How to hide "Schema" from "response" and "Request body" using OpenAPI 3 in Spring Boot?


Is there any way to hide Schema from the Responses and Request body parts? We only need to show Example Value. We use OpenAPI 3.

Dependency:

<dependency>
   <groupId>org.springdoc</groupId>
   <artifactId>springdoc-openapi-ui</artifactId>
   <version>1.6.9</version>
</dependency>

We can hide listed schema part by using springdoc.swagger-ui.defaultModelsExpandDepth=-1 in application.properties file.

enter image description here

but we want to remove the API schema part from Request Body and Responses.

enter image description here

I tried content= @Content(schema = @Schema(hidden = true )) but it hides whole request body/Response.

enter image description here

Code for Response:

@ApiResponses({
            @ApiResponse(responseCode = "200", content = @Content(schema = @Schema(name = "Success response", example = "JsonResponse..."),
                    mediaType = MediaType.APPLICATION_JSON_VALUE)),
            @ApiResponse(responseCode = "400", description = "BAD REQUEST", content = @Content(schema = @Schema(hidden = true))) 
    })

Code for Request Body:

@io.swagger.v3.oas.annotations.parameters.RequestBody(
            content= @Content(schema = @Schema(example="JsonRequestBody...")))

Can anyone please suggest how we can do that?

UPDATE:

We can hide the Schema part from the response like below.

@ApiResponse(responseCode = IConstants.R_str_200, content = @Content(examples=
@ExampleObject(name="SUCCESS RESPONSE",value="Json response..."),
                mediaType = IConstants.MEDIA_JSONVALUE))

enter image description here

but still can't able to hide Schema part from Request Body.


Solution

  • I don't think this can be solved using annotations.

    You can predefine swagger css to hide the element you want.

    To achieve that, first check which version of swagger-ui are you using. In my case it's 3.25.0. You can check which version you are using by going to External Libraries folder (if you use InteliJ) and you should find it there ( see picture below)

    enter image description here

    Then, write controller class like this :

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.nio.charset.StandardCharsets;
    import java.util.stream.Collectors;
    
    @RestController
    @RequestMapping(path = "/swagger-ui")
    public class SwaggerController {
        @GetMapping(path = "/swagger-ui.css", produces = "text/css")
        public String getCss() {
            String orig = toText(getClass().getResourceAsStream("/META-INF/resources/webjars/swagger-ui/3.25.0/swagger-ui.css"));
            String customCss = "li.tabitem.active {\n" +
                    "    display:block !important;\n" +
                    "}\n" +
                    "li.tabitem {\n" +
                    "    display:none !important;\n" +
                    "}}";
            return  orig+customCss;
        }
    
    
        static String toText(InputStream in) {
            return new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))
                    .lines().collect(Collectors.joining("\n"));
        }
    }
    

    The endpoint of this controller will be called when the css is loaded. In essence, the loading css is intercepted here and a custom css is added to hide the element you want.

    With this change, when you start the application and go to the endpoint to view the swagger documentation you should see UI as in the picture below :

    enter image description here