-
Notifications
You must be signed in to change notification settings - Fork 56
Closed
Description
I would like to generate the OpenAPI specification for the endpoint that receives the request body as multipart/form-data. Is this possible?
I'm pasting my attempts below, but all of them resulted in the request body to be documented as application/json.
Code:
package main
import (
"fmt"
"mime/multipart"
"net/http"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/loopfz/gadgeto/tonic"
"github.com/wI2L/fizz"
)
type CreateForm struct {
Message string `form:"message"`
File *multipart.FileHeader `form:"file"`
}
// ATTEMPT 1: tried to bind with the f parameter: marks is as JSON
func handler(c *gin.Context, f *CreateForm) error {
var form CreateForm
if err := c.ShouldBindWith(&form, binding.FormMultipart); err != nil {
return err
}
// more implementation here
return nil
}
func main() {
f := fizz.NewFromEngine(gin.New())
f.GET("/openapi", nil, f.OpenAPI(nil, "yaml"))
f.POST(
"/create",
[]fizz.OperationOption{
// ATTEMPT 2: try to bind with the fizz.InputModel: marks is as JSON
fizz.InputModel(CreateForm{}),
},
tonic.Handler(
handler,
201,
),
)
fmt.Println("Errors:", f.Errors())
srv := &http.Server{Addr: ":8090", Handler: f}
srv.ListenAndServe()
}Generated OpenAPI:
openapi: 3.0.1
info: null
paths:
/create:
post:
operationId: handler
requestBody:
content:
application/json:
schema:
type: object
properties:
File:
$ref: '#/components/schemas/MultipartFileHeader'
Message:
type: string
responses:
"201":
description: Created
components:
schemas:
MultipartFileHeader:
type: object
properties:
Filename:
type: string
Header:
type: object
additionalProperties:
type: array
items:
type: string
Size:
type: integer
format: int64Metadata
Metadata
Assignees
Labels
No labels