swag icon indicating copy to clipboard operation
swag copied to clipboard

I would like to define enum.

Open KousukeUchiyama opened this issue 2 years ago • 11 comments

What is the best way to output the following Swagger?

swagger: "2.0"

components:
  schemas:
    EnumFileStatus:
      type: string
      title: EnumFileStatus
      enum:
        - not_uploaded
        - file_error
        - data_error
        - uploaded
    ResponseUploadFile:
      title: ResponseUploadFile
      type: object
      description: ''
      properties:
        fileStatus:
          $ref: '#/components/schemas/EnumFileStatus'

I would like to define enum. We have tried the following methods, but did not achieve the intended result.

https://github.com/swaggo/swag#attribute

KousukeUchiyama avatar Oct 27 '22 06:10 KousukeUchiyama

global enums haven't been supported yet

sdghchj avatar Oct 31 '22 03:10 sdghchj

Any plan supporting this?

Zxilly avatar Nov 11 '22 15:11 Zxilly

I also would appreciate this feature very much

nicolasassi avatar Nov 16 '22 11:11 nicolasassi

I would like to give an implementation.

for example:

package types

type Class int

const (
	A Class = iota + 1 // AAA
	B                  /* BBB */
	C
	D
)

type Mask int

const (
	Mask1 Mask = 1 << iota // Mask1
	Mask2                  /* Mask2 */
	Mask3                  // Mask3
	Mask4                  // Mask4
)

type Type string

const (
	Teacher Type = "teacher" // teacher
	Student Type = "student" /* student */
	Other   Type = "Other"   // Other
	Unknown      = "Unknown"
	OtherUnknown
)

type Person struct {
	Name  string
	Class Class
	Mask  Mask
	Type  Type
}

this will output:

definitions:
  types.Class:
    type: integer
    enum:
      - 1
      - 2
      - 3
      - 4
    x-enum-comments:
      A: AAA
      B: BBB
    x-enum-varnames:
      - A
      - B
      - C
      - D
  types.Mask:
    type: integer
    enum:
      - 1
      - 2
      - 4
      - 8
    x-enum-comments:
      Mask1: Mask1
      Mask2: Mask2
      Mask3: Mask3
      Mask4: Mask4
    x-enum-varnames:
      - Mask1
      - Mask2
      - Mask3
      - Mask4
  types.Person:
    type: object
    properties:
      class:
        $ref: '#/definitions/types.Class'
      mask:
        $ref: '#/definitions/types.Mask'
      name:
        type: string
      type:
        $ref: '#/definitions/types.Type'
  types.Type:
    type: string
    enum:
      - teacher
      - student
      - Other
    x-enum-comments:
      Other: Other
      Student: student
      Teacher: teacher
    x-enum-varnames:
      - Teacher
      - Student
      - Other

sdghchj avatar Nov 19 '22 06:11 sdghchj

try the lastest master commit

sdghchj avatar Nov 20 '22 10:11 sdghchj

I tried this implementation

type Type string

const (
	TypePerson   Type = "person"   // person
	TypeBusiness Type = "business" /* business */
)

type customerIn struct {
	ClientId string `json:"client_id"`
	Type     Type   `json:"type"`
}

And then ran this cmd:

swag init -g service/server/server.go --pd
swag fmt -g service/server/server.go

I'm using with gin-swagger and it didn't work...

definitions:
  customerIn:
    properties:
      clientId:
        type: string
      type:
        type: string
    type: object

nicolasassi avatar Nov 21 '22 11:11 nicolasassi

@nicolasassi have you re-compile your swag with master branch? I tested ok

sdghchj avatar Nov 21 '22 12:11 sdghchj

@sdghchj I re-compiled with master (today) and now its not working anymore. I submitted a bug for this https://github.com/swaggo/swag/issues/1392

nicolasassi avatar Nov 22 '22 11:11 nicolasassi

@sdghchj Can this be used as data type in API Operation ?

Zxilly avatar Dec 06 '22 18:12 Zxilly

@sdghchj Can this be used as data type in API Operation ?

it is OK in response body

sdghchj avatar Dec 07 '22 05:12 sdghchj

@sdghchj I'd like to use it as @Param argument, It shows generating the type, But type didn't exist in the generated document

Zxilly avatar Dec 07 '22 10:12 Zxilly

Check later on, it works with current master version, but when using an array of enums it stop working. Described more on: #1521

Jictyvoo avatar Mar 29 '23 16:03 Jictyvoo