Search code examples
goswaggergo-restful

Displaying PUT body format with go-restful and swagger


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.


Solution

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

    DataType("UserFields")
    

    I have to use

    DataType("main.UserFields")