Generating Swagger spec with golang comments
Asked Answered
A

2

7

I am trying to create documentation for my REST API written in golang using Swagger. I have been following this guide: https://goswagger.io/generate/spec.html to do so and have been able to create the json file from my comments in my main file, however I do not know how to create the .yaml file that would translate to the actual swagger spec. I have followed the instructions on the page and included the //go:generate swagger generate spec in my main file. Anyone know how to create the .yaml file?

Apfelstadt answered 1/6, 2016 at 0:17 Comment(1)
were you able to get this working? I am trying to do this for my own project but with no luckAnthology
W
4

The generated spec would be json not yaml at this stage, not saying it can't be made to do yaml but currently it only generates json.

you can annotate your main package with: //go:generate swagger generate spec -o swagger.json

then you can generate the spec with: go generate when you are in folder of the main package. If your main package is not your project root but something like $project_root/cmd/my-server then you can run go generate ./cmd/my-server from the project root.

Wina answered 1/6, 2016 at 3:12 Comment(0)
P
3

Yes, you can create a YAML file. Following an example.

swagger generate spec -m -o ../../api/swagger.yaml

Also, feel free to check the Blog Post.

Paleobotany answered 12/2, 2019 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.