当使用json.RawMessage时,生成go-swagger文档时出现错误。

huangapple go评论70阅读模式
英文:

Error go-swagger docs generation when use json.RawMessage

问题

我有一个使用Swagger文档生成的Go Web应用程序。最近在我的项目中添加了一个新的端点,该端点在POST和PUT请求中使用以下结构体:

Secret struct {
    // Secret unique key name.
    Name string `json:"name" example:"ACCESS_TOKEN"`

    // type: string
    // x-go-type: "string"
    Value json.RawMessage `json:"value" swagger:"type:string"`

    // Tags in which this secret is used.
    Tags []string `json:"tags" example:"dev,prod,omitempty"`
}

当我尝试使用命令swag init -md ./documentation -o ./swagger生成Swagger文档时,我遇到了以下错误:

当使用json.RawMessage时,生成go-swagger文档时出现错误。

如果我理解正确,应该显式添加类型定义,但我不知道如何正确定义类型,所有的注释组合都不起作用。这个问题与json.RawMessage类型(Value字段)有关,因为如果我将此类型替换为interface{},一切都正常工作。

英文:

I have a go web application with swagger docs generation. Recently in my project was added new endpoint that uses following struct in POST & PUT requests:

Secret struct {
		// Secret unique key name.
		Name string `json:"name" example:"ACCESS_TOKEN"`

		// type: string
		// x-go-type: "string"
		Value json.RawMessage `json:"value" swagger:"type:string"`

		// Tags in which this secret is used.
		Tags []string `json:"tags" example:"dev,prod,omitempty"`
	}

when i am attempt to build swagger docs with command: swag init -md ./documentation -o ./swagger
i am getting following error:

当使用json.RawMessage时,生成go-swagger文档时出现错误。

If i am correctly understand there should be added type definition explicitly, but i don't understand how to define type correctly, all combination of commentaries does not work. This issue is related with json.RawMessage type (Value field) because if i replace this type with interface{} everything works fine.

答案1

得分: 1

我找到了解决我的问题的方法。之前,我不得不省略使用 --parseDependency,因为生成 Swagger 文档会卡住,但是如果我同时传递 --parseDepth 1,我的文档就能成功生成。所以完整的命令是:

swag init --parseDependency --parseInternal --parseDepth 1 -md ./documentation -o ./swagger

我理解这是因为 json.RawMessage 需要导入,但是如果我不使用 --parseDependency,就找不到 json.RawMessage 类型,但是在我的问题中,单独使用 --parseDependency 会卡住,所以不能使用它。解决方法是使用 --parseDepth 参数。

英文:

I found solution for my issue, previously i have to omit usage --parseDependency because swagger doc generation hangs but if i simultaneously pass --parseDepth 1 my docs generates successfully, so a full cmd is:

swag init --parseDependency --parseInternal --parseDepth 1 -md ./documentation -o ./swagger

As i understand this occurs because json.RawMessage requires import but if i am not using --parseDependency json.RawMessage type could not be found but in my question case single --parseDependency hangs so it could not be used. Solution is to use --parseDepth argument

huangapple
  • 本文由 发表于 2021年12月23日 20:30:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/70462176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定