1

I am trying to create a spec file for the go implementation of petstore example . I tried a go generate in the directory which had doc.go but it did nothing. On further reading, I realised that there should be

//go:generate swagger generate spec -o swagger.json

in doc.go . When I added that it creates a spec file but it doesn't contain the entire specification. This is the .json file that is create on go generate

{
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "schemes": [
    "http",
    "https"
  ],
  "swagger": "2.0",
  "info": {
    "description": "the purpose of this application is to provide an application\nthat is using plain go code to define an API\n\nThis should demonstrate all the possible comment annotations\nthat are available to turn go code into a fully compliant swagger 2.0 spec",
    "title": "Petstore API.",
    "termsOfService": "there are no TOS at this moment, use at your own risk we take no responsibility\ngo:generate swagger generate spec -o swagger.json",
    "contact": {
      "name": "John Doe",
      "url": "http://john.doe.com",
      "email": "john.doe@example.com"
    },
    "license": {
      "name": "MIT",
      "url": "http://opensource.org/licenses/MIT"
    },
    "version": "0.0.1"
  },
  "host": "localhost",
  "basePath": "/v2",
  "paths": {}
}

There are no routes/parameters/models mentioned in the spec file. Is this how this is supposed to be? This is an official example from the goswagger repo so I doubt the code is wrong. What should I change to make it work?

Krash
  • 2,085
  • 3
  • 13
  • 36

1 Answers1

0

The code you want to document needs to be reachable from your main package. That means from the main package its usage needs to be detectable. So if you have a working server that isn't hooked up through some means of reflection it will generate the right specification.

Casual Jim
  • 1,179
  • 7
  • 13