Thanks Ron.
Our project uses Jersey, models are annotated with jaxb annotations and not jackson annotations. I wonder if this matters?
Also I'd like to confirm I'm doing it right about setting up the servlet. We already have a apiServlet that serves the API. It currently has all the resource classes as providers. To serve swagger.json, I added a new servlet (swaggerServlet) parallel to the current apiServlet, and added all resource classes as well as the ApiListingResource and SwaggerSerializer classes to providers. Is this understanding correct?
If my understanding is correct, I am wondering if the two servlets can potentially be combined. Right now the answer is no because ApiListingResource maps to path "/", which seems to cause a conflect. /swagger.json returns 404 when I just added the two swagger classes to the apiServlet. Other than this, is there any other reason why we need a new servlet just to serve swagger.json and swagger.yaml?
Back to my original question of how Models are generated, I created a sample project with a Model class (which has String name variable) extending a BaseModel calsss (which has String id and String type.) I then created two APIs, one returns List<Model>, one returns Model.
@ApiOperation(value = "Get models", notes = "Returns a model list")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid request") })
@GET
@Path("")
@Produces(MediaType.APPLICATION_JSON)
public List<Model> getAll() {
List<Model> models = Arrays.<Model>asList(new Model("myId1", "myType1", "myName1"), new Model("myId2", "myType2", "myName2"));
return models;
}
@ApiOperation(value = "Get model", notes = "Returns a model object")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid request") })
@GET
@Path("/1")
@Produces(MediaType.APPLICATION_JSON)
public Model getOne() {
return new Model("myId", "myType", "myName");
}
In the generated swagger.json, I see an issue with the List<Model> API:
"paths": {
"/model": {
"get": {
"tags": [
"apimodel"
],
"summary": "Get models",
"description": "Returns a model list",
"operationId": "getAll",
"produces": [
"application/json"
],
"parameters": [],
"responses": {
"200": {
"description": "successful operation",
"schema": {
"type": "array",
"items": {
"type": "object"
}
}
},
"400": {
"description": "Invalid request"
}
}
}
},
Note the "schema" section which is an array of type "object", and not an array of Model.
For comparison, the /model/1 API that is supposed to return a single Model object does the right thing by referencing #/definitions/Model defined in the definitions section:
Model model {
id (string): ID of the model,
type (string): Type of the model,
name (string, optional): Name of the model,
description (string, optional)
}
I wonder what I'm missing that causes the Model to be generated correctly, but not List<Model>?
Thanks again!
Jack