Skip to content
This repository has been archived by the owner on Jul 30, 2023. It is now read-only.

SwaggerDoc doesn't work #2

Open
taget opened this issue Mar 8, 2017 · 6 comments
Open

SwaggerDoc doesn't work #2

taget opened this issue Mar 8, 2017 · 6 comments

Comments

@taget
Copy link

taget commented Mar 8, 2017

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
?

@emicklei
Copy link
Owner

emicklei commented Mar 8, 2017 via email

@taget
Copy link
Author

taget commented Mar 14, 2017

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": ""
}
}

@emicklei
Copy link
Owner

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

@taget
Copy link
Author

taget commented Mar 15, 2017

{
"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"
}
}
}

@emicklei
Copy link
Owner

SwaggerDoc must be defined on the struct, not the Resource

@emicklei
Copy link
Owner

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

gorexlv pushed a commit to gorexlv/go-restful-swagger20 that referenced this issue Aug 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants