doulu4233 2014-11-17 15:12
浏览 169
已采纳

轻松地显示PUT主体格式

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条回答 默认 最新

  • dongzz4545 2014-11-17 19:07
    关注

    I figured it out. In the Go code above instead of

    DataType("UserFields")
    

    I have to use

    DataType("main.UserFields")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!