Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support objects for formData #300

Closed
lsattem opened this issue Feb 14, 2019 · 5 comments · Fixed by #770
Closed

Support objects for formData #300

lsattem opened this issue Feb 14, 2019 · 5 comments · Fixed by #770

Comments

@lsattem
Copy link

lsattem commented Feb 14, 2019

Is your feature request related to a problem? Please describe.
Not that i know of.

Describe the solution you'd like
I have not seen a solution to this in the documentation, but the documentation on @param is a bit lacking (i.e. does not mention formData being an option).
When setting @param, it would be nice if you could specify object and object type. This way it's clear what sort of input my endpoint expects. For example, the following go code...

package main

type MyStruct struct{
	AnInt int
}

// @Summary test form data
// @Accept multipart/form-data
// @Param payload formData main.MyStruct true "form data"
// @Router /test [post]
func test(){}

...provides the following swagger:

{
    "info": {
        "contact": {},
        "license": {}
    },
    "paths": {
        "/test": {
            "post": {
                "consumes": [
                    "multipart/form-data"
                ],
                "summary": "test form data",
                "parameters": [
                    {
                        "type": "main.MyStruct",
                        "description": "form data",
                        "name": "payload",
                        "in": "formData",
                        "required": true
                    }
                ]
            }
        }
    }
}

What I would like is for the swagger to include the type def, and look like this:

{
    "info": {
        "contact": {},
        "license": {}
    },
    "paths": {
        "/test": {
            "post": {
                "consumes": [
                    "multipart/form-data"
                ],
                "summary": "test form data",
                "parameters": [
                    {
                        "type": "main.MyStruct",
                        "description": "form data",
                        "name": "payload",
                        "in": "formData",
                        "required": true
                    }
                ]
            }
        }
    },
    "definitions": {
        "main.MyStruct": {
            "type": "object",
            "properties": {
                "anInt": {
                    "type": "integer"
                }
            }
        }
    }
}

Describe alternatives you've considered
An alternative, but ugly, solution is to add a dummy function that contains the struct as an object, such as this one.

// @Failure 200 {object} main.MyStruct "wish this wasn't necessary"
func dummy(){}

I could also use param type 'body', but that is also somewhat ugly (and misleading).

Additional context
N/A

@pei0804
Copy link
Member

pei0804 commented Mar 20, 2019

I see.
I'm thoughting for solution.

@JayenLee
Copy link

JayenLee commented May 8, 2020

I have the same problem

type equipmentQuery struct {
	ProductName string `json:"product_name" form:"product_name"` // 产品名称
	Sn          string `json:"sn" form:"sn"`                     // sn号
	ClientCode  string `json:"client_code" form:"client_code"`   // 代理商编号可多选用","隔开
	Page        int    `json:"page" form:"page"`                 //页数
	PageSize    int    `json:"pagesize" form:"pagesize"`         // 每页展示条数
	File        string `json:"file"form:"file" format:"binary" form:"file"`
}
// @Param file  formData equipmentQuery true "parameter"

I set a structure to use the parameter type of formdata, but when generating a swagger document, it will report equipmentquery is not supported type for formdata

Does formdata not support user defined struct yet?

@danielpoe
Copy link
Contributor

Any updates?

paramType body seems to work - paramType formData not..
(In our case the posted form application/x-www-form-urlencoded data is marshaled into a struct)

@wiliansilvazup
Copy link

wiliansilvazup commented Jun 23, 2020

@danielpoe
i'm use the follow code and work's for me

// @Tags Analysis
// @Security ApiKeyAuth
// @Description Start new analysis
// @ID start-new-analysis
// @Accept  json
// @Produce  json
// @Param fileZip formData file true "Body with file zip"
// @Success 200 {object} http.Response{content=string} "OK"
// @Success 400 {object} http.Response{content=string} "BAD REQUEST"
// @Success 404 {object} http.Response{content=string} "NOT FOUND"
// @Failure 500 {object} http.Response{content=string} "INTERNAL SERVER ERROR"
// @Router /api/analysis [post]

@birdycn
Copy link

birdycn commented Feb 23, 2021

你好,请问解决了吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants