go - How to display multiple HTTP 200 Response Samples on Swagger-UI using Swaggoswag - Stack Overflow

I want to display three possible response samples on Swagger-UI for HTTP status code 200.Here's w

I want to display three possible response samples on Swagger-UI for HTTP status code 200.

Here's what I have tried so far:

  1. Using multiple @Success annotations above the function.
  2. Referring to the Swaggo/swag documentation and attempting to use a Markdown file with @x-codeSample.
  3. Asking ChatGPT for guidance.
File struct
├─api
│  └─backend
│      ├─controller
|      |     |─controller //func is declare here 
|      |
│      ├─middleware
│      └─router
├─cmd
│  └─api
|     |──main.go
|
├─docs
|  |─docs.go
|  |─swagger.json
|  |─swagger.yaml
|  |─Get signature records.md
...
# go.mod
    github/swaggo/files v1.0.1
    github/swaggo/gin-swagger v1.6.0
    github/swaggo/swag v1.16.3

# CLI command to generate swagger file
    swag init -g cmd/api/main.go -o ./docs
# Response model of API

    package model

    type GetDeliverySignatureResp struct {
        SignatureTypeId    int       `json:"signatureTypeId"`
            ...
    }
    type GetDeliverySignatureRespQrcode struct {
        SignatureTypeId    int       `json:"signatureTypeId"  example:"1"`
            ...
    }
    type GetDeliverySignatureRespManual struct {
        SignatureTypeId    int       `json:"signatureTypeId"  example:"2"`
            ...
    }
    type GetDeliverySignatureRespNoContact struct {
        SignatureTypeId    int       `json:"signatureTypeId"  example:"3"`
            ...
    }
# gin handler function

    package controller

    // ApiGetSignatureRecords
    // @Summary Get signature records
    // @Description 1.QrCode, 2.Manual, 3.NoContact
    // @Tags Fleet
    // @Param deliveryId path string true "deliveryId"
    // @Param Token header string true "JWT"
    // @Success 200 {object} model.GetDeliverySignatureRespQrcode "QR Code"
    // @Success 200 {object} model.GetDeliverySignatureRespManual "Manual"
    // @Success 200 {object} model.GetDeliverySignatureRespNoContact "No Contact"
    // @Router /api/{deliveryId} [GET]
    func (f *fleetController) ApiGetSignatureRecords(c *gin.Context) {
      ...
    }

Despite these efforts, I couldn't find a working example of using an external file, and my attempts with annotations, Markdown, and raw JSON have not been successful.

Could someone provide clear instructions or an example to achieve this?

I prefer using a JSON file or Markdown file to define and present the different responses, instead of directly defining structs in the code, if possible.

I want to display three possible response samples on Swagger-UI for HTTP status code 200.

Here's what I have tried so far:

  1. Using multiple @Success annotations above the function.
  2. Referring to the Swaggo/swag documentation and attempting to use a Markdown file with @x-codeSample.
  3. Asking ChatGPT for guidance.
File struct
├─api
│  └─backend
│      ├─controller
|      |     |─controller //func is declare here 
|      |
│      ├─middleware
│      └─router
├─cmd
│  └─api
|     |──main.go
|
├─docs
|  |─docs.go
|  |─swagger.json
|  |─swagger.yaml
|  |─Get signature records.md
...
# go.mod
    github/swaggo/files v1.0.1
    github/swaggo/gin-swagger v1.6.0
    github/swaggo/swag v1.16.3

# CLI command to generate swagger file
    swag init -g cmd/api/main.go -o ./docs
# Response model of API

    package model

    type GetDeliverySignatureResp struct {
        SignatureTypeId    int       `json:"signatureTypeId"`
            ...
    }
    type GetDeliverySignatureRespQrcode struct {
        SignatureTypeId    int       `json:"signatureTypeId"  example:"1"`
            ...
    }
    type GetDeliverySignatureRespManual struct {
        SignatureTypeId    int       `json:"signatureTypeId"  example:"2"`
            ...
    }
    type GetDeliverySignatureRespNoContact struct {
        SignatureTypeId    int       `json:"signatureTypeId"  example:"3"`
            ...
    }
# gin handler function

    package controller

    // ApiGetSignatureRecords
    // @Summary Get signature records
    // @Description 1.QrCode, 2.Manual, 3.NoContact
    // @Tags Fleet
    // @Param deliveryId path string true "deliveryId"
    // @Param Token header string true "JWT"
    // @Success 200 {object} model.GetDeliverySignatureRespQrcode "QR Code"
    // @Success 200 {object} model.GetDeliverySignatureRespManual "Manual"
    // @Success 200 {object} model.GetDeliverySignatureRespNoContact "No Contact"
    // @Router /api/{deliveryId} [GET]
    func (f *fleetController) ApiGetSignatureRecords(c *gin.Context) {
      ...
    }

Despite these efforts, I couldn't find a working example of using an external file, and my attempts with annotations, Markdown, and raw JSON have not been successful.

Could someone provide clear instructions or an example to achieve this?

I prefer using a JSON file or Markdown file to define and present the different responses, instead of directly defining structs in the code, if possible.

Share Improve this question asked Nov 19, 2024 at 2:10 陳禮佑陳禮佑 234 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

unfortunately, it seems that is not possible in the current version of the swaggo/swag that use Swagger 2.0 (see here).

This feature already implemented in the v2 branch of that package, which will use OpenAPI 3.0 (the merged pull request).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745586145a4634550.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信