Golang swaggo nullable(Golang swaggo 可为空)

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

Golang swaggo nullable

问题

我使用的是swaggo/swag v1.8.6。

我该如何设置字段可为空的通知?

我尝试写 extensions:"x-nullable",但没有效果。

type MyStruct struct {
    Field1 string `json:"field_id,omitempty" extensions:"x-nullable"`
}
英文:

I use swaggo/swag v1.8.6

how can I set notice that field is nullable?

I tried to write extensions:"x-nullable", but it has no effect

type MyStruct struct {
 Field1 string `json:"field_id,omitempty"  extensions:"x-nullable"`
}

答案1

得分: 1

我已经创建了一个解决方法,通过在<project_dir>/docs中创建fix.sh文件:

#!/bin/sh

sed -i "s/x-nullable/nullable/g" ./docs/docs.go
sed -i "s/x-omitempty/omitempty/g" ./docs/docs.go
sed -i "s/x-nullable/nullable/g" ./docs/swagger.json
sed -i "s/x-omitempty/omitempty/g" ./docs/swagger.json
sed -i "s/x-nullable/nullable/g" ./docs/swagger.yaml
sed -i "s/x-omitempty/omitempty/g" ./docs/swagger.yaml

然后通过以下命令运行swaggo:

swag init -g cmd/app/main.go && ./docs/fix.sh
英文:

I have made a workaround

by creating fix.sh at <project_dir>/docs

#!/bin/sh

sed -i &quot;s/x-nullable/nullable/g&quot; ./docs/docs.go
sed -i &quot;s/x-omitempty/omitempty/g&quot; ./docs/docs.go
sed -i &quot;s/x-nullable/nullable/g&quot; ./docs/swagger.json
sed -i &quot;s/x-omitempty/omitempty/g&quot; ./docs/swagger.json
sed -i &quot;s/x-nullable/nullable/g&quot; ./docs/swagger.yaml
sed -i &quot;s/x-omitempty/omitempty/g&quot; ./docs/swagger.yaml

and running swaggo by

swag init -g cmd/app/main.go &amp;&amp; ./docs/fix.sh

huangapple
  • 本文由 发表于 2022年10月14日 04:38:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/74061535.html
匿名

发表评论

匿名网友

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

确定