英文:
Displaying PUT body format with go-restful and swagger
问题
我正在使用go-restful和swagger生成apidocs,效果很好。我遇到的问题是,当我向文档中添加一个body参数时,我希望能指定数据类型和格式。我可以指定数据类型(例如UserFields),但是JSON的格式在Swagger UI中不显示,这可能非常方便。
以下是我所说的示例:
下面的链接显示了一个body参数和相应的JSON/模型http://petstore.swagger.wordnik.com/#!/store/placeOrder
在我的情况下,JSON/模型缺失,只显示了数据类型http://ibounce.co:8282/apidocs/#!/users/PutUserField
以下是生成此特定端点文档的示例Go代码。
ws.Route(ws.PUT("/{id}/fields").
To(PutUserField).
Doc("Update user fields").
Operation("PutUserField").
Param(ws.HeaderParameter("Authorization", "username and password").DataType("string")).
Param(ws.PathParameter("id", "identifier of the user").DataType("int")).
Param(ws.BodyParameter("body", "identifier of the user").DataType("UserFields")).
Returns(http.StatusOK, http.StatusText(http.StatusOK), User{}).
Returns(http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized), ApiError{}).
Returns(http.StatusBadRequest, http.StatusText(http.StatusBadRequest), ApiError{}))
UserFields是一个结构体:
type UserFields struct {
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
URL string `json:"url,omitempty"`
Address string `json:"address,omitempty"`
}
欢迎提出任何建议。
英文:
I am using go-restful and swagger to generate apidocs which is working great. The problem I am facing is that when I add a body parameter to the documentation I would like to specify the DataType and its format. I can specify the DataType (i.e. UserFields), but the format of the JSON does not show in the Swagger UI, which can be very convenient.
Here is an example of what I am talking about:
The following link shows a body parameter and the corresponding JSON/model next to it http://petstore.swagger.wordnik.com/#!/store/placeOrder
In my case, the JSON/model is missing and only the DataType is displayed http://ibounce.co:8282/apidocs/#!/users/PutUserField
Here is the sample Go code is generates the documentation for this specific endpoint.
ws.Route(ws.PUT("/{id}/fields").
To(PutUserField).
Doc("Update user fields").
Operation("PutUserField").
Param(ws.HeaderParameter("Authorization", "username and password").DataType("string")).
Param(ws.PathParameter("id", "identifier of the user").DataType("int")).
Param(ws.BodyParameter("body", "identifier of the user").DataType("UserFields")).
Returns(http.StatusOK, http.StatusText(http.StatusOK), User{}).
Returns(http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized), ApiError{}).
Returns(http.StatusBadRequest, http.StatusText(http.StatusBadRequest), ApiError{}))
UserFields is a struct:
type UserFields struct {
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
URL string `json:"url,omitempty"`
Address string `json:"address,omitempty"`
}
Any suggestions will be appreciated.
答案1
得分: 1
我明白了。在上面的Go代码中,我需要使用
DataType("main.UserFields")
而不是
DataType("UserFields")
英文:
I figured it out. In the Go code above instead of
DataType("UserFields")
I have to use
DataType("main.UserFields")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论