swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.68k stars 1.2k forks source link

how to show local images in description in swaggo #1660

Open minbala opened 1 year ago

minbala commented 1 year ago

I want to show some local images in description.

rozman50 commented 1 year ago

I managed by embedding image project and exposing API endpoint (I am using Gin), which then reads from embedded image.

I created following folder structure

  static
  ├── main.go
  └── images
          └── image1.png

In static/main.go:

package static
import "embed"

//go:embed *
var Files embed.FS

In function, which gets called by gin. Note that ReadFile will return byte array.

func (a *Controller) getSwaggerLocalFile(c *gin.Context) {
  b, err = static.Files.ReadFile("images/image1.png")
  if err != nil {
      return
  }

  c.Data(http.StatusOK, "image", b) // <- if you have any type other than image, you'll have to detect ContentType on your own
}

In Swagger documentation:

// My API route is called api/swagger_local_file/image1.png, where image1.png is image name which I'd like to show in swagger. In code, which reads from static.Files.ReadFile I hardcoded this parameter
// @Description ![Image 1 Example](api/swagger_local_file/image1.png)