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

V2: unable to produce string binary request body #1933

Open
zak905 opened this issue Nov 26, 2024 · 0 comments
Open

V2: unable to produce string binary request body #1933

zak905 opened this issue Nov 26, 2024 · 0 comments

Comments

@zak905
Copy link

zak905 commented Nov 26, 2024

Describe the bug

According to the OpenAPI 3 documentation https://swagger.io/docs/specification/v3_0/describing-request-body/file-upload/, it is possible to specify a binary file request body by specifying the type as string and the format as binary. For example:

requestBody:
  content:
    image/png:
      schema:
        type: string
        format: binary

This is somehow different from swagger 2 format which needs the usage of formData for specifying binary data

      parameters:
      - description: 'file binary data'
        in: formData
        name: payload
        required: true
        type: file

Not only the annotation for specifying a binary data file did not change from version 1 to version 2 of swag:

// @Accept  application/octet-stream
// @Param payload formData file true "file binary data"

but also the produced specs are confusing if I use --v3.1:

 operationId: test-post
      requestBody:
        content:
          application/octet-stream:
            schema:
              format: binary
              type: string
          application/x-www-form-urlencoded:
            schema:
              type: file
        description: 'file binary data'
        required: true

I am not sure why the application/x-www-form-urlencoded is part of generated of the specs. I believe it's more accurate to have only the type specified by @Accept in addition to format: binary for the definition:

     operationId: test-post
      requestBody:
        content:
          application/octet-stream:
            schema:
              format: binary
              type: string
        description: 'file binary data'
        required: true

It would help if a new annotation is added for v2 that will help in producing definitions for binary types. For example:

// @Accept  application/octet-stream
// @Param payload binary file true "file binary data"

To Reproduce
running swag init --v3.1 on a folder that contains the following file:

package main

import "fmt"

//	@title			Swagger Example API
//	@version		1.0
//	@description	This is a sample server Petstore server.
//	@termsOfService	http://swagger.io/terms/

//	@contact.name	API Support
//	@contact.url	http://www.swagger.io/support
//	@contact.email	[email protected]

//	@license.name	Apache 2.0
//	@license.url	http://www.apache.org/licenses/LICENSE-2.0.html

// @servers.url		https://petstore.swagger.io/v2
func main() {
	fmt.Println("testing")
}

// @ID test-post
// @Param payload formData file true "file binary data, acceptable formats: jpeg,png"
// @Accept  application/octet-stream
// @Produce octet-stream
// @Success 200 {file} file "response contains the file"
// @Router /test [post]
func getFile() {
	fmt.Println("test")
}

Expected behavior

     operationId: test-post
      requestBody:
        content:
          application/octet-stream:
            schema:
              format: binary
              type: string
        description: 'file binary data'
        required: true

Your swag version
v2.0.0

Your go version
1.22.4

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

No branches or pull requests

1 participant