emicklei / go-restful-swagger12

Swagger 1.2 extension to the go-restful package

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SwaggerDoc doesn't work

taget opened this issue · comments

I have follow codes:
type Cpuinfo struct {
Id string json:"socket_id,omitempty"
Cpus string json:"cpus,omitempty"
}
...

and defined:

func (cpuinfo CpuinfoResource) SwaggerDoc() map[string]string {
return map[string]string{
"": "Cpuinfo doc",
"socket_id": "ID of physical CPU socket",
"cpus": "Cpu list which sits on this socket",
}
}

but seems the doc is not generated at all
http://localhost:8081/apidocs.json
{
"swaggerVersion": "1.2",
"apis": [
{
"path": "/v1/cpuinfo",
"description": "Show the cupinfo of a host."
}
],
"apiVersion": "1.0",
"info": {
"title": "",
"description": ""
}
}

doesn't find anything doc information in swagger UI

Am I missing something
screen shot 2017-03-08 at 11 34 32 am
?

Yes, it does.

type Cpuinfo struct {
Id string json:"socket_id,omitempty"
Cpus string json:"cpus,omitempty"
}

func (cpuinfo CpuinfoResource) Register(container *restful.Container) {
ws := new(restful.WebService)
ws.
Path("/v1/cpuinfo").
Doc("Show the cup information of a host.").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON)

ws.Route(ws.GET("/").To(cpuinfo.getCpuinfo).
	Doc("get cpuinfo").
	Operation("getCpuinfo").
	Writes(Cpuinfo{}))

ws.Route(ws.GET("/{socket-id}").To(cpuinfo.getSocketId).
	Doc("get cpuinfo per socket id").
	Param(ws.PathParameter("socket-id", "indentifier for a CPU socket").DataType("string")).
	Operation("getSocketId").
	Writes(Cpuinfo{}))

container.Add(ws)

}

// GET http://localhost:8081/cpuinfo/
func (cpuinfo CpuinfoResource) getCpuinfo(request *restful.Request, response *restful.Response) {

log.Printf("Received Request: %s", request.PathParameter("socket_id"))

res := make(map[string]Cpuinfo)

info := new(Cpuinfo)
info.Id = "1"

res["socket"] = *info

response.WriteEntity(res)

}

// GET http://localhost:8081/cpuinfo/{socket_id}

func (cpuinfo CpuinfoResource) getSocketId(request *restful.Request, response *restful.Response) {

log.Printf("In get socket id, received Request: %s", request.PathParameter("socket-id"))

info := new(Cpuinfo)
info.Id = "1"

response.WriteEntity(info)

}

The apidocs.json is:

{
"swaggerVersion": "1.2",
"apis": [
{
"path": "/v1/cpuinfo",
"description": "Show the cup information of a host."
},
{
"path": "/v1/cache/l2/usage",
"description": "Show the level 2 cache usage of specific processes."
}
],
"apiVersion": "1.0",
"info": {
"title": "",
"description": ""
}
}

what does /apidocs.json/v1/cpuinfo show ?

{
"swaggerVersion": "1.2",
"apiVersion": "",
"basePath": "http://localhost:8081",
"resourcePath": "/v1/cpuinfo",
"info": {
"title": "",
"description": ""
},
"apis": [
{
"path": "/v1/cpuinfo",
"description": "Show the cup information of a host.",
"operations": [
{
"type": "v1.Cpuinfo",
"method": "GET",
"summary": "get cpuinfo",
"nickname": "getCpuinfo",
"parameters": [],
"produces": [
"application/json"
],
"consumes": [
"application/json"
]
}
]
},
{
"path": "/v1/cpuinfo/{socket-id}",
"description": "Show the cup information of a host.",
"operations": [
{
"type": "v1.Cpuinfo",
"method": "GET",
"summary": "get cpuinfo per socket id",
"nickname": "getSocketId",
"parameters": [
{
"type": "string",
"paramType": "path",
"name": "socket-id",
"description": "indentifier for a CPU socket",
"required": true,
"allowMultiple": false
}
],
"produces": [
"application/json"
],
"consumes": [
"application/json"
]
}
]
}
],
"models": {
"v1.Cpuinfo": {
"id": "v1.Cpuinfo",
"properties": {
"socket_id": {
"type": "string"
},
"cpus": {
"type": "string"
}
}
}

SwaggerDoc must be defined on the struct, not the Resource

were you able to fix this? can this be closed?