Swagger OpenAPI 2:无法从定义模型名称中删除包

pkwftd7m  于 2023-04-11  发布在  其他
关注(0)|答案(1)|浏览(125)

我正在通过swagger为我的GoLang Web API生成一个OpenAPI 2规范。我生成的模型都在模型名称之前添加了包名称前缀,这不是我想要的行为。
当我将API注解为:

// @Summary Get Account
// @Schemes
// @Description Get account data from session token
// @Tags Account
// @Accept json
// @Produce json
// @Success 200 {object} models.Account
// @Router /account [get]
func (h *AccountController) GetAccount(c *gin.Context) {

}

models.account结构体来自我的model包,它被定义为:

// Account model info
// @Description User account information
// @Description with user id, username, nickname, avatar, roles, guild avatar, and rank
type Account struct {
    ID          string   `json:"id" validate:"required"`
    Username    string   `json:"username" validate:"required"`
    Nick        string   `json:"nick"`
    Avatar      string   `json:"avatar" validate:"required"`
    Roles       []string `json:"roles" validate:"required"`
    GuildAvatar string   `json:"guildAvatar"`
    Rank        string   `json:"rank" validate:"required"`
}

它为模型生成以下定义和名称:

definitions:
  models.Account:
    description: User account information with user id, username, nickname, avatar,
      roles, guild avatar, and rank
    properties:
      avatar:
        type: string
      guildAvatar:
        type: string
      id:
        type: string
      nick:
        type: string
      rank:
        type: string
      roles:
        items:
          type: string
        type: array
      username:
        type: string
    required:
    - avatar
    - id
    - rank
    - roles
    - username
    type: object

我希望能够生成没有包前缀“models.”的模型定义,并且只具有来自我的GoLang模型的结构体名称。
这是我想要生产的预期模型(从模型名称中删除前缀“models.”):

definitions:
  Account:
    description: User account information with user id, username, nickname, avatar,
      roles, guild avatar, and rank
    properties:
      avatar:
        type: string
      guildAvatar:
        type: string
      id:
        type: string
      nick:
        type: string
      rank:
        type: string
      roles:
        items:
          type: string
        type: array
      username:
        type: string
    required:
    - avatar
    - id
    - rank
    - roles
    - username
    type: object

有谁知道如何从我生成的模型定义中删除models.prefix吗?
注意,我使用swag库中的“swag init”来生成openapi规范:https://github.com/swaggo/swag

mbzjlibv

mbzjlibv1#

我通过向类型添加@name注解并更新API以引用新名称解决了这个问题。
例如,下面是我的struct now(注意@name注解):

// Account model info
// @Description User account information
// @Description with user id, username, nickname, avatar, roles, guild avatar, and rank
type Account struct {
    ID          string   `json:"id" validate:"required"`
    Username    string   `json:"username" validate:"required"`
    Nick        string   `json:"nick"`
    Avatar      string   `json:"avatar" validate:"required"`
    Roles       []string `json:"roles" validate:"required"`
    GuildAvatar string   `json:"guildAvatar"`
    Rank        string   `json:"rank" validate:"required"`
} //@name Account

下面是更新后的API定义,它引用了Account而不是models。Account:

// @Summary Get Account
// @Schemes
// @Description Get account data from session token
// @Tags Account
// @Accept json
// @Produce json
// @Success 200 {object} models.Account
// @Router /account [get]
func (h *AccountController) GetAccount(c *gin.Context) {

}

这导致生成的定义为:

definitions:
  Account:
    description: User account information with user id, username, nickname, avatar,
      roles, guild avatar, and rank
    properties:
      avatar:
        type: string
      guildAvatar:
        type: string
      id:
        type: string
      nick:
        type: string
      rank:
        type: string
      roles:
        items:
          type: string
        type: array
      username:
        type: string
    required:
    - avatar
    - id
    - rank
    - roles
    - username
    type: object

相关问题