2

The Jsend protocol is a simple 'standard' of how to format json responses in a REST API. https://github.com/omniti-labs/jsend

I am generating Swagger documentation using https://github.com/swaggo/swag but am having great trouble working out how to use the declarative comment format to describe Jsend responses.

If anyone has done this, I would greatly appreciate an example snippet of how they defined the jsend response using the swag declarative comment format.

richp10
  • 820
  • 8
  • 20

1 Answers1

0

I solved this by switching to goswagger.io which had easier to handle syntax. These are the models used for the basic jsend types. For other responses I replaced the Data element with the name of the relevent struct and swagger did the rest.

// Success: no response data required
// swagger:response responseSuccess
type responseSuccess struct {
    // in: body
    Body struct {
        // enum: success
        Status string      `json:"status"`
        Data   interface{} `json:"data"`
    } `json:"body"`
}

// Error: Incorrect use of the API or the requested data is not available
// swagger:response responseError
type responseError struct {
    // in: body
    Body struct {
        // enum: error
        Status  string      `json:"status"`
        Data    interface{} `json:"data"`
        Message string      `json:"message"`
    } `json:"body"`
}

// Fail: Backend or system failure.
// swagger:response responseFail
type responseFail struct {
    // in: body
    Body struct {
        // enum: fail
        Status  string      `json:"status"`
        Data    interface{} `json:"data"`
        Message string      `json:"message"`
    } `json:"body"`
}
richp10
  • 820
  • 8
  • 20