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.
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.
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.
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With