当前位置:网站首页>swagger中的枚举、自定义类型和swaggerignore
swagger中的枚举、自定义类型和swaggerignore
2022-06-22 08:07:00 【活火石】
swagger中的枚举、自定义类型和swaggerignore
想要一些低成本的接口文档,无疑swagger是最好的选择,对于Golang,我们选择的是swag,他可以将代码注释转换成swagger2.0文档。除了swag,还需要一个web框架的中间件包装器库。
详细介绍可以看官网,这里则介绍一些不起眼的东西,比如枚举该如何展示,自定义类型,忽略某些字段该如何去做
枚举
type ExampleRequest struct {
UserId int64 `json:"user_id,string" swaggertype:"string" example:"11111111111111"` // 用户id
Role []string `json:"role" swaggertype:"array,integer" enums:"0,1,2" example:"0,1"` // 角色id: 0-超级管理员 1-管理员,2-用户
RoleString []string `json:"role_string" swaggertype:"array,integer" enums:"0,1,2" example:"0,1"` // 角色id: 0-超级管理员 1-管理员,2-用户
Page int `json:"page" example:"1"`
Limit int `json:"limit" example:"10"`
}

从例子中可以看出,我们只需要在结构体的tag中加上enums,我们只需要用逗号隔开即可。
自定义类型swaggertype
swaggertype允许我们去自定义类型,这里拿官方例子展示
type TimestampTime struct {
time.Time
}
type Account struct {
// Override primitive type by simply specifying it via `swaggertype` tag
ID sql.NullInt64 `json:"id" swaggertype:"integer"`
// Override struct type to a primitive type 'integer' by specifying it via `swaggertype` tag
RegisterTime TimestampTime `json:"register_time" swaggertype:"primitive,integer"`
// Array types can be overridden using "array,<prim_type>" format
Coeffs []big.Float `json:"coeffs" swaggertype:"array,number"`
}
swaggerignore
使用swaggerignore标签排除字段,这个常用在文件流上传使用
type UploadFileRequest struct {
File *multipart.FileHeader `json:"file" form:"file" binding:"required" swaggerignore:"true"` // 保存的文件
}
参考:https://promacanthus.netlify.app/tools/swagger/swagger-guide/#parameterType
边栏推荐
- Interview shock 59: can there be multiple auto increment columns in a table?
- Energy and interference of waves
- Model electricity experiment -- Experiment 2 JFET common source amplifier circuit
- Upload your own library to pod
- 0 basic self-study STM32 (wildfire) -- what is a register?
- 成为一名卓越云架构师要具备的五项技能
- Mt4/mql4 getting started to be proficient in EA tutorial lesson 6 - common functions of MQL language (VI) - common order function
- .net core 技术栈 网站收集
- How Navicat queries the password information of the connected database
- 模电实验——实验一 晶体管共射极单管放大器
猜你喜欢

什么是分布式事务

QT custom composite control (class promotion function)

The significance of code coverage testing to programming white and its application
![[Oracle database] mammy tutorial day14 conversion function](/img/ef/c468316750c57212a067abd00a10e9.png)
[Oracle database] mammy tutorial day14 conversion function

Mystery of power bank

QT combox的使用示例

Mt4/mql4 getting started to proficient in foreign exchange EA automatic trading tutorial - common functions of MQL language

XMIND 2022 mind map active resources?

Example of QT combox

Stored procedures and functions of MySQL
随机推荐
(7) Bidirectional linked list
找出不是两个数组共有的元素
Maptalks: basic operation and wms/wmts map service loading
MySQL master-slave replication
OSI and tcp/ip
MySQL intercepts the string cs0000_ 1 medium_ Following characters
DTD约束
Energy and interference of waves
Restrict input type (multiple methods)
It operation and maintenance knowledge map
How Navicat queries the password information of the connected database
Failed to access the ES installed on Tencent ECs, but the solution to accessing the ES successfully on the server
QT custom composite control (class promotion function)
MySQL queries data within one hour
The significance of code coverage testing to programming white and its application
Layer drawing method
mysql查询group by 1055 问题完美解决,最简单最便捷的方法
Some suggestions on Oracle SQL query return optimization
. Net core technology stack website Collection
RAID technology