swaggo / gin-swagger

gin middleware to automatically generate RESTful API documentation with Swagger 2.0.
MIT License
3.66k stars 266 forks source link

how to use swagger @Success #263

Open 371873574 opened 1 year ago

371873574 commented 1 year ago

type Response struct { Code int json:"code" Msg string json:"msg" Data interface{} json:"data" } How to declare specific structures for data,becase of defining data as an interface can improve scalability { "code": 200, "msg": "success", "data": { "total": 100, "list": [{ "name":"test", "namespace": "default" }] } }

dnsx2k commented 1 year ago

// JSONResult's data field will be overridden by the specific type proto.Order @success 200 {object} jsonresult.JSONResult{data=proto.Order} "desc"

in your case:

@Success 200 {object} Response{data=[]httpresponses.ResponseItem}

371873574 commented 1 year ago

// JSONResult's data field will be overridden by the specific type proto.Order @success 200 {object} jsonresult.JSONResult{data=proto.Order} "desc"

in your case:

@success 200 {object} Response{data=[]httpresponses.ResponseItem}

Actually, it hasn't completely solved my problem. Where is the specific document address? I'll study it

dnsx2k commented 1 year ago

Reference right here: https://github.com/swaggo/swag/blob/master/README.md#model-composition-in-response

371873574 commented 1 year ago

Reference right here: https://github.com/swaggo/swag/blob/master/README.md#model-composition-in-response

thanks!

371873574 commented 1 year ago

Reference right here: https://github.com/swaggo/swag/blob/master/README.md#model-composition-in-response

type Vo struct { Id string json:"id,omitempty" Name string json:"name,omitempty" Permission []permission.Resp json:"permission,omitempty" IsPreset uint json:"isPreset" CreateTime string json:"createTime,omitempty" UpdateTime string json:"updateTime,omitempty" }

Add does not require an ID, update requires an ID. How to use the same vo struct

putianhui commented 10 months ago

Brother, have you solved this problem? I have exactly the same problem as you.

My response structure is like this data is an interface type

type Response struct {
    Code    int         `json:"code"`
    Data    interface{} `json:"data"`
    Message string      `json:"message"`
}

It's ok for me to write like this when responding @Success 200 {object} response.Response{} "响应示例" But the example value displayed above the page is like this.

{
  "code": 0,
  "data": "string",
  "message": "string"
}

The result I want is to add an example value to data, something like this

{
  "code": 0,
  "data": {
    "token": "50e1689dbccc9b48e3c8797aabe542",
    "money": "5.800"
  },
  "message": "success"
}

I tried to use the example below to supplement the sample value of data, but when swag init it prompts :invalid type: response.Response{data=map[string]interface{}{

@Success 200 {object} response.Response{data=map[string]interface{}{"token": "1234","money":"123"}} "响应示例"