Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger doc for api response (type List)

Tags:

java

swagger

I am using swagger to generate the documentation of my REST API. However I am having problems specifying the responses of some API calls.

This is my code:

@GET
@Path("/self/activities")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get all activities created by this user",
            notes = "Returns the list that the authenticated user   (JWT) has created",
            response = Activity.class,
            responseContainer = "List")
@ApiResponses(value = {
      @ApiResponse(code = 400, response = ErrorResponse.Error.class, responseContainer = "List", message = "There was something wrong in the request and therefore could not be processed (headers, json syntax/content)"),
      @ApiResponse(code = 500, response = ErrorResponse.Error.class, responseContainer = "List", message = "Unknown Internal server error")})
public void getActivities(...){...}

And this is the doc that it generates:

"responses" : {
      "200" : {
        "description" : "successful operation",
        "schema" : {
          "type" : "array",
          "items" : {
            "$ref" : "#/definitions/Activity"
          }
        }
      },
      "400" : {
        "description" : "There was something wrong in the request and therefore could not be processed (headers, json syntax/content)",
        "schema" : {
          "$ref" : "#/definitions/Error"
        }
      },
      "500" : {
        "description" : "Unknown Internal server error",
        "schema" : {
          "$ref" : "#/definitions/Error"
        }
      }
    }

And I do not understand why the error responses are not of type 'Array' like the 200 Response. What I want is that all the responses have the same format as the 200 responses (array with items):

 "500" : {
        "description" : "Uknown interval server error",
        "schema" : {
          "type" : "array",
          "items" : {
            "$ref" : "#/definitions/Error"
          }
        }
      }

Thanks for your time.

like image 628
mtrebi Avatar asked Jul 13 '15 15:07

mtrebi


People also ask

How do I document my API with swagger?

Head over to Swagger Inspector, and insert the end point of the resource you want to have documented. You can then navigate to the right panel from the History section of Swagger Inspector, and click "Create API definition" to create the OAS definition.

How does swagger define list of strings?

Firstly, we start by specifying the array of strings in Swagger using YAML notation. In the schema section, we include type: array with items String.


1 Answers

@ApiResponse(code = 400, response = ErrorResponse.Error.class, responseContainer = "List", message = "There was something wrong in the request and therefore could not be processed (headers, json syntax/content)"),
  @ApiResponse(code = 500, response = ErrorResponse.Error.class, responseContainer = "List", message = "Unknown Internal server error")})

400 and 500 errors response will return an ErrorResponse. Not an array.

like image 131
Squalex Avatar answered Sep 21 '22 05:09

Squalex