swag
swag copied to clipboard
I would like to define enum.
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
global enums haven't been supported yet
Any plan supporting this?
I also would appreciate this feature very much
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
try the lastest master commit
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 have you re-compile your swag with master branch? I tested ok
@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
@sdghchj Can this be used as data type
in API Operation
?
@sdghchj Can this be used as
data type
inAPI Operation
?
it is OK in response body
@sdghchj I'd like to use it as @Param
argument, It shows generating the type, But type didn't exist in the generated document
Check later on, it works with current master version, but when using an array of enums it stop working. Described more on: #1521