4

I am following the instructions for generating swagger spec here https://goswagger.io/generate/spec.html.

I have an existing project that needs a UI for the API. I want to use go swagger but I am completely confused https://github.com/go-swagger/go-swagger/tree/master/examples/todo-list I want to set it up so I add annotations in the code and then run the command swagger generate spec and it would generate the spec However whenever I run it, it prints {"swagger":"2.0","paths":{},"definitions":{}}

This is my command to run it

...com/projectFolder]$ swagger generate spec                                                
{"swagger":"2.0","paths":{},"definitions":{}}

My project structure is as follows

project/ 
  main.go
  api/
    router.go

In main.go I have this annotation

//go:generate swagger generate spec
package main

In router above one of my handlers I have this annotation

// swagger:route GET /profile
//
// Gets profile of user
//
//     Produces:
//     - application/json
//     - application/x-protobuf
//
//     Schemes: http, https, ws, wss
//
//     Security:
//       api_key:
//       oauth: read, write
//
//     Responses:
//       default: genericError
//       200: someResponse
//       422: validationError
r.GET("/profile", profileHandler

I've been stuck trying to set up an api generator for a while. Any help is much appreciated. If you have experience setting it up, please let me know how you did it

Sakib
  • 1,503
  • 4
  • 26
  • 39

1 Answers1

3

This might be a late answer, but anyway. I faced the same problem and was struggling with go-swagger docs as well; that's what I finally came up with:

swagger generate can take input file argument (-b main.go, an entry point to your application, from where it automagically finds all the other annotations), and output file (-o swagger.json)

So in a whole your command should be like this:

swagger generate spec -b ./main.go -o swagger.json

Within my main go file, at the top of it, I have a bit different annotation than you have. It defines the basic project properties like title:

// Project title (the dot in the end matters).
//
// Project description.
//
//     Schemes: http
//     Version: 0.1
//
//     Consumes:
//     - application/json
//
//     Produces:
//     - application/json
//
//
// swagger:meta
Anton Sergeyev
  • 929
  • 1
  • 9
  • 24