Forum Discussion

wichers's avatar
wichers
Occasional Contributor
5 years ago

How to suppress requestBody generation in OpenAPI spec using swagger-maven-plugin

I'm using the swagger-maven-plugin in a Java application with an old school servlet implementation. I'm trying to follow the examples here: https://github.com/swagger-api/swagger-samples/, but unfortunately this one: https://github.com/swagger-api/swagger-samples/blob/master/java/java-servlet/src/main/java/io/swagger/sample/servlet/SampleServlet.java which is the closest match, uses the 1.x version of swagger.core, not 2.x.

 

I found other examples that sort of work (see what I did below), except that it generates an almost 950 line requestBody element in the openapi.yaml doc for this endpoint. Why does it do that? Is there a way to suppress that? Below is the maven import and the code snippet:

 

<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>2.1.2</version>

 

The code with annotations is:

 @Path("/Test00020")

 public class Test00020 extends HttpServlet {

 

         @Override

         @GET

         @Operation (

                 summary = "Ask something, get something back.",

                 parameters = { @Parameter(in = ParameterIn.QUERY, name="TestParam00020", required = true ) }

         )

         public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ...

 

Thanks, Dave

  • you can hide any parameter including request body via @Parameter.hidden annotation, like:

     

        @Override
        @GET
        @Operation (
            summary = "Ask something, get something back.",
            parameters = { @Parameter(in = ParameterIn.QUERY, name="TestParam00020", required = true ) }
        )
        public void doGet(@Parameter(hidden = true) HttpServletRequest request, @Parameter(hidden = true) HttpServletResponse response) throws ServletException, IOException {
        }
  • Did you apply the annotation also to the response? and are you using the latest version?

     

    I suspect you didn't hide the response and you're seeing that in your outcome. As mentioned, the reader is targeting JAX-RS methods, therefore it tries to resolve method parameters if not hidden, and response is a method parameter.

     

    I tested with the code above and request body is correctly not resolved

  • Hi Dave,

     

    "plain" servlets are not supported in 2.x version of swagger-core, as they are in 1.x version.

     

    You are probably getting a result anyway because you are using JAX-RS annotations (e.g. `@GET`), which "marks" the servlet method as processable by swagger-core. 

     

    In your case I guess swagger-core is processing the request and response as parameters/request body which is clearly not what you want; you can add annotations (swagger-core 2.x ones) to specify parameters, request bodies and responses yourself defining exactly what you need (see swagger-core wiki and swagger-samples branch `2.0`).

     

    • wichers's avatar
      wichers
      Occasional Contributor

      In the case of a normal GET request there is NO requestBody at all. I see how you can create a custom requestBody, per your comment: "you can add annotations (swagger-core 2.x ones) to specify parameters, request bodies and responses yourself defining exactly what you need (see swagger-core wiki and swagger-samples branch `2.0`)". But I see no way to completely suppress it. That's what I'm asking, how do you use swagger-core 2.0 annotations to completely supporess the requestBody. If there isn't any way to do that, maybe create a new feature request?

      • frantuma's avatar
        frantuma
        Staff

        you can hide any parameter including request body via @Parameter.hidden annotation, like:

         

            @Override
            @GET
            @Operation (
                summary = "Ask something, get something back.",
                parameters = { @Parameter(in = ParameterIn.QUERY, name="TestParam00020", required = true ) }
            )
            public void doGet(@Parameter(hidden = true) HttpServletRequest request, @Parameter(hidden = true) HttpServletResponse response) throws ServletException, IOException {
            }
    • wichers's avatar
      wichers
      Occasional Contributor

      A question related to your comment: ""plain" servlets are not supported in 2.x version of swagger-core, as they are in 1.x version."  Is 1.x of swagger-core going to be updated so you can (optionally?) use it on plain servlets to generate a openapi v3.0 compatible spec?  I see that the 1.x branch is still being maintained. Given that it is, seems like having the ability to generate a 3.0 compatible spec in that branch would be desireable. Esp. for someone like me trying to generate openapi docs for old code.

      • frantuma's avatar
        frantuma
        Staff

        Answer is nope, servlet support can be added to the master 2.x version, to produce OpenAPI 3.0, but 1.x will always be producing only Swagger/OpenAPI 2.0